利用 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(tasks): roll back state when scheduling fails
HaningZS17b7b3f524df
diff --git a/app/controllers/v1/video.py b/app/controllers/v1/video.pyindex b73cadff..70943445 100644--- a/app/controllers/v1/video.py+++ b/app/controllers/v1/video.py@@ -206,11 +206,19 @@ def create_task("params": body.model_dump(),}sm.state.update_task(task_id)- task_manager.add_task(tm.start, task_id=task_id, params=body, stop_at=stop_at)+ try:+ task_manager.add_task(+ tm.start, task_id=task_id, params=body, stop_at=stop_at+ )+ except Exception:+ # 状态记录在调度前创建,默认标记为 processing。如果调度器没能+ # 接管任务(例如线程启动失败或 Redis 队列不可用),必须回滚该+ # 记录,否则 API 和 WebUI 会永久展示一个实际从未运行的任务。+ sm.state.delete_task(task_id)+ raiselogger.success(f"Task created: {utils.to_json(task)}")return utils.get_response(200, task)except TaskQueueFullError as e:- sm.state.delete_task(task_id)logger.warning(f"reject task because queue is full, request_id: {request_id}, task_id: {task_id}")diff --git a/test/services/test_controller_video.py b/test/services/test_controller_video.pyindex e7c9982b..0e355064 100644--- a/test/services/test_controller_video.py+++ b/test/services/test_controller_video.py@@ -173,6 +173,30 @@ class TestVideoControllerTasks(unittest.TestCase):self.assertEqual(raised.exception.status_code, 429)delete_task.assert_called_once_with("task-123")+ def test_create_task_removes_state_when_scheduler_fails(self):+ """调度器未能接管任务时,不能留下永远处于 processing 的状态。"""+ body = MagicMock()+ body.model_dump.return_value = {"video_subject": "Coffee"}+ scheduling_error = RuntimeError("can't start new thread")+ state = sm.MemoryState()++ with (+ patch.object(video_controller.utils, "get_uuid", return_value="task-123"),+ patch.object(video_controller.sm, "state", state),+ patch.object(+ video_controller.task_manager,+ "add_task",+ side_effect=scheduling_error,+ ),+ ):+ with self.assertRaises(RuntimeError) as raised:+ video_controller.create_task(+ self._request(), body, stop_at="video"+ )++ self.assertIs(raised.exception, scheduling_error)+ self.assertIsNone(state.get_task("task-123"))+def test_get_all_tasks_preserves_pagination(self):"""任务列表响应必须包含状态层返回的总数和请求分页参数。"""with patch.object(