fix: FF auto-import always runs if CSV present, deletes CSV after success
- Remove count>0 guard — import runs regardless of existing rows (idempotent upsert) - After successful import, CSV is deleted from /app so next restart skips cleanly - No more need to manually trigger import or remove CSV from git separately Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -95,30 +95,27 @@ def startup():
|
||||
from services.institutional_scheduler import start_institutional_scheduler
|
||||
start_institutional_scheduler()
|
||||
|
||||
# Auto-import Forex Factory CSV if ff_calendar table is empty
|
||||
# Auto-import Forex Factory CSV if file is present (force, then delete CSV)
|
||||
import threading
|
||||
def _auto_import_ff():
|
||||
import time, os
|
||||
from services.database import get_conn
|
||||
import os
|
||||
from routers.eco import _find_csv, _run_ff_import, _ff_import_status
|
||||
csv_path = _find_csv()
|
||||
if not csv_path:
|
||||
print("[Startup] forex_factory_cache.csv not found — skipping FF auto-import", flush=True)
|
||||
return
|
||||
print(f"[Startup] FF CSV found at {csv_path} — importing (force) …", flush=True)
|
||||
_run_ff_import(csv_path)
|
||||
result = _ff_import_status.get("last_result") or {}
|
||||
inserted = result.get("inserted", 0)
|
||||
total = result.get("total", 0)
|
||||
print(f"[Startup] FF import done — {inserted} inserted / {total} total", flush=True)
|
||||
# Delete CSV after successful import so next restart skips automatically
|
||||
try:
|
||||
conn = get_conn()
|
||||
count = conn.execute("SELECT COUNT(*) FROM ff_calendar").fetchone()[0]
|
||||
conn.close()
|
||||
if count > 0:
|
||||
print(f"[Startup] ff_calendar already has {count} rows — skipping auto-import", flush=True)
|
||||
return
|
||||
csv_path = _find_csv()
|
||||
if not csv_path:
|
||||
print("[Startup] forex_factory_cache.csv not found — skipping auto-import", flush=True)
|
||||
return
|
||||
print(f"[Startup] ff_calendar empty — auto-importing {csv_path} …", flush=True)
|
||||
_run_ff_import(csv_path)
|
||||
result = _ff_import_status.get("last_result") or {}
|
||||
inserted = result.get("inserted", 0)
|
||||
print(f"[Startup] FF auto-import done — {inserted} rows inserted", flush=True)
|
||||
os.remove(csv_path)
|
||||
print(f"[Startup] CSV deleted: {csv_path}", flush=True)
|
||||
except Exception as e:
|
||||
print(f"[Startup] FF auto-import error: {e}", flush=True)
|
||||
print(f"[Startup] Could not delete CSV: {e}", flush=True)
|
||||
|
||||
threading.Thread(target=_auto_import_ff, daemon=True).start()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user