Calendar synchro

This commit is contained in:
OpenSquared
2026-06-28 12:15:25 +02:00
parent 98d6be8212
commit 79d4a9f741
6 changed files with 237 additions and 90 deletions

View File

@@ -106,41 +106,28 @@ def startup():
from services.institutional_scheduler import start_institutional_scheduler
start_institutional_scheduler()
# Daily calendars sync — FF live (this week) + TE upcoming (if key configured)
# Calendar sync loop — FXStreet primary, FF fallback; interval from calendar_refresh_h config (default 6h)
import threading, time as _time
def _daily_calendar_sync():
_time.sleep(60) # let server fully boot first
def _calendar_sync_loop():
_time.sleep(60) # let server fully boot
while True:
try:
# FF live JSON — this week actuals
from services.ff_calendar import sync_live
r = sync_live()
print(f"[Daily] FF live sync: {r}", flush=True)
from datetime import datetime as _dt, timezone as _tz, timedelta as _td
interval_h = float(get_config("calendar_refresh_h") or 6)
from services.calendar_sync import calendar_sync, set_next_run
set_next_run((_dt.now(_tz.utc) + _td(hours=interval_h)).isoformat())
r = calendar_sync()
src = r.get("source") or ("ff_fallback" if r.get("_fallback") else "fxstreet")
print(f"[Calendar] Sync done — {src}, upserted={r.get('total_upserted', '?')}", flush=True)
except Exception as e:
print(f"[Daily] FF live sync error: {e}", flush=True)
print(f"[Calendar] Sync loop error: {e}", flush=True)
try:
# FXStreet — upcoming forecasts (no API key needed)
from services.fxstreet_calendar import fetch_upcoming as fxs_fetch
r = fxs_fetch(weeks_ahead=6)
print(f"[Daily] FXStreet sync: {r.get('total_upserted', 0)} upserted", flush=True)
except Exception as e:
print(f"[Daily] FXStreet sync error: {e}", flush=True)
interval_h = float(get_config("calendar_refresh_h") or 6)
except Exception:
interval_h = 6
_time.sleep(int(interval_h * 3600))
try:
# FMP — upcoming forecasts (only if key is set + Starter plan)
from services.database import get_config
fmp_key = get_config("fmp_api_key") or ""
if fmp_key:
from services.fmp_calendar import fetch_upcoming
r = fetch_upcoming(weeks_ahead=6)
print(f"[Daily] FMP sync: {r.get('total_upserted', 0)} upserted", flush=True)
except Exception as e:
print(f"[Daily] FMP sync error: {e}", flush=True)
_time.sleep(86400) # repeat every 24h
threading.Thread(target=_daily_calendar_sync, daemon=True).start()
threading.Thread(target=_calendar_sync_loop, daemon=True).start()
# Auto-import Forex Factory CSV if file is present (force, then delete CSV)
import threading