""" Weekly scheduler for institutional reports: - CFTC COT: released every Friday ~15:30 ET → fetch Saturday UTC - EIA Petroleum Weekly: released every Wednesday ~10:30 ET → fetch Wednesday afternoon UTC Checks once per hour; only fetches when the day matches and report not yet fetched this week. """ import logging import threading from datetime import datetime, timedelta from typing import Optional logger = logging.getLogger(__name__) _stop_event = threading.Event() _thread: Optional[threading.Thread] = None _CHECK_INTERVAL_S = 3600 # check every hour def _should_fetch_cot() -> bool: """Saturday UTC = day after COT release.""" now = datetime.utcnow() return now.weekday() == 5 # Saturday def _should_fetch_eia() -> bool: """Wednesday afternoon UTC.""" now = datetime.utcnow() return now.weekday() == 2 and now.hour >= 16 # Wednesday ≥16:00 UTC def _last_fetch_date(report_type: str) -> Optional[str]: try: from services.database import get_conn conn = get_conn() try: row = conn.execute( "SELECT MAX(fetch_date) FROM institutional_reports WHERE report_type=?", (report_type,), ).fetchone() return (row[0] or "")[:10] if row else None finally: conn.close() except Exception: return None def _run_loop(): logger.info("[InstitutionalScheduler] Started") while not _stop_event.is_set(): try: today = datetime.utcnow().strftime("%Y-%m-%d") if _should_fetch_cot(): last = _last_fetch_date("cot") if last != today: logger.info("[InstitutionalScheduler] Fetching COT...") try: from services.cot_fetcher import fetch_cot_report from routers.institutional import save_institutional_report report = fetch_cot_report() if report: save_institutional_report(report) logger.info(f"[InstitutionalScheduler] COT saved: {report['report_date']}") except Exception as e: logger.warning(f"[InstitutionalScheduler] COT fetch failed: {e}") if _should_fetch_eia(): last = _last_fetch_date("eia") if last != today: logger.info("[InstitutionalScheduler] Fetching EIA...") try: from services.database import get_config from services.eia_fetcher import fetch_eia_report from routers.institutional import save_institutional_report key = get_config("eia_api_key") or "" if key: report = fetch_eia_report(key) if report: save_institutional_report(report) logger.info(f"[InstitutionalScheduler] EIA saved: {report['report_date']}") else: logger.info("[InstitutionalScheduler] EIA skipped — no API key configured") except Exception as e: logger.warning(f"[InstitutionalScheduler] EIA fetch failed: {e}") except Exception as e: logger.warning(f"[InstitutionalScheduler] Loop error: {e}") _stop_event.wait(_CHECK_INTERVAL_S) logger.info("[InstitutionalScheduler] Stopped") def start_institutional_scheduler(): global _thread _stop_event.clear() _thread = threading.Thread(target=_run_loop, daemon=True, name="institutional-scheduler") _thread.start() def stop_institutional_scheduler(): _stop_event.set() if _thread and _thread.is_alive(): _thread.join(timeout=5)