Spaces:
Running
Running
Commit ·
b1e2dca
1
Parent(s): cdf6be8
test for readyness time
Browse files
app.py
CHANGED
|
@@ -1,63 +1,23 @@
|
|
| 1 |
-
# from fastapi import FastAPI
|
| 2 |
-
# import datetime, os
|
| 3 |
-
|
| 4 |
-
# app = FastAPI()
|
| 5 |
-
|
| 6 |
-
# @app.get("/")
|
| 7 |
-
# def home():
|
| 8 |
-
# return {"status": "running", "message": "trigger space is alive"}
|
| 9 |
-
|
| 10 |
-
# @app.get("/run")
|
| 11 |
-
# def run_task():
|
| 12 |
-
# """Simple test endpoint that creates a text file in /tmp."""
|
| 13 |
-
# filename = "/tmp/test.txt"
|
| 14 |
-
# with open(filename, "a") as f:
|
| 15 |
-
# f.write(f"Triggered at {datetime.datetime.utcnow().isoformat()}\n")
|
| 16 |
-
# print(f"✅ File created at {filename}")
|
| 17 |
-
# return {"status": "ok", "message": "test.txt created"}
|
| 18 |
-
|
| 19 |
from fastapi import FastAPI
|
| 20 |
-
import requests,
|
| 21 |
|
| 22 |
app = FastAPI()
|
| 23 |
|
| 24 |
-
DB3_URL = "https://alesamodio-db3-update.hf.space"
|
| 25 |
-
SECRET = os.getenv("CRON_SECRET", "Cielo2017.tiamo")
|
| 26 |
|
| 27 |
@app.get("/")
|
| 28 |
def home():
|
| 29 |
-
return {"status": "running", "message": "
|
| 30 |
-
|
| 31 |
-
@app.get("/
|
| 32 |
-
def
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
log.append(f"✅ Attempt {attempt}: space ready, calling /run")
|
| 44 |
-
run_url = f"{DB3_URL}/run?secret={SECRET}"
|
| 45 |
-
run_resp = requests.get(run_url, timeout=120)
|
| 46 |
-
log.append(f"Response: {run_resp.text}")
|
| 47 |
-
if '"status":"ok"' in run_resp.text:
|
| 48 |
-
log.append("🎉 DB3 update completed successfully!")
|
| 49 |
-
break
|
| 50 |
-
else:
|
| 51 |
-
log.append("⚠️ DB3 responded but not ok, retrying…")
|
| 52 |
-
else:
|
| 53 |
-
log.append(f"⏳ Attempt {attempt}: not ready yet")
|
| 54 |
-
except Exception as e:
|
| 55 |
-
log.append(f"❌ Attempt {attempt}: error {e}")
|
| 56 |
-
time.sleep(30)
|
| 57 |
-
|
| 58 |
-
finish = datetime.datetime.utcnow()
|
| 59 |
-
duration = (finish - start).total_seconds()
|
| 60 |
-
log.append(f"🏁 Finished at {finish.isoformat()} (duration {duration:.0f}s)")
|
| 61 |
-
print("\n".join(log))
|
| 62 |
-
return {"status": "done", "log": log}
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
import requests, os
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
+
DB3_URL = "https://alesamodio-db3-update.hf.space" # receiver Space URL
|
|
|
|
| 7 |
|
| 8 |
@app.get("/")
|
| 9 |
def home():
|
| 10 |
+
return {"status": "running", "message": "Minimal ping trigger alive"}
|
| 11 |
+
|
| 12 |
+
@app.get("/ping")
|
| 13 |
+
def ping_receiver():
|
| 14 |
+
"""Ping the receiver Space once to wake it up."""
|
| 15 |
+
try:
|
| 16 |
+
r = requests.get(DB3_URL, timeout=10)
|
| 17 |
+
return {
|
| 18 |
+
"status": "ok",
|
| 19 |
+
"code": r.status_code,
|
| 20 |
+
"response": r.text[:200]
|
| 21 |
+
}
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return {"status": "error", "error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|