mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 15:38:19 +08:00
fix(agent): bind runtime tasks to app lifespan (#6303)
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
import asyncio
|
||||
import threading
|
||||
|
||||
from app.agent import agent_manager
|
||||
from app.core.config import settings
|
||||
from app.log import logger
|
||||
@@ -51,37 +48,16 @@ class AgentInitializer:
|
||||
agent_initializer = AgentInitializer()
|
||||
|
||||
|
||||
def init_agent():
|
||||
async def init_agent() -> bool:
|
||||
"""
|
||||
初始化AI智能体(同步版本,用于在后台线程中运行)
|
||||
在应用事件循环中初始化AI智能体。
|
||||
"""
|
||||
try:
|
||||
if not settings.AI_AGENT_ENABLE:
|
||||
logger.info("AI智能体功能未启用")
|
||||
return True
|
||||
|
||||
# 在新的事件循环中初始化AI智能体管理器
|
||||
def run_init():
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
try:
|
||||
success = loop.run_until_complete(agent_initializer.initialize())
|
||||
if success:
|
||||
logger.info("AI智能体管理器初始化成功")
|
||||
else:
|
||||
logger.error("AI智能体管理器初始化失败")
|
||||
return success
|
||||
except Exception as err:
|
||||
logger.error(f"AI智能体管理器初始化失败: {err}")
|
||||
return False
|
||||
finally:
|
||||
loop.close()
|
||||
|
||||
# 在后台线程中初始化
|
||||
init_thread = threading.Thread(target=run_init, daemon=True)
|
||||
init_thread.start()
|
||||
|
||||
return True
|
||||
return await agent_initializer.initialize()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"初始化AI智能体时发生错误: {e}")
|
||||
|
||||
@@ -80,7 +80,7 @@ async def lifespan(app: FastAPI):
|
||||
# 初始化路由
|
||||
init_routers(app)
|
||||
# 初始化模块
|
||||
init_modules()
|
||||
await init_modules()
|
||||
if settings.MOVIEPILOT_SAFE_MODE:
|
||||
print("MoviePilot safe mode enabled: skip plugins, scheduler, monitor, commands and workflow.")
|
||||
else:
|
||||
|
||||
@@ -154,7 +154,7 @@ async def stop_modules():
|
||||
await run_step("临时文件", clear_temp)
|
||||
|
||||
|
||||
def init_modules():
|
||||
async def init_modules():
|
||||
"""
|
||||
启动模块
|
||||
"""
|
||||
@@ -178,7 +178,7 @@ def init_modules():
|
||||
MoviePilotServerHelper.get_user_uuid()
|
||||
MoviePilotServerHelper.get_github_user()
|
||||
# 初始化AI智能体
|
||||
init_agent()
|
||||
await init_agent()
|
||||
# 启动前端服务
|
||||
start_frontend()
|
||||
# 检查认证状态
|
||||
|
||||
Reference in New Issue
Block a user