Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/services/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ def is_task_busy(task: dict | None) -> bool:
try:
state = int(state)
except (TypeError, ValueError):
pass
# The function's own docstring promises to treat unparseable
# states as busy, so the delete API keeps a task that we
# cannot reason about. Returning False here would let
# delete_video() cancel a task that is still running or
# cross-posting whenever an older Redis snapshot or a
# legacy call site stored the state as a string.
return True

# 视频生成和跨平台发布都可能继续读取任务目录。统一视为忙碌状态,
# 可以避免 API 与 WebUI 分别维护规则后出现一个允许删除、另一个禁止
Expand Down
71 changes: 71 additions & 0 deletions test/services/test_is_task_busy_string_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import unittest
import sys
from pathlib import Path

# add project root to python path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))

from app.services import task as tm


class TestIsTaskBusyDefensiveDefault(unittest.TestCase):
"""is_task_busy() must keep tasks whose state is unparseable."""

def setUp(self):
# The registry is process-wide; clear it so cross-post futures
# left behind by earlier tests cannot affect the assertions.
with tm._cross_post_registry_lock:
tm._cross_post_futures.clear()

def tearDown(self):
with tm._cross_post_registry_lock:
tm._cross_post_futures.clear()

def test_string_state_is_treated_as_busy(self):
# Older Redis snapshots can surface the state as a string. The
# delete endpoint must not be allowed to cancel a task whose
# state cannot be parsed.
self.assertTrue(tm.is_task_busy({"state": "processing"}))
self.assertTrue(tm.is_task_busy({"state": "4"}))
self.assertTrue(tm.is_task_busy({"state": "PROCESSING"}))

def test_none_state_is_treated_as_busy(self):
# A missing or None state still implies a task we cannot reason
# about, so the conservative default must apply.
self.assertTrue(tm.is_task_busy({"state": None}))
self.assertTrue(tm.is_task_busy({"state": "not-an-int"}))

def test_string_state_with_idle_cross_post_still_busy(self):
# The previous code path returned False here, which let the
# delete API silently cancel in-flight tasks.
self.assertTrue(
tm.is_task_busy(
{
"state": "processing",
"cross_post_state": tm.const.CROSS_POST_STATE_COMPLETE,
}
)
)

def test_integer_processing_state_remains_busy(self):
# Existing happy path stays unchanged.
self.assertTrue(tm.is_task_busy({"state": tm.const.TASK_STATE_PROCESSING}))

def test_integer_complete_with_idle_cross_post_remains_idle(self):
# Existing happy path stays unchanged.
self.assertFalse(
tm.is_task_busy(
{
"state": tm.const.TASK_STATE_COMPLETE,
"cross_post_state": tm.const.CROSS_POST_STATE_COMPLETE,
}
)
)

def test_none_task_remains_idle(self):
# The existing None guard at the top of is_task_busy still wins.
self.assertFalse(tm.is_task_busy(None))


if __name__ == "__main__":
unittest.main()
Loading