refactor: register history ai redo tasks

This commit is contained in:
jxxghp
2026-08-23 15:02:53 +08:00
parent 0459b6a662
commit e7e232d625
4 changed files with 74 additions and 9 deletions
+3 -2
View File
@@ -13,8 +13,8 @@
"runtime_to_db": [],
"workflow_to_db": []
},
"edge_count": 6470,
"edge_sha256": "571b66b75b51e801a053e4164c5dbe0eb580b91f20cf84fa554e13b9469dd650",
"edge_count": 6471,
"edge_sha256": "30a14c4218ffd8e2798ab93231e7fa0d4ed3e368a5f1cdf75b09336e863e0a50",
"edges": [
"app -> app.runtime",
"app -> app.runtime.compat",
@@ -1836,6 +1836,7 @@
"app.api.endpoints.history -> app.runtime.config",
"app.api.endpoints.history -> app.runtime.log",
"app.api.endpoints.history -> app.runtime.progress",
"app.api.endpoints.history -> app.runtime.tasks",
"app.api.endpoints.history -> app.schemas",
"app.api.endpoints.history -> app.schemas.common",
"app.api.endpoints.history -> app.schemas.history",
+44 -1
View File
@@ -3,7 +3,7 @@
import asyncio
from types import SimpleNamespace
from app.api.endpoints import message, site, subscribe, webhook
from app.api.endpoints import history, message, site, subscribe, webhook
from app.runtime.tasks import TaskRegistry
@@ -19,6 +19,17 @@ class _TaskRegistry(TaskRegistry):
"""保存函数、参数和 owner。"""
self.calls.append((function, args, kwargs, owner))
def create(
self,
coroutine,
*,
owner: str,
cancel_on_shutdown: bool = True,
) -> None:
"""保存异步任务登记参数,并关闭未执行的 coroutine。"""
coroutine.close()
self.calls.append((None, (), {"cancel_on_shutdown": cancel_on_shutdown}, owner))
class _WebhookRequest:
"""提供 webhook 端点读取的最小请求接口。"""
@@ -139,3 +150,35 @@ def test_seerr_subscribe_uses_task_registry(monkeypatch) -> None:
"username": "tester",
}
assert owner == "api.subscribe.seerr"
def test_history_ai_redo_uses_task_registry() -> None:
"""单条历史 AI 重做应登记宿主任务并使用稳定 owner。"""
registry = _TaskRegistry()
history._start_ai_redo_task(
history_id=7,
prompt="整理记录",
progress_key="progress-7",
task_registry=registry,
)
assert registry.calls == [
(None, (), {"cancel_on_shutdown": True}, "api.history.ai_redo")
]
def test_history_batch_ai_redo_uses_task_registry() -> None:
"""批量历史 AI 重做应登记宿主任务并区分批量 owner。"""
registry = _TaskRegistry()
history._start_batch_ai_redo_task(
history_ids=[7, 8],
prompt="批量整理",
progress_key="progress-batch",
task_registry=registry,
)
assert registry.calls == [
(None, (), {"cancel_on_shutdown": True}, "api.history.ai_redo_batch")
]