Browse Source

异步恢复中断的流程

jihuaqiang 4 days ago
parent
commit
ee5b8ac8b6
1 changed files with 10 additions and 1 deletions
  1. 10 1
      agent.py

+ 10 - 1
agent.py

@@ -113,12 +113,21 @@ async def lifespan(app: FastAPI):
     identify_tool = IdentifyTool()
     
     # 启动后恢复中断的流程
-    await restore_interrupted_processes()
+    # 异步恢复中断流程,避免阻塞启动
+    app.state.restore_task = asyncio.create_task(restore_interrupted_processes())
     
     yield
     
     # 关闭时执行
     logger.info("🛑 关闭 Knowledge Agent 服务...")
+    # 优雅取消后台恢复任务
+    restore_task = getattr(app.state, 'restore_task', None)
+    if restore_task and not restore_task.done():
+        restore_task.cancel()
+        try:
+            await restore_task
+        except asyncio.CancelledError:
+            logger.info("✅ 已取消后台恢复任务")
 
 async def restore_interrupted_processes():
     """