Spaces:
Running
Running
| """ | |
| trigger_proactive/app.py | |
| βββββββββββββββββββββββββ | |
| Lightweight HF Space trigger. | |
| Wakes quickly, then polls the main Socrates Docker until ready, | |
| then calls /proactive/run. | |
| Env vars required: | |
| SOCRATES_URL β main Docker URL (e.g. https://alesamodio.hf.space) | |
| PROACTIVE_TRIGGER_SECRET β shared secret (must match main Docker) | |
| """ | |
| import os | |
| import time | |
| import requests | |
| from fastapi import FastAPI, Request, HTTPException | |
| app = FastAPI() | |
| SOCRATES_URL = os.environ.get("SOCRATES_URL", "").rstrip("/") | |
| TRIGGER_SECRET = os.environ.get("PROACTIVE_TRIGGER_SECRET", "") | |
| POLL_INTERVAL = 15 # seconds between readiness checks | |
| POLL_MAX = 40 # max attempts β 40 Γ 15s = 10 minutes | |
| def health(): | |
| return {"status": "trigger_proactive alive"} | |
| def run(request: Request): | |
| # ββ Auth ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if TRIGGER_SECRET: | |
| provided = request.headers.get("x-trigger-secret", "") | |
| if provided != TRIGGER_SECRET: | |
| raise HTTPException(status_code=403, detail="Invalid trigger secret") | |
| if not SOCRATES_URL: | |
| raise HTTPException(status_code=500, detail="SOCRATES_URL not configured") | |
| # ββ Step 1: send wake-up ping βββββββββββββββββββββββββββββββββββββββββββββ | |
| print(f"[trigger] Waking {SOCRATES_URL} ...") | |
| try: | |
| requests.get(SOCRATES_URL + "/ready", timeout=10) | |
| except Exception: | |
| pass # expected to fail or time out while waking | |
| # ββ Step 2: poll until ready ββββββββββββββββββββββββββββββββββββββββββββββ | |
| print("[trigger] Polling until main Docker is ready ...") | |
| ready = False | |
| for attempt in range(1, POLL_MAX + 1): | |
| try: | |
| r = requests.get(SOCRATES_URL + "/ready", timeout=10) | |
| if r.status_code in (200, 422): | |
| print(f"[trigger] Ready after {attempt} attempts ({attempt * POLL_INTERVAL}s)") | |
| ready = True | |
| break | |
| except Exception as e: | |
| print(f"[trigger] attempt {attempt}: {e}") | |
| time.sleep(POLL_INTERVAL) | |
| if not ready: | |
| raise HTTPException(status_code=504, detail="Main Docker did not wake in time") | |
| # ββ Step 3: call /proactive/run βββββββββββββββββββββββββββββββββββββββββββ | |
| print("[trigger] Calling /proactive/run ...") | |
| try: | |
| resp = requests.post( | |
| SOCRATES_URL + "/proactive/run", | |
| headers={ | |
| "Content-Type": "application/json", | |
| "x-trigger-secret": TRIGGER_SECRET, | |
| }, | |
| json={}, | |
| timeout=300, # proactive pipeline can take a few minutes for many users | |
| ) | |
| result = resp.json() | |
| print(f"[trigger] Done. sent={result.get('sent', '?')}") | |
| return result | |
| except Exception as e: | |
| raise HTTPException(status_code=502, detail=f"Proactive pipeline error: {e}") | |