| 1234567891011121314151617181920212223242526272829 |
- from contextlib import asynccontextmanager
- from fastapi import FastAPI
- from core.agent_manager import agent_manager
- from utils.registration_manager import registration_manager
- from utils.logger import chat_logger
- @asynccontextmanager
- async def lifespan(app: FastAPI):
- """应用生命周期管理"""
- try:
- # 启动时检查注册状态,但不阻止启动
- registration_status = await registration_manager.check_registration()
- if not registration_status:
- chat_logger.warning("⚠️ 服务启动:注册检查未通过,/chat接口将受限")
- else:
- chat_logger.info("✅ 服务启动:注册检查通过")
- await agent_manager.initialize()
- chat_logger.info("🚀🚀 AI助手服务启动")
- yield
- finally:
- cleared_count = await agent_manager.shutdown()
- chat_logger.info(f"🛑🛑🛑 AI助手服务停止,清理了 {cleared_count} 个Agent实例")
- def create_lifespan():
- """创建生命周期管理器"""
- return lifespan
|