File size: 1,315 Bytes
9d837ba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from contextlib import asynccontextmanager
import asyncio
from api.routes import router
from api.routers.auth import router as auth_router
from api.dependencies import patient_service
async def session_sweeper_task():
"""Background task to sweep idle sessions every 5 minutes."""
while True:
try:
# Sweep sessions inactive for 30 minutes
patient_service.sweep_abandoned_sessions(timeout_minutes=30)
except Exception as e:
print(f"[Sweeper Error] {e}")
# Sleep for 5 minutes
await asyncio.sleep(300)
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup: Start the background sweeper
sweeper = asyncio.create_task(session_sweeper_task())
yield
# Shutdown: Cancel the task
sweeper.cancel()
try:
await sweeper
except asyncio.CancelledError:
pass
app = FastAPI(title="Serinity", version="2.0.0", lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/static", StaticFiles(directory="static"), name="static")
app.include_router(auth_router)
app.include_router(router) |