Spaces:
Sleeping
Sleeping
Upadte status
Browse files
main.py
CHANGED
|
@@ -5,21 +5,32 @@ import requests
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
# Root endpoint to confirm the service is running
|
| 9 |
@app.get("/")
|
| 10 |
async def root():
|
| 11 |
return {"message": "Scheduler Space is running. Use /schedule_every5_multiple/"}
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
scheduler = BackgroundScheduler()
|
| 14 |
scheduler.start()
|
| 15 |
|
| 16 |
-
# Job: send POST to given URL
|
| 17 |
def do_post(url: str):
|
| 18 |
try:
|
| 19 |
resp = requests.post(url, json={})
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
-
print(f"[{datetime.utcnow()}] ERROR posting to {url}: {e}")
|
| 23 |
|
| 24 |
# Schedule multiple endpoints every 5 minutes
|
| 25 |
default_description = "List of URLs to call every 5 minutes"
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
# In-memory store of last run timestamps
|
| 9 |
+
last_runs: dict[str, str] = {}
|
| 10 |
+
|
| 11 |
# Root endpoint to confirm the service is running
|
| 12 |
@app.get("/")
|
| 13 |
async def root():
|
| 14 |
return {"message": "Scheduler Space is running. Use /schedule_every5_multiple/"}
|
| 15 |
|
| 16 |
+
# Status endpoint to view last run times
|
| 17 |
+
@app.get("/status/")
|
| 18 |
+
async def status():
|
| 19 |
+
return {"last_runs": last_runs}
|
| 20 |
+
|
| 21 |
+
# Initialize and start the scheduler
|
| 22 |
scheduler = BackgroundScheduler()
|
| 23 |
scheduler.start()
|
| 24 |
|
| 25 |
+
# Job: send POST to given URL and record timestamp
|
| 26 |
def do_post(url: str):
|
| 27 |
try:
|
| 28 |
resp = requests.post(url, json={})
|
| 29 |
+
timestamp = datetime.utcnow().isoformat() + 'Z'
|
| 30 |
+
last_runs[url] = timestamp
|
| 31 |
+
print(f"[{timestamp}] POST {url} → {resp.status_code}")
|
| 32 |
except Exception as e:
|
| 33 |
+
print(f"[{datetime.utcnow().isoformat()}Z] ERROR posting to {url}: {e}")
|
| 34 |
|
| 35 |
# Schedule multiple endpoints every 5 minutes
|
| 36 |
default_description = "List of URLs to call every 5 minutes"
|