refactor: 收敛 factory 模块级副作用并统一 async 路径进度为异步后端

- 将 configure_token_codec/configure_plugin_routes 移入 create_app(),消除 import 期副作用
- runtime 层新增 AsyncCacheProxy/AsyncTTLCache/AsyncProgressHelper,共享 progress region
- search/system/history/scheduler/dashboard 的事件循环路径切换异步进度后端
- 同步进度回调经事件循环提交或线程池执行,避免阻塞事件循环
This commit is contained in:
jxxghp
2026-08-19 09:11:42 +08:00
parent 91d339a8dd
commit 03ada7eb01
9 changed files with 446 additions and 105 deletions
+9 -4
View File
@@ -2,6 +2,7 @@ from pathlib import Path
from typing import Any, List, Optional, Annotated
from fastapi import Depends
from fastapi.concurrency import run_in_threadpool
from app.schemas.dashboard import DashboardMemoryInfo as _SchemaDashboardMemoryInfo
from app.schemas.dashboard import DashboardSystemInfo as _SchemaDashboardSystemInfo
@@ -160,7 +161,8 @@ async def schedule(_: Any = Depends(get_current_active_superuser)) -> Any:
"""
查询后台服务信息
"""
return Scheduler().list()
# 同步 list() 内含同步进度读取,放到线程池执行避免阻塞事件循环
return await run_in_threadpool(Scheduler().list)
@router.get(
@@ -174,7 +176,8 @@ async def schedule_progress(
"""
查询指定后台服务的执行进度。
"""
progress = Scheduler().get_progress(job_id)
# 异步进度后端读取,避免同步 Redis 调用阻塞事件循环
progress = await Scheduler().aget_progress(job_id)
if not progress:
return _SchemaResponse(success=False, message="后台服务不存在")
return _SchemaResponse(success=True, data=progress.model_dump())
@@ -189,7 +192,8 @@ async def schedule2(_: Annotated[str, Depends(verify_apitoken)]) -> Any:
"""
查询下载器信息 API_TOKEN认证(?token=xxx
"""
return Scheduler().list()
# 同步 list() 内含同步进度读取,放到线程池执行避免阻塞事件循环
return await run_in_threadpool(Scheduler().list)
@router.get(
@@ -203,7 +207,8 @@ async def schedule_progress2(
"""
查询指定后台服务的执行进度 API_TOKEN认证(?token=xxx
"""
progress = Scheduler().get_progress(job_id)
# 异步进度后端读取,避免同步 Redis 调用阻塞事件循环
progress = await Scheduler().aget_progress(job_id)
if not progress:
return _SchemaResponse(success=False, message="后台服务不存在")
return _SchemaResponse(success=True, data=progress.model_dump())
+33 -23
View File
@@ -28,7 +28,7 @@ from app.api.deps import (
get_history_query_service,
get_transfer_history_mutation_command,
)
from app.runtime.progress import ProgressHelper
from app.runtime.progress import AsyncProgressHelper
from app.application.history import (
DownloadHistoryMutationCommand,
HistoryQueryService,
@@ -49,19 +49,24 @@ def normalize_history_ids(history_ids: list[int]) -> list[int]:
def _start_ai_redo_task(history_id: int, prompt: str, progress_key: str):
"""在后台线程中启动单条 AI 重新整理任务,并通过 ProgressHelper 实时更新进度。"""
progress = ProgressHelper(progress_key)
progress.start()
progress.update(
text=f"智能助手正在准备整理记录 #{history_id} ...",
data={"history_id": history_id, "success": True},
)
"""在后台任务中启动单条 AI 重新整理任务,并通过异步进度辅助类实时更新进度。"""
progress = AsyncProgressHelper(progress_key)
def update_output(text: str):
progress.update(text=text, data={"history_id": history_id})
# 输出回调由 agent 在事件循环上同步调用,不能直接 await;
# 提交到全局事件循环非阻塞执行,避免同步缓存后端阻塞事件循环。
asyncio.run_coroutine_threadsafe(
progress.update(text=text, data={"history_id": history_id}),
global_vars.loop,
)
async def runner():
try:
await progress.start()
await progress.update(
text=f"智能助手正在准备整理记录 #{history_id} ...",
data={"history_id": history_id, "success": True},
)
manager = get_running_agent_manager()
if manager is None:
logger.warning("智能助手服务未运行,跳过单条整理历史 AI 重做")
@@ -73,12 +78,12 @@ def _start_ai_redo_task(history_id: int, prompt: str, progress_key: str):
reply_mode=ReplyMode.CAPTURE_ONLY,
allow_message_tools=False,
)
progress.update(
await progress.update(
text="智能助手整理完成",
data={"history_id": history_id, "success": True, "completed": True},
)
except Exception as e:
progress.update(
await progress.update(
text=f"智能助手整理失败:{str(e)}",
data={
"history_id": history_id,
@@ -88,7 +93,7 @@ def _start_ai_redo_task(history_id: int, prompt: str, progress_key: str):
},
)
finally:
progress.end()
await progress.end()
asyncio.run_coroutine_threadsafe(runner(), global_vars.loop)
@@ -98,19 +103,24 @@ def _start_batch_ai_redo_task(
prompt: str,
progress_key: str,
):
"""在后台线程中启动批量 AI 重新整理任务,并通过 ProgressHelper 实时更新进度。"""
progress = ProgressHelper(progress_key)
progress.start()
progress.update(
text=f"智能助手正在准备批量整理 {len(history_ids)} 条记录 ...",
data={"history_ids": history_ids, "success": True},
)
"""在后台任务中启动批量 AI 重新整理任务,并通过异步进度辅助类实时更新进度。"""
progress = AsyncProgressHelper(progress_key)
def update_output(text: str):
progress.update(text=text, data={"history_ids": history_ids})
# 输出回调由 agent 在事件循环上同步调用,不能直接 await;
# 提交到全局事件循环非阻塞执行,避免同步缓存后端阻塞事件循环。
asyncio.run_coroutine_threadsafe(
progress.update(text=text, data={"history_ids": history_ids}),
global_vars.loop,
)
async def runner():
try:
await progress.start()
await progress.update(
text=f"智能助手正在准备批量整理 {len(history_ids)} 条记录 ...",
data={"history_ids": history_ids, "success": True},
)
manager = get_running_agent_manager()
if manager is None:
logger.warning("智能助手服务未运行,跳过批量整理历史 AI 重做")
@@ -122,12 +132,12 @@ def _start_batch_ai_redo_task(
reply_mode=ReplyMode.CAPTURE_ONLY,
allow_message_tools=False,
)
progress.update(
await progress.update(
text="智能助手批量整理完成",
data={"history_ids": history_ids, "success": True, "completed": True},
)
except Exception as e:
progress.update(
await progress.update(
text=f"智能助手批量整理失败:{str(e)}",
data={
"history_ids": history_ids,
@@ -137,7 +147,7 @@ def _start_batch_ai_redo_task(
},
)
finally:
progress.end()
await progress.end()
asyncio.run_coroutine_threadsafe(runner(), global_vars.loop)
+3 -3
View File
@@ -52,7 +52,7 @@ from app.adapters.external.market import (
split_plugin_market_repo_urls,
)
from app.application.messaging.message import MessageHelper
from app.runtime.progress import ProgressHelper
from app.runtime.progress import AsyncProgressHelper
from app.application.rules import RuleHelper
from app.adapters.external.server import MoviePilotServerHelper
from app.runtime.state import SystemHelper
@@ -847,7 +847,7 @@ async def get_progress(
"""
实时获取处理进度,返回格式为SSE
"""
progress = ProgressHelper(process_type)
progress = AsyncProgressHelper(process_type)
locale = LocaleHelper.get_current_locale()
async def event_generator():
@@ -855,7 +855,7 @@ async def get_progress(
while not global_vars.is_system_stopped:
if await request.is_disconnected():
break
detail = progress.get(locale=locale)
detail = await progress.get(locale=locale)
yield f"data: {json.dumps(detail)}\n\n"
await asyncio.sleep(0.5)
except asyncio.CancelledError: