9aa450bb8b
CI/CD Pipeline / Validate Code Quality And Tests (push) Failing after 8s
CI/CD Pipeline / Frontend Lint (push) Successful in 1m9s
CI/CD Pipeline / Build & Push Staging (Watchtower auto-deploy) (push) Has been skipped
CI/CD Pipeline / Build Production Runtime Images (push) Has been skipped
CI/CD Pipeline / Staging E2E Tests (push) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (push) Has been skipped
CI/CD Pipeline / Deploy Production (push) Has been skipped
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
- 新增 app/core/task_enqueue.py,统一 safe_enqueue_generation_task 实现 - generation_tasks.py 和 task_center.py 改为 import 公共函数 - 修复 task_center.py 缺失 logging 导入的问题 - 日志前缀通过参数传入,保持各模块语义不变
59 lines
1.8 KiB
Python
Executable File
59 lines
1.8 KiB
Python
Executable File
import logging
|
|
from typing import Any
|
|
|
|
from app.core.celery_app import celery_app
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def safe_enqueue_generation_task(
|
|
task: Any,
|
|
generation_task_repository: Any,
|
|
*,
|
|
log_prefix: str = "[任务队列]",
|
|
log_task_status: bool = False,
|
|
) -> bool:
|
|
"""安全入队:send_task 失败时自动把任务标记为 failed,避免留下 pending 僵尸任务。
|
|
|
|
Args:
|
|
task: 生成任务对象,需有 id 属性和 mark_failed 方法
|
|
generation_task_repository: 任务仓储,用于更新状态
|
|
log_prefix: 日志前缀,便于区分调用来源
|
|
log_task_status: 成功日志中是否额外打印任务状态
|
|
|
|
Returns:
|
|
True 表示入队成功,False 表示入队失败(已标记为 failed)
|
|
"""
|
|
try:
|
|
celery_app.send_task("worker.generate_video", args=[task.id])
|
|
if log_task_status:
|
|
logger.info(
|
|
"%s 入队成功: task_id=%s, status=%s",
|
|
log_prefix,
|
|
task.id,
|
|
task.status,
|
|
)
|
|
else:
|
|
logger.info("%s 入队成功: task_id=%s", log_prefix, task.id)
|
|
return True
|
|
except Exception as e:
|
|
logger.error(
|
|
"%s 入队失败,标记为失败: task_id=%s error=%s",
|
|
log_prefix,
|
|
task.id,
|
|
e,
|
|
exc_info=True,
|
|
)
|
|
try:
|
|
task.mark_failed(f"任务入队失败: {e}")
|
|
generation_task_repository.update(task)
|
|
except Exception as update_err:
|
|
logger.error(
|
|
"%s 入队失败后更新状态也失败: task_id=%s error=%s",
|
|
log_prefix,
|
|
task.id,
|
|
update_err,
|
|
exc_info=True,
|
|
)
|
|
return False
|