rohitdeshmukh318 commited on
Commit
ea93193
Β·
1 Parent(s): c1bd687

Add self-ping background task to prevent HF Space from sleeping

Browse files
Files changed (1) hide show
  1. api/main.py +22 -1
api/main.py CHANGED
@@ -24,6 +24,21 @@ from api.routers import profile as profile_router
24
  # ── Rate limiter ──────────────────────────────────────────────────────────────
25
  limiter = Limiter(key_func=get_remote_address)
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  @asynccontextmanager
29
  async def lifespan(app: FastAPI):
@@ -34,8 +49,14 @@ async def lifespan(app: FastAPI):
34
  print("[Lifespan] Loading embedding model (first run may download ~420MB)...")
35
  get_embedder()
36
  print("[Lifespan] Embedding model loaded successfully.")
 
 
 
 
37
  yield
38
- # Shutdown: close DB pool
 
 
39
  try:
40
  from db.pool import get_pool
41
  get_pool().closeall()
 
24
  # ── Rate limiter ──────────────────────────────────────────────────────────────
25
  limiter = Limiter(key_func=get_remote_address)
26
 
27
+ import asyncio
28
+ import httpx
29
+
30
+ async def keep_awake():
31
+ """Background task to ping the server and prevent HF Space from sleeping."""
32
+ while True:
33
+ try:
34
+ await asyncio.sleep(600) # Ping every 10 minutes
35
+ async with httpx.AsyncClient() as client:
36
+ await client.get("http://127.0.0.1:7860/health", timeout=10.0)
37
+ print("[Keep-Awake] Pinged health endpoint to prevent sleep.")
38
+ except asyncio.CancelledError:
39
+ break
40
+ except Exception as e:
41
+ print(f"[Keep-Awake] Ping failed: {e}")
42
 
43
  @asynccontextmanager
44
  async def lifespan(app: FastAPI):
 
49
  print("[Lifespan] Loading embedding model (first run may download ~420MB)...")
50
  get_embedder()
51
  print("[Lifespan] Embedding model loaded successfully.")
52
+
53
+ # Start the keep-awake background task
54
+ ping_task = asyncio.create_task(keep_awake())
55
+
56
  yield
57
+
58
+ # Shutdown: cancel task and close DB pool
59
+ ping_task.cancel()
60
  try:
61
  from db.pool import get_pool
62
  get_pool().closeall()