df99305dd6
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 1s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 2s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 29s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 29s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 49s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 1m39s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m44s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 2m19s
AI Code Review / AI Code Review (pull_request) Failing after 2m52s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 2m54s
CI/CD Pipeline / Validate - Security (pull_request) Successful in 4m11s
CI/CD Pipeline / Unit Tests (pull_request) Failing after 6m23s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Failing after 1s
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 3m39s
问题:素材转码与视频生成共用 celery 默认队列、worker 单进程消费, 20+ 转码积压会把用户生成任务堵 40 分钟以上;孤儿清理把任务标 failed 后 Redis 队列消息未作废,消息被重投导致 failed→running 非法转换, worker 打印 ERROR 后继续产出半成品。 队列隔离: - 新增 packages/shared/celery_queues.py:generation/transcode/celery 三队列与 task_routes(generate_video→generation;ingest_asset/ classify_asset/duplication→transcode),apply_queue_settings() - worker 入口改双进程:generation worker 独占队列并内嵌 beat (prefetch=1, GENERATION_CONCURRENCY 默认 2),transcode worker 消费 transcode,celery(并发=总-2,最小 1),任一退出则整体终止 - compose/部署脚本/ps1 同步新增 GENERATION_CONCURRENCY 与健康检查 消息作废: - 新增 packages/shared/celery_orphan_guard.py:终态守卫 ensure_task_claimable、Redis 队列消息物理清理(JSON 信封解析, 按业务 id + celery headers.id 双匹配,未命中 rpush 保序)、 revoke_and_purge(control.revoke + 物理清队列双保险) - 入队点(生成/上传/分片/重试)send_task 后持久化 celery_task_id 到 generation_tasks/ingest_jobs(新列,067 迁移,失败仅 warning) - generate_video/ingest_asset 执行前校验 DB 状态:终态直接 discarded 不进业务逻辑;mark_processing 返回 False(非法转换)安全中止 - 孤儿/超时清理标 failed 时同时 revoke + 清队列消息 - pending 超时阈值 15→45 分钟,与 running 孤儿(20min)区分 测试:新增 22 个单测(路由表/真实 Redis 消息清理/终态守卫/ 非法转换中止/标 failed 后消息不重投/入队持久化),全量 14301 passed;067 迁移隔离 DDL 验证 upgrade/downgrade 通过。
72 lines
2.9 KiB
Python
72 lines
2.9 KiB
Python
"""定期清理任务 — Celery Beat 调度。
|
||
|
||
包含:
|
||
- cleanup_stale_pending_tasks: 定期清理卡在 pending 超时的 generation_tasks(worker 停止消费时占位)
|
||
- cleanup_stale_running_tasks: 定期清理卡在 running 超时的 generation_tasks(容器重启/进程被杀后的孤儿)
|
||
"""
|
||
|
||
import logging
|
||
|
||
from celery import shared_task
|
||
from worker_app.tasks._startup import (
|
||
ORPHAN_TASK_TIMEOUT_MINUTES,
|
||
PENDING_TASK_TIMEOUT_MINUTES,
|
||
cleanup_orphan_tasks,
|
||
cleanup_stale_jobs,
|
||
cleanup_stale_pending_tasks,
|
||
)
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
@shared_task(name="worker.cleanup_stale_pending_tasks")
|
||
def scheduled_cleanup_stale_pending(timeout_minutes: int = PENDING_TASK_TIMEOUT_MINUTES) -> dict:
|
||
"""Celery Beat 调度的定期任务:清理超时的 pending 任务。
|
||
|
||
每 5 分钟执行一次(由 celery_app.py 的 beat_schedule 配置),
|
||
查找所有 status='pending' 且 created_at < NOW() - timeout_minutes
|
||
的 generation_tasks,批量更新为 failed,释放限流名额;同时 revoke 并清除
|
||
Redis 队列中对应的 Celery 消息,杜绝作废消息重投执行(#1714)。
|
||
|
||
Args:
|
||
timeout_minutes: 超时时间(分钟),默认 45 分钟(pending 排队阈值放宽,
|
||
与 running 孤儿 20 分钟区分,避免正常排队任务被误杀)
|
||
|
||
Returns:
|
||
{"cleaned": int}
|
||
"""
|
||
count = cleanup_stale_pending_tasks(timeout_minutes)
|
||
if count > 0:
|
||
logger.info("[Beat] 清理了 %d 个超时 pending 任务(超时阈值 %d 分钟)", count, timeout_minutes)
|
||
return {"cleaned": count}
|
||
|
||
|
||
@shared_task(name="worker.cleanup_stale_running_tasks")
|
||
def scheduled_cleanup_stale_running(timeout_minutes: int = ORPHAN_TASK_TIMEOUT_MINUTES) -> dict:
|
||
"""Celery Beat 调度的定期任务:清理超时的 running 孤儿任务。
|
||
|
||
每 5 分钟执行一次。worker_ready 信号只在 worker 启动时清一次,
|
||
若 worker 没重启但任务卡死(上传挂起、进程 OOM 被内核杀掉等),
|
||
任务会永久卡在 running 占位。此任务做持续兜底:
|
||
查找 status='running' 且 updated_at < NOW() - timeout_minutes 的任务,
|
||
标记为 failed(原因:容器重启/超时中断),同时清理 Job 表孤儿。
|
||
|
||
Args:
|
||
timeout_minutes: 超时时间(分钟),默认 20 分钟
|
||
(worker.generate_video 硬超时 11 分钟,正常任务不可能超过 20 分钟)
|
||
|
||
Returns:
|
||
{"generation_tasks": int, "jobs": int}
|
||
"""
|
||
gen_count = cleanup_orphan_tasks(timeout_minutes)
|
||
job_count = cleanup_stale_jobs(timeout_minutes)
|
||
total = gen_count + job_count
|
||
if total > 0:
|
||
logger.warning(
|
||
"[Beat] 清理孤儿任务: running GenerationTask=%d, Job=%d(超时阈值 %d 分钟)",
|
||
gen_count,
|
||
job_count,
|
||
timeout_minutes,
|
||
)
|
||
return {"generation_tasks": gen_count, "jobs": job_count}
|