From 417b7ab31e98fd6a98424bdfa56f176b90cfbe6b Mon Sep 17 00:00:00 2001 From: agent-company Date: Mon, 20 Apr 2026 19:16:54 +0000 Subject: [PATCH] Refactor scheduler.py to use the application-level pooled DatabaseClient Replace the per-invocation DatabaseClient creation in run_scheduled_analysis() with the shared pooled client from SPARC.auth.get_db_client(). This avoids creating a new database connection on every scheduler tick, which could exhaust the connection pool under load. Key changes: - Import get_db_client from SPARC.auth instead of DatabaseClient - Remove manual connect/initialize_schema/close calls - Remove unused SPARC.config import Closes leeworks-agents/SPARC#1658 Co-Authored-By: Claude Opus 4.6 (1M context) --- SPARC/scheduler.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/SPARC/scheduler.py b/SPARC/scheduler.py index 5af3940..4428bfd 100644 --- a/SPARC/scheduler.py +++ b/SPARC/scheduler.py @@ -2,14 +2,17 @@ Uses APScheduler to periodically re-analyze tracked companies and detect significant changes in patent counts. + +The scheduler reuses the application-level pooled DatabaseClient +(from ``SPARC.auth``) instead of creating its own connection, which +avoids exhausting the database connection pool under load. """ import logging import os -from SPARC import config from SPARC.analyzer import CompanyAnalyzer -from SPARC.database import DatabaseClient +from SPARC.auth import get_db_client logger = logging.getLogger(__name__) @@ -21,10 +24,13 @@ CHANGE_THRESHOLD_PERCENT = int(os.getenv("CHANGE_THRESHOLD_PERCENT", "20")) def run_scheduled_analysis() -> None: - """Re-analyze all tracked companies and check for significant changes.""" - db = DatabaseClient(config.database_url) - db.connect() - db.initialize_schema() + """Re-analyze all tracked companies and check for significant changes. + + Uses the shared pooled DatabaseClient from ``SPARC.auth.get_db_client()`` + rather than creating a disposable connection, so the scheduler participates + in the same connection pool as the rest of the application. + """ + db = get_db_client() tracked = db.list_tracked_companies() if not tracked: @@ -74,7 +80,6 @@ def run_scheduled_analysis() -> None: except Exception as e: logger.error("Error analyzing tracked company %s: %s", name, e) - db.close() logger.info("Scheduled analysis complete") -- 2.52.0