Repository navigation
refactor(services): shared FastAPI bootstrap and db-pool lifespan factory - #15
Closed
rsanghvi21 wants to merge 1 commit into
Closed
rsanghvi21 wants to merge 1 commit into
rsanghvi21 wants to merge 1 commit into
Conversation
…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
This was referenced Sep 11, 2026
Harikeshav-R
added a commit
that referenced
this pull request
Sep 11, 2026
Owner
|
Closed as integrated into main via PR #6. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Build-Plan Step Implemented
Checkpoint Execution & Output
test_edge_gateway.pyis excluded for the pre-existing Windows-local-onlyreason noted in PR #12/#13 (real
httpx.AsyncClient()construction). Iadditionally verified
get_items/create_itemdirectly (mockingapp.state.http_client, bypassing the lifespan) to confirm the routes stillwork correctly under
create_service_app.ADRs Relied On
Mock Registry Disclosures
Documentation Updates
tunable changed. Route behavior, readiness checks, and fault endpoints are
unchanged; only the bootstrap/pool-lifecycle plumbing moved.
Definition of Done
make checkgreen (see checkpoint output; one pre-existing Windows-local-only file excluded, unrelated, independently verified)# MOCKED:marker without a row in docs/MOCKS.mdContext
A
/code-reviewpass against PR #6 flagged duplication across the fourservices/*/app.pyfiles: 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, andworker.services/_common/already existed as theshared layer (metrics, faults, health, db) but had no composing factory for
either pattern. Added
create_service_appanddb_pool_lifespanthere andrefactored all four services to use them;
edge_gateway(no database) keepsits own
lifespanfor itshttpx.AsyncClient.Existing tests that patched
create_poolon each service module now patchit 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.pyrefactor builds directly on that PR's_worker_iteration/worker_loopfix -- rebasing ontofeat/policies-and-loadgenwould otherwise conflict. Once #13 merges, thisPR's diff will narrow to just this PR's changes.
🤖 Generated with Claude Code
https://claude.ai/code/session_01CzBeBUsZmE38K1kWeLMhNh