fix: 生成任务入队失败时标记为failed,避免pending僵尸任务 #214
Reference in New Issue
Block a user
Delete Branch "fix/generation-task-enqueue-failure"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
问题
一键生成任务创建后,DB 有记录但 Celery 队列里没有,任务一直卡在 pending 状态。
根因
任务创建(DB commit)和任务入队(Celery send_task)是两个独立操作,没有事务保证。当 send_task 失败时(如 Redis 连接波动、broker 不可用),任务已经 commit 到 DB 但永远不会被执行。
批量创建场景更严重:count > 1 时,前面的任务可能成功入队,中间某个失败后留下 pending 僵尸任务。
修复内容
_safe_enqueue_generation_task安全入队函数generation_tasks.py批量创建generation_tasks.py单任务重试task_center.py用户级重试task_center.py项目级重试验证
相关测试 130/130 全部通过。
PR #214 审查结论:✅ 通过(安全入队部分)
审查范围
聚焦本次核心改动:任务安全入队修复。PR 还包含统一渲染、编辑模式、CosyVoice、结构化日志等其他功能,本次不展开。
核心改动验证
1. 安全入队函数
_safe_enqueue_generation_taskcelery_app.send_task外层 try/except,失败时调用task.mark_failed()+repository.update()2. 四处入口覆盖 ✅
generation_tasks.pycreate_generation_taskfor 循环内generation_tasks.pyretry_generation_tasktask_center.pyretry_task_by_idtask_center.pyretry_project_task3. 批量创建的事务边界 ✅
代码质量
_safe_enqueue_generation_task在generation_tasks.py和task_center.py重复定义,可抽成公共函数(非阻塞,后续重构可做)测试
test_oneclick_gen_p0_fixes.py等新增测试覆盖