forked from 0xWheatyz/SPARC
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) <noreply@anthropic.com>
This commit is contained in:
+12
-7
@@ -2,14 +2,17 @@
|
|||||||
|
|
||||||
Uses APScheduler to periodically re-analyze tracked companies and
|
Uses APScheduler to periodically re-analyze tracked companies and
|
||||||
detect significant changes in patent counts.
|
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 logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from SPARC import config
|
|
||||||
from SPARC.analyzer import CompanyAnalyzer
|
from SPARC.analyzer import CompanyAnalyzer
|
||||||
from SPARC.database import DatabaseClient
|
from SPARC.auth import get_db_client
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -21,10 +24,13 @@ CHANGE_THRESHOLD_PERCENT = int(os.getenv("CHANGE_THRESHOLD_PERCENT", "20"))
|
|||||||
|
|
||||||
|
|
||||||
def run_scheduled_analysis() -> None:
|
def run_scheduled_analysis() -> None:
|
||||||
"""Re-analyze all tracked companies and check for significant changes."""
|
"""Re-analyze all tracked companies and check for significant changes.
|
||||||
db = DatabaseClient(config.database_url)
|
|
||||||
db.connect()
|
Uses the shared pooled DatabaseClient from ``SPARC.auth.get_db_client()``
|
||||||
db.initialize_schema()
|
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()
|
tracked = db.list_tracked_companies()
|
||||||
if not tracked:
|
if not tracked:
|
||||||
@@ -74,7 +80,6 @@ def run_scheduled_analysis() -> None:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Error analyzing tracked company %s: %s", name, e)
|
logger.error("Error analyzing tracked company %s: %s", name, e)
|
||||||
|
|
||||||
db.close()
|
|
||||||
logger.info("Scheduled analysis complete")
|
logger.info("Scheduled analysis complete")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user