Replace lifespan with on_event handlers for older FastAPI compat
The lifespan constructor parameter was added in FastAPI 0.93. When
running outside Docker with an older system-packaged FastAPI, the
parameter is silently ignored — the app starts and serves routes but
init_db() and start_presence() never run, so the bot never goes online
and webhooks crash with "Database not initialised".
on_event("startup")/on_event("shutdown") work across all FastAPI
versions.
https://claude.ai/code/session_019ANRdyL2jfi7ysWhqN4PfD
This commit is contained in:
31
app/main.py
31
app/main.py
@@ -1,5 +1,4 @@
|
|||||||
import logging
|
import logging
|
||||||
from contextlib import asynccontextmanager
|
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
@@ -13,18 +12,8 @@ logging.basicConfig(
|
|||||||
)
|
)
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
app = FastAPI(title="Melora")
|
||||||
@asynccontextmanager
|
app.include_router(webhook_router)
|
||||||
async def lifespan(app: FastAPI):
|
|
||||||
log.info("Starting Melora")
|
|
||||||
await init_db()
|
|
||||||
await _ensure_thread_roots()
|
|
||||||
await start_presence()
|
|
||||||
log.info("Melora ready")
|
|
||||||
yield
|
|
||||||
log.info("Shutting down Melora")
|
|
||||||
await close_client()
|
|
||||||
await close_db()
|
|
||||||
|
|
||||||
|
|
||||||
async def _ensure_thread_roots() -> None:
|
async def _ensure_thread_roots() -> None:
|
||||||
@@ -38,8 +27,20 @@ async def _ensure_thread_roots() -> None:
|
|||||||
await set_thread_root(media_type, event_id)
|
await set_thread_root(media_type, event_id)
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(title="Melora", lifespan=lifespan)
|
@app.on_event("startup")
|
||||||
app.include_router(webhook_router)
|
async def startup() -> None:
|
||||||
|
log.info("Starting Melora")
|
||||||
|
await init_db()
|
||||||
|
await _ensure_thread_roots()
|
||||||
|
await start_presence()
|
||||||
|
log.info("Melora ready")
|
||||||
|
|
||||||
|
|
||||||
|
@app.on_event("shutdown")
|
||||||
|
async def shutdown() -> None:
|
||||||
|
log.info("Shutting down Melora")
|
||||||
|
await close_client()
|
||||||
|
await close_db()
|
||||||
|
|
||||||
|
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
|
|||||||
Reference in New Issue
Block a user