Added periodic fetch

This commit is contained in:
2025-11-29 09:48:50 +01:00
parent 90f0e14f6e
commit 47f4a22bef
4 changed files with 49 additions and 3 deletions

View File

@@ -1,10 +1,13 @@
import asyncio
import logging
from pathlib import Path
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from routes.fetch import router as fetch_router
from routes.fetch import router as fetch_router, run_fetch
from routes.events import router as events_router
from config import ENABLE_PERIODIC_FETCH, FETCH_INTERVAL_MINUTES
app = FastAPI()
@@ -15,3 +18,33 @@ app.include_router(events_router, prefix="/api")
# works regardless of the working directory used to start uvicorn.
frontend_dir = Path(__file__).parent / "frontend"
app.mount("/", StaticFiles(directory=frontend_dir, html=True), name="frontend")
logger = logging.getLogger("aoc.fetcher")
async def _periodic_fetch():
while True:
try:
await asyncio.to_thread(run_fetch)
logger.info("Periodic fetch completed.")
except Exception as exc:
logger.error("Periodic fetch failed: %s", exc)
await asyncio.sleep(FETCH_INTERVAL_MINUTES * 60)
@app.on_event("startup")
async def start_periodic_fetch():
if ENABLE_PERIODIC_FETCH:
app.state.fetch_task = asyncio.create_task(_periodic_fetch())
@app.on_event("shutdown")
async def stop_periodic_fetch():
task = getattr(app.state, "fetch_task", None)
if task:
task.cancel()
try:
await task
except Exception:
pass