d42965bba1
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / Build Production API Image (push) Has been skipped
CI/CD Pipeline / Build Production Web Image (push) Has been skipped
CI/CD Pipeline / Build Production Worker Image (push) Has been skipped
CI/CD Pipeline / Deploy Production (push) Has been skipped
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 3m0s
CI/CD Pipeline / Validate Code Quality And Tests (push) Successful in 3m32s
CI/CD Pipeline / Frontend Lint (push) Successful in 3m33s
CI/CD Pipeline / Unit Tests (push) Successful in 4m24s
CI/CD Pipeline / Integration Tests (push) Successful in 2m22s
CI/CD Pipeline / Build Staging Web Image (push) Failing after 9m32s
CI/CD Pipeline / Build Staging API Image (push) Successful in 14m17s
CI/CD Pipeline / Build Staging Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (push) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (push) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (push) Has been cancelled
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
147 lines
4.8 KiB
Python
Executable File
147 lines
4.8 KiB
Python
Executable File
"""视频合成 Celery 任务 — Phase 8 任务 2.10.
|
|
|
|
使用 JobService 管理任务生命周期,通过 RenderAdapter 调用 UnifiedRenderService 执行合成。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
from celery.utils.log import get_task_logger
|
|
from worker_app.celery_app import celery_app
|
|
from worker_app.db import SessionLocal
|
|
|
|
logger = get_task_logger(__name__)
|
|
|
|
|
|
def _get_job_service():
|
|
"""延迟导入 JobService,避免循环依赖。"""
|
|
from apps.api.app.services.job_service import JobService
|
|
from packages.adapters.sqlalchemy_impl.job_repository import SQLAlchemyJobRepository
|
|
|
|
db = SessionLocal()
|
|
repo = SQLAlchemyJobRepository(db)
|
|
return JobService(repo), db
|
|
|
|
|
|
@celery_app.task(
|
|
name="worker.compose_video",
|
|
bind=True,
|
|
max_retries=3,
|
|
default_retry_delay=60,
|
|
)
|
|
def compose_video(self, job_id: str, **kwargs):
|
|
"""视频合成任务。
|
|
|
|
使用 UnifiedRenderService(图层架构)进行渲染。
|
|
|
|
Args:
|
|
job_id: JobService 中的任务 ID
|
|
**kwargs: 来自 Job.payload 的额外参数(plan_id, output_path 等)
|
|
"""
|
|
job_service, db = _get_job_service()
|
|
|
|
try:
|
|
job = job_service.get_job(job_id)
|
|
if job is None:
|
|
logger.error("Job not found: %s", job_id)
|
|
return {"status": "error", "message": f"Job {job_id} not found"}
|
|
|
|
plan_id = job.payload.get("plan_id", "")
|
|
if not plan_id:
|
|
job_service.fail_job(job_id, "Missing plan_id in job payload")
|
|
return {"status": "error", "message": "Missing plan_id"}
|
|
|
|
# 使用 unified 渲染引擎
|
|
return _compose_with_unified_engine(self, job_service, job, plan_id, db)
|
|
|
|
except self.retry_exc as exc:
|
|
logger.warning("视频合成重试中: job_id=%s, exc=%s", job_id, exc)
|
|
raise
|
|
except Exception as exc:
|
|
logger.exception("视频合成异常: job_id=%s", job_id)
|
|
try:
|
|
job_service.fail_job(job_id, str(exc)[:500])
|
|
except Exception:
|
|
logger.exception("更新 Job 失败状态时出错")
|
|
raise self.retry(exc=exc, countdown=60) from exc
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def _compose_with_unified_engine(task, job_service, job, plan_id: str, db) -> dict:
|
|
"""新引擎渲染路径(UnifiedRenderService + RenderAdapter)。"""
|
|
job_id = job.id
|
|
|
|
# 标记为 running
|
|
job_service.update_progress(job_id, progress=10.0, current_stage="初始化统一渲染引擎")
|
|
|
|
from video_processing.render_adapter import RenderAdapter
|
|
|
|
adapter = RenderAdapter(db)
|
|
|
|
# 校验合成条件
|
|
job_service.update_progress(job_id, progress=15.0, current_stage="校验合成条件")
|
|
valid, errors, warnings, ready_count, total_count = adapter.validate_plan(plan_id)
|
|
if not valid:
|
|
error_msg = "; ".join(errors)
|
|
job_service.fail_job(job_id, f"合成校验失败: {error_msg}")
|
|
return {"status": "error", "message": error_msg}
|
|
|
|
# 进度回调
|
|
def progress_cb(progress: float, stage: str) -> None:
|
|
try:
|
|
job_service.update_progress(job_id, progress=progress, current_stage=stage)
|
|
except Exception:
|
|
logger.exception("更新进度失败")
|
|
|
|
# 执行渲染
|
|
job_service.update_progress(job_id, progress=20.0, current_stage="开始渲染")
|
|
logger.info("统一渲染引擎开始: job_id=%s plan_id=%s", job_id, plan_id)
|
|
|
|
result = adapter.render_plan(
|
|
plan_id=plan_id,
|
|
job_id=job_id,
|
|
progress_cb=progress_cb,
|
|
)
|
|
|
|
if not result.success:
|
|
job_service.fail_job(job_id, f"渲染失败: {result.error_message}")
|
|
raise RuntimeError(result.error_message)
|
|
|
|
# 更新 Job 状态为完成
|
|
result_data = {
|
|
"plan_id": plan_id,
|
|
"output_path": str(result.output_path) if result.output_path else "",
|
|
"storage_key": f"rendered/{plan_id}/{job_id}.mp4",
|
|
"output_url": result.output_url,
|
|
"estimated_duration": result.duration,
|
|
"clip_count": result.clip_count,
|
|
"engine": "unified",
|
|
"width": result.width,
|
|
"height": result.height,
|
|
"file_size": result.file_size,
|
|
}
|
|
job_service.complete_job(job_id, result=result_data)
|
|
|
|
logger.info(
|
|
"视频合成完成(unified): job_id=%s plan_id=%s duration=%.2fs",
|
|
job_id,
|
|
plan_id,
|
|
result.duration,
|
|
)
|
|
return {"status": "completed", "job_id": job_id, "result": result_data}
|
|
|
|
|
|
def _cleanup_output(job_id: str) -> None:
|
|
"""清理临时输出文件。"""
|
|
try:
|
|
_output_dir = os.environ.get("VIDEO_OUTPUT_DIR", os.path.join(tempfile.gettempdir(), "video_output"))
|
|
output_path = os.path.join(_output_dir, f"{job_id}.mp4")
|
|
if Path(output_path).exists():
|
|
Path(output_path).unlink()
|
|
except Exception as e:
|
|
logger.warning(f"清理输出文件失败: {e}", exc_info=True)
|