(app: FastAPI)
| 687 | |
| 688 | @asynccontextmanager |
| 689 | async def lifespan(app: FastAPI): |
| 690 | logger.info("正在初始化应用...") |
| 691 | # 初始化数据库 |
| 692 | await init_db() |
| 693 | |
| 694 | # 加载配置(多进程下串行化启动写操作) |
| 695 | async with db_startup_lock(): |
| 696 | await load_config() |
| 697 | # 启动后台任务 |
| 698 | task = asyncio.create_task(delete_expire_files()) |
| 699 | chunk_cleanup_task = asyncio.create_task(clean_incomplete_uploads()) |
| 700 | logger.info("应用初始化完成") |
| 701 | |
| 702 | try: |
| 703 | yield |
| 704 | finally: |
| 705 | # 清理操作 |
| 706 | logger.info("正在关闭应用...") |
| 707 | task.cancel() |
| 708 | chunk_cleanup_task.cancel() |
| 709 | await asyncio.gather(task, chunk_cleanup_task, return_exceptions=True) |
| 710 | await Tortoise.close_connections() |
| 711 | logger.info("应用已关闭") |
| 712 | |
| 713 | |
| 714 | async def load_config(): |
nothing calls this directly
no test coverage detected