提交
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
"""FastAPI 入口。数据库结构统一由 Alembic 管理。"""
|
||||
from contextlib import asynccontextmanager
|
||||
import asyncio
|
||||
from contextlib import asynccontextmanager, suppress
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from sqlalchemy import text
|
||||
|
||||
from . import cache
|
||||
from . import cache, scheduler
|
||||
from .api import router
|
||||
from .auth_api import router as auth_router
|
||||
from .config import settings
|
||||
@@ -16,7 +17,11 @@ from .db import engine
|
||||
async def lifespan(app: FastAPI):
|
||||
async with engine.connect() as conn:
|
||||
await conn.execute(text("SELECT 1"))
|
||||
nightly = asyncio.create_task(scheduler.run_nightly_loop())
|
||||
yield
|
||||
nightly.cancel()
|
||||
with suppress(asyncio.CancelledError):
|
||||
await nightly
|
||||
await engine.dispose()
|
||||
await cache.aclose() # 释放 Redis 连接池(未启用时是 no-op)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user