Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(webui): refresh active task state beyond first page

Rudy Celeklif530d0b1b5fe
diff --git a/test/services/test_webui_task.py b/test/services/test_webui_task.py
index aeb32cb1..816fce17 100644
--- a/test/services/test_webui_task.py
+++ b/test/services/test_webui_task.py
@@ -140,6 +140,102 @@ def test_webui_runtime_config_updates_do_not_use_blocking_writes():
assert direct_writes == []
+def test_active_task_uses_terminal_state_when_outside_runtime_page(tmp_path):
+ """An active session marker must not hide a finished task past page one."""
+ tree = ast.parse(WEBUI_MAIN.read_text(encoding="utf-8"))
+ function = next(
+ node
+ for node in tree.body
+ if isinstance(node, ast.FunctionDef)
+ and node.name == "_collect_task_summaries"
+ )
+ namespace = {
+ "_scan_history_tasks": lambda limit: [],
+ "_active_generation_tasks": lambda: {
+ "new-task": {"subject": "Latest video", "mtime": 1000}
+ },
+ "_task_state_filter_key": lambda task: (
+ "failed" if task["state"] == const.TASK_STATE_FAILED else "processing"
+ ),
+ "sm": SimpleNamespace(
+ state=SimpleNamespace(
+ get_all_tasks=lambda page, page_size: (
+ [
+ {
+ "task_id": f"old-{index}",
+ "state": const.TASK_STATE_COMPLETE,
+ }
+ for index in range(50)
+ ],
+ 51,
+ ),
+ get_task=MagicMock(
+ return_value={
+ "task_id": "new-task",
+ "state": const.TASK_STATE_FAILED,
+ "progress": 70,
+ }
+ ),
+ )
+ ),
+ "utils": SimpleNamespace(task_dir=lambda: str(tmp_path)),
+ "os": os,
+ "const": const,
+ "logger": MagicMock(),
+ }
+ module = ast.fix_missing_locations(ast.Module(body=[function], type_ignores=[]))
+ exec(compile(module, str(WEBUI_MAIN), "exec"), namespace)
+
+ tasks = namespace["_collect_task_summaries"](limit=20)
+
+ latest = next(task for task in tasks if task["task_id"] == "new-task")
+ assert latest["state"] == const.TASK_STATE_FAILED
+ assert latest["progress"] == 70
+ assert latest["mtime"] == 1000
+ namespace["sm"].state.get_task.assert_called_once_with("new-task")
+
+
+def test_task_summary_tolerates_directory_removed_during_scan():
+ """A concurrent deletion between isdir and stat must not crash the panel."""
+ tree = ast.parse(WEBUI_MAIN.read_text(encoding="utf-8"))
+ function = next(
+ node
+ for node in tree.body
+ if isinstance(node, ast.FunctionDef)
+ and node.name == "_collect_task_summaries"
+ )
+ getmtime = MagicMock(side_effect=FileNotFoundError("task directory removed"))
+ namespace = {
+ "_scan_history_tasks": lambda limit: [],
+ "_active_generation_tasks": lambda: {},
+ "sm": SimpleNamespace(
+ state=SimpleNamespace(
+ get_all_tasks=lambda page, page_size: (
+ [{"task_id": "removed-task", "state": const.TASK_STATE_COMPLETE}],
+ 1,
+ )
+ )
+ ),
+ "utils": SimpleNamespace(task_dir=lambda: "/tasks"),
+ "os": SimpleNamespace(
+ path=SimpleNamespace(
+ join=os.path.join,
+ isdir=lambda _path: True,
+ getmtime=getmtime,
+ )
+ ),
+ "logger": MagicMock(),
+ }
+ module = ast.fix_missing_locations(ast.Module(body=[function], type_ignores=[]))
+ exec(compile(module, str(WEBUI_MAIN), "exec"), namespace)
+
+ tasks = namespace["_collect_task_summaries"]()
+
+ assert tasks[0]["task_id"] == "removed-task"
+ assert tasks[0]["mtime"] == 0
+ getmtime.assert_called_once_with(os.path.join("/tasks", "removed-task"))
+
+
@pytest.mark.parametrize(
("ui_config", "expected_open_count"),
[
diff --git a/webui/Main.py b/webui/Main.py
index 68a76825..935ad43a 100644
--- a/webui/Main.py
+++ b/webui/Main.py
@@ -1027,6 +1027,7 @@ def _scan_history_tasks(limit=30):
def _collect_task_summaries(limit=20):
history_tasks = {task["task_id"]: task for task in _scan_history_tasks(limit=50)}
+ active_tasks = _active_generation_tasks()
try:
runtime_tasks, _ = sm.state.get_all_tasks(1, 50)
@@ -1034,6 +1035,21 @@ def _collect_task_summaries(limit=20):
logger.warning(f"failed to load runtime tasks: {e}")
runtime_tasks = []
+ # The paginated state view can omit this session's newer tasks after 50
+ # older records. Read those active IDs directly so a completed or failed
+ # task cannot remain labelled as processing forever.
+ runtime_ids = {task.get("task_id") for task in runtime_tasks}
+ for task_id in active_tasks:
+ if task_id in runtime_ids:
+ continue
+ try:
+ task = sm.state.get_task(task_id)
+ except Exception as e:
+ logger.warning(f"failed to load active task {task_id}: {e}")
+ continue
+ if task:
+ runtime_tasks.append(task)
+
for task in runtime_tasks:
task_id = task.get("task_id", "")
if not task_id:
@@ -1051,6 +1067,16 @@ def _collect_task_summaries(limit=20):
or (task.get("script", "")[:40] if task.get("script") else "")
or task_id
)
+ task_mtime = active_tasks.get(task_id, {}).get("mtime") or history_task.get(
+ "mtime", 0
+ )
+ if os.path.isdir(task_path):
+ try:
+ task_mtime = os.path.getmtime(task_path)
+ except OSError:
+ # Another session can delete this directory between isdir and
+ # getmtime. Keep rendering the persisted task state.
+ pass
history_tasks[task_id] = {
"task_id": task_id,
@@ -1058,15 +1084,13 @@ def _collect_task_summaries(limit=20):
"state": task.get("state"),
"cross_post_state": task.get("cross_post_state"),
"progress": int(task.get("progress", 0) or 0),
- "mtime": os.path.getmtime(task_path)
- if os.path.isdir(task_path)
- else history_task.get("mtime", 0),
+ "mtime": task_mtime,
"task_path": task_path,
"video_file": video_file,
"source": "runtime",
}
- for task_id, active_task in _active_generation_tasks().items():
+ for task_id, active_task in active_tasks.items():
history_task = history_tasks.get(task_id, {})
if history_task and _task_state_filter_key(history_task) in {
"complete",