Avinashnalla7 commited on
Commit
9d416ac
·
1 Parent(s): 810e18a

Add startup background heartbeat loop

Browse files
Files changed (4) hide show
  1. app/background.py +11 -0
  2. app/config.py +1 -1
  3. app/drive_client.py +18 -0
  4. app/main.py +28 -2
app/background.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ from app.drive_client import write_heartbeat_to_drive
3
+
4
+ async def heartbeat_loop(root_folder_id: str, poll_seconds: int):
5
+ # ✅ once on startup (immediate)
6
+ write_heartbeat_to_drive(root_folder_id)
7
+
8
+ # ✅ then every poll_seconds
9
+ while True:
10
+ await asyncio.sleep(poll_seconds)
11
+ write_heartbeat_to_drive(root_folder_id)
app/config.py CHANGED
@@ -1,4 +1,4 @@
1
  import os
2
 
3
- ROOT_FOLDER_ID = os.getenv("ROOT_FOLDER_ID")
4
  POLL_SECONDS = int(os.getenv("POLL_SECONDS", "300"))
 
1
  import os
2
 
3
+ ROOT_FOLDER_ID = os.getenv("ROOT_FOLDER_ID","")
4
  POLL_SECONDS = int(os.getenv("POLL_SECONDS", "300"))
app/drive_client.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from datetime import datetime, timezone
3
+
4
+ def heartbeat_payload(version: str = "0.1.0") -> dict:
5
+ return {
6
+ "service": "sa-resume-worker",
7
+ "status": "alive",
8
+ "last_seen": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
9
+ "version": version,
10
+ }
11
+
12
+ def write_heartbeat_to_drive(root_folder_id: str) -> None:
13
+ """
14
+ TODO: Replace this with real Google Drive API upload/update.
15
+ For now: print payload so you prove the coroutine timing works.
16
+ """
17
+ payload = heartbeat_payload()
18
+ print(f"[HEARTBEAT] root={root_folder_id} payload={json.dumps(payload)}")
app/main.py CHANGED
@@ -1,10 +1,36 @@
 
1
  from fastapi import FastAPI
 
2
  from app.health import router as health_router
 
 
3
 
4
  app = FastAPI(title="SA Resume Worker")
5
 
 
6
  @app.get("/")
7
  def root():
8
- return {"service": "sa-resume-worker", "status": "running"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- app.include_router(health_router)
 
 
 
 
 
 
 
1
+ import asyncio
2
  from fastapi import FastAPI
3
+
4
  from app.health import router as health_router
5
+ from app.config import ROOT_FOLDER_ID, POLL_SECONDS
6
+ from app.background import heartbeat_loop
7
 
8
  app = FastAPI(title="SA Resume Worker")
9
 
10
+ # Root endpoint (for HF UI)
11
  @app.get("/")
12
  def root():
13
+ return {
14
+ "service": "sa-resume-worker",
15
+ "status": "running"
16
+ }
17
+
18
+ # Health endpoint
19
+ app.include_router(health_router)
20
+
21
+ # 🔹 STARTUP HOOK
22
+ @app.on_event("startup")
23
+ async def start_background_tasks():
24
+ # ---- TEMPORARY CONFIG LOGS (REMOVE LATER) ----
25
+ print("[STARTUP] Worker starting up")
26
+ print(f"[CONFIG] ROOT_FOLDER_ID set: {bool(ROOT_FOLDER_ID)}")
27
+ print(f"[CONFIG] POLL_SECONDS = {POLL_SECONDS}")
28
+ # --------------------------------------------
29
 
30
+ # Start ONE background coroutine (do not await)
31
+ asyncio.create_task(
32
+ heartbeat_loop(
33
+ root_folder_id=ROOT_FOLDER_ID,
34
+ poll_seconds=POLL_SECONDS
35
+ )
36
+ )