Skip to content

refactor(services): shared FastAPI bootstrap and db-pool lifespan factory - #15

Closed
rsanghvi21 wants to merge 1 commit into
fix/worker-fault-lock-magnitude-docsfrom
refactor/shared-service-bootstrap
Closed

rsanghvi21 wants to merge 1 commit into
fix/worker-fault-lock-magnitude-docsfrom
refactor/shared-service-bootstrap

Conversation

@rsanghvi21

Copy link
Copy Markdown
Collaborator

Build-Plan Step Implemented

  • Step: A1.1/A1.2/A1.3/A1.8 (refactor across already-completed services).

Checkpoint Execution & Output

$ uv run pytest tests/unit/services/test_bootstrap.py -v
5 passed in 1.14s

$ uv run pytest tests/unit/services --ignore=tests/unit/services/test_edge_gateway.py -k "not test_run_loadgen_with_owned_client_and_zero_rps" -v
89 passed, 1 deselected

$ uv run pytest tests/unit --ignore=tests/unit/services/test_edge_gateway.py -k "not test_run_loadgen_with_owned_client_and_zero_rps" --cov=understudy --cov-branch --cov-fail-under=100
198 passed, 1 deselected, 100.00% coverage

$ uv run ruff check . && uv run ruff format --check . && uv run mypy --strict src tests && uv run lint-imports && uv run python scripts/check_mocks.py
All green.

test_edge_gateway.py is excluded for the pre-existing Windows-local-only
reason noted in PR #12/#13 (real httpx.AsyncClient() construction). I
additionally verified get_items/create_item directly (mocking
app.state.http_client, bypassing the lifespan) to confirm the routes still
work correctly under create_service_app.

ADRs Relied On

  • None.

Mock Registry Disclosures

  • None.

Documentation Updates

  • None required -- internal-only refactor, no documented contract or
    tunable changed. Route behavior, readiness checks, and fault endpoints are
    unchanged; only the bootstrap/pool-lifecycle plumbing moved.

Definition of Done

  • Implementation complete for the named build-plan step
  • Unit tests written; 100% line and branch coverage of the added lines
  • make check green (see checkpoint output; one pre-existing Windows-local-only file excluded, unrelated, independently verified)
  • Checkpoint command run and output matches, OR marked unverified with a reason
  • Docs updated where this change made them untrue (none required)
  • No new # MOCKED: marker without a row in docs/MOCKS.md
  • Conventional commit(s), conventional branch, PR opened, never pushed to main

Context

A /code-review pass against PR #6 flagged duplication across the four
services/*/app.py files: an identical 4-line FastAPI/metrics/fault-
middleware/fault-routes bootstrap, and a near-identical open-pool/assign-to-
fault-manager/optional-init/close skeleton repeated in auth_service,
data_service, and worker. services/_common/ already existed as the
shared layer (metrics, faults, health, db) but had no composing factory for
either pattern. Added create_service_app and db_pool_lifespan there and
refactored all four services to use them; edge_gateway (no database) keeps
its own lifespan for its httpx.AsyncClient.

Existing tests that patched create_pool on each service module now patch
it on services._common.bootstrap, where the call actually happens now;
route/readiness/fault-endpoint behavior is otherwise unchanged, confirmed by
the existing per-service tests passing unmodified apart from that patch
target.

Base branch is fix/worker-fault-lock-magnitude-docs (PR #13, still open),
since this PR's worker/app.py refactor builds directly on that PR's
_worker_iteration/worker_loop fix -- rebasing onto
feat/policies-and-loadgen would otherwise conflict. Once #13 merges, this
PR's diff will narrow to just this PR's changes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CzBeBUsZmE38K1kWeLMhNh

…tory

All four services/*/app.py files repeated an identical 4-line
bootstrap (FastAPI(...), setup_metrics, setup_fault_middleware,
setup_fault_routes), and auth_service, data_service, and worker
repeated a near-identical open-pool/assign-to-fault-manager/optional
-init/close skeleton around their connection pool lifecycle.

Add services/_common/bootstrap.py:
- create_service_app(title, fault_manager, lifespan) does the shared
  4-line bootstrap, returning the constructed app.
- db_pool_lifespan(database_url, fault_manager, on_open=None) is an
  async context manager doing the shared pool lifecycle, yielding the
  pool (or None) so each service assigns it to its own module-level
  db_pool global -- the helper has no way to reach into another
  module's globals itself. on_open lets worker/data_service pass
  their init_worker_db/init_db callbacks; a PsycopgError from it is
  logged and swallowed, matching prior behavior.

Refactor all four app.py files to use create_service_app; refactor
auth_service, data_service, and worker to use db_pool_lifespan.
edge_gateway has no database and keeps its own lifespan (it manages
an httpx.AsyncClient instead). Route registration and readiness-check
logic stay in each service's own file.

Existing tests that patched create_pool on the service module now
patch it on services._common.bootstrap, where the call actually
happens.

Implements build-plan step A1.1/A1.2/A1.3/A1.8 (refactor across
already-completed services).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CzBeBUsZmE38K1kWeLMhNh
@Harikeshav-R

Copy link
Copy Markdown
Owner

Closed as integrated into main via PR #6.

@Harikeshav-R
Harikeshav-R deleted the refactor/shared-service-bootstrap branch September 11, 2026 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants