利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(queue): coerce the configured task limits before scheduling
c0206273c9873014c77
diff --git a/app/controllers/manager/base_manager.py b/app/controllers/manager/base_manager.pyindex ab214643..6ceb7a70 100644--- a/app/controllers/manager/base_manager.py+++ b/app/controllers/manager/base_manager.py@@ -1,3 +1,4 @@+import mathimport threadingfrom typing import Any, Callable, Dict@@ -8,10 +9,41 @@ class TaskQueueFullError(ValueError):pass+def _coerce_task_limit(value: Any, name: str) -> int:+ """+ 把配置里的并发 / 排队上限解析成整数。++ TOML 的数值既可能写成 `max_concurrent_tasks = 5`,也可能被写成 `"5"`。字符串+ 会一路传到 `add_task` 的比较运算里,变成不指出配置键名的 TypeError,而且+ `max_queued_tasks` 写错时只在并发名额用尽后才触发。`app/services/webui_task.py`+ 对同一个 `max_queued_tasks` 键已经做了同样的收敛,这里把它提到唯一的构造入口。++ 解析不出整数的写法一律报出配置键名:`0.5` 会被 `int()` 静默截断成 0(任务只入队、+ 没有 worker 执行),TOML 允许的 `inf` / `-inf` / `nan` 也不是有效上限。+ 0 与负数保持原样:前者表示暂时不执行任务,后者让请求直接进入排队分支。+ """+ if isinstance(value, bool):+ # bool 是 int 的子类,但 `true` 显然不是用户想要的上限。+ raise ValueError(f"{name} must be an integer, got {value!r}")+ if isinstance(value, float):+ # 非有限值不是上限,小数则会在截断后退化成「0:只排队不执行」的语义。+ if not math.isfinite(value):+ raise ValueError(f"{name} must be a finite integer, got {value!r}")+ if not value.is_integer():+ raise ValueError(f"{name} must be an integer, got {value!r}")+ return int(value)+ try:+ return int(value)+ except (TypeError, ValueError, OverflowError):+ raise ValueError(f"{name} must be an integer, got {value!r}") from None++class TaskManager:def __init__(self, max_concurrent_tasks: int, max_queued_tasks: int = 100):- self.max_concurrent_tasks = max_concurrent_tasks- self.max_queued_tasks = max_queued_tasks+ self.max_concurrent_tasks = _coerce_task_limit(+ max_concurrent_tasks, "max_concurrent_tasks"+ )+ self.max_queued_tasks = _coerce_task_limit(max_queued_tasks, "max_queued_tasks")self.current_tasks = 0self.lock = threading.Lock()self.queue = self.create_queue()diff --git a/test/services/test_task_manager.py b/test/services/test_task_manager.pyindex 3ec25594..01a2f552 100644--- a/test/services/test_task_manager.py+++ b/test/services/test_task_manager.py@@ -138,6 +138,68 @@ class TestInMemoryTaskManager(unittest.TestCase):)fake_thread.start.assert_called_once_with()+ def test_limits_accept_quoted_toml_integers(self):+ """+ TOML 里把上限写成字符串时也要按整数比较;修复前并发名额的比较会抛+ TypeError,队列上限的比较则要等到名额用尽才触发。+ """+ manager = InMemoryTaskManager(max_concurrent_tasks="1", max_queued_tasks="1")++ with patch.object(manager, "execute_task") as execute_task:+ manager.add_task(len, [1])+ manager.add_task(len, [2])+ with self.assertRaises(TaskQueueFullError):+ manager.add_task(len, [3])++ self.assertEqual(manager.max_concurrent_tasks, 1)+ self.assertEqual(manager.max_queued_tasks, 1)+ execute_task.assert_called_once_with(len, [1])+ self.assertEqual(manager.queue_size(), 1)++ def test_invalid_limits_raise_a_named_error(self):+ """无法解析的上限要立刻指出配置键名,不能推迟成调度期的匿名异常。"""+ with self.assertRaisesRegex(ValueError, "max_concurrent_tasks"):+ InMemoryTaskManager(max_concurrent_tasks="abc")++ with self.assertRaisesRegex(ValueError, "max_queued_tasks"):+ InMemoryTaskManager(max_concurrent_tasks=1, max_queued_tasks="abc")++ with self.assertRaisesRegex(ValueError, "max_queued_tasks"):+ InMemoryTaskManager(max_concurrent_tasks=1, max_queued_tasks=True)++ def test_non_integral_limits_are_rejected_by_name(self):+ """修复前 `0.5` 会被 `int()` 截断成 0(任务入队后没有 worker 执行),`inf` 则漏出 OverflowError。"""+ cases = (+ (0.5, "an integer"),+ (1.5, "an integer"),+ ("0.5", "an integer"),+ (float("nan"), "a finite"),+ (float("inf"), "a finite"),+ (float("-inf"), "a finite"),+ )+ for field in ("max_concurrent_tasks", "max_queued_tasks"):+ for value, msg in cases:+ with self.subTest(field=field, value=value):+ limits = {"max_concurrent_tasks": 1, "max_queued_tasks": 1}+ limits[field] = value+ pattern = f"{field} must be {msg}"+ with self.assertRaisesRegex(ValueError, pattern):+ InMemoryTaskManager(**limits)++ # 没有截断风险的整数值浮点仍然可用(TOML 允许 `max_queued_tasks = 2.0`)。+ integral = {"max_concurrent_tasks": 1, "max_queued_tasks": 2.0}+ self.assertEqual(InMemoryTaskManager(**integral).max_queued_tasks, 2)++ def test_zero_concurrency_keeps_queueing_without_executing(self):+ """0 与负数仍是合法取值:只排队、不执行,既有用例依赖这一语义。"""+ manager = InMemoryTaskManager(max_concurrent_tasks=0, max_queued_tasks=1)++ with patch.object(manager, "execute_task") as execute_task:+ manager.add_task(len, [1])++ execute_task.assert_not_called()+ self.assertEqual(manager.queue_size(), 1)+class TestRedisTaskManager(unittest.TestCase):def setUp(self):