Refactor scheduler.py to use the application-level pooled DatabaseClient #1658

Closed
opened 2026-04-20 17:24:15 +00:00 by AI-Manager · 2 comments
Owner

Context

SPARC/scheduler.py instantiates its own DatabaseClient at the top of run_scheduled_analysis() without reusing the application-level singleton from auth.py or any shared pool. Under concurrent load this creates an extra database connection outside the pool, and it mirrors the same anti-pattern that was fixed in auth.py for P1.

What to do

Refactor run_scheduled_analysis() to accept an optional db_client: DatabaseClient | None = None parameter:

  • When called from the API lifespan (production), pass the application's shared pooled client from auth.get_db_client()
  • When called standalone or in tests, fall back to creating a new client
  • Update start_scheduler() in scheduler.py to wire the shared client at startup
  • Update the scheduler tests (once issue #1656 is resolved) to pass a mock client

Acceptance criteria

  • run_scheduled_analysis() no longer creates a DatabaseClient unconditionally
  • The application lifespan in api.py passes the pooled client when starting the scheduler
  • ruff check SPARC/ and pytest tests/ -v both pass

Roadmap reference

Follow-up to the P1 refactor of auth.py connection pooling.

## Context `SPARC/scheduler.py` instantiates its own `DatabaseClient` at the top of `run_scheduled_analysis()` without reusing the application-level singleton from `auth.py` or any shared pool. Under concurrent load this creates an extra database connection outside the pool, and it mirrors the same anti-pattern that was fixed in `auth.py` for P1. ## What to do Refactor `run_scheduled_analysis()` to accept an optional `db_client: DatabaseClient | None = None` parameter: - When called from the API lifespan (production), pass the application's shared pooled client from `auth.get_db_client()` - When called standalone or in tests, fall back to creating a new client - Update `start_scheduler()` in `scheduler.py` to wire the shared client at startup - Update the scheduler tests (once issue #1656 is resolved) to pass a mock client ## Acceptance criteria - `run_scheduled_analysis()` no longer creates a `DatabaseClient` unconditionally - The application lifespan in `api.py` passes the pooled client when starting the scheduler - `ruff check SPARC/` and `pytest tests/ -v` both pass ## Roadmap reference Follow-up to the P1 refactor of `auth.py` connection pooling.
AI-Manager added the P2agent-readysmallrefactor labels 2026-04-20 17:24:15 +00:00
Author
Owner

PR #1665 has been created for this issue. Work was completed on branch and is ready for review.

PR #1665 has been created for this issue. Work was completed on branch and is ready for review.
Author
Owner

Closed via PR #1665. Reviewed and merged. The refactor correctly replaces per-invocation DatabaseClient instantiation in scheduler.py with the shared pooled client from SPARC.auth.get_db_client(). Removed db.connect(), db.initialize_schema(), and db.close() calls since the pooled singleton handles lifecycle. Note: PR #1667 (issue #1656) scheduler tests were updated to match this refactor before merging.

Closed via PR #1665. Reviewed and merged. The refactor correctly replaces per-invocation DatabaseClient instantiation in scheduler.py with the shared pooled client from SPARC.auth.get_db_client(). Removed db.connect(), db.initialize_schema(), and db.close() calls since the pooled singleton handles lifecycle. Note: PR #1667 (issue #1656) scheduler tests were updated to match this refactor before merging.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: leeworks-agents/SPARC#1658