23fe5f9822
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
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 2s
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (push) Successful in 1s
CI/CD Pipeline / Validate - Style (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Validate - Security (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 3s
CI/CD Pipeline / Frontend Lint (push) Has been skipped
CI/CD Pipeline / PR Build API Image (push) Has been skipped
CI/CD Pipeline / PR Build Web Image (push) Has been skipped
CI/CD Pipeline / Check push changed paths (push) Successful in 10s
CI/CD Pipeline / PR Build Worker Image (push) Has been skipped
CI/CD Pipeline / Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Integration Tests (pull_request) Has been skipped
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) Successful in 56s
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 / PR Build API Image (pull_request) Successful in 2m37s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 48s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m19s
CI/CD Pipeline / Build Production 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 / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
PR Automation / Auto Merge on CI Green + Approved (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 2m3s
CI/CD Pipeline / CI Gate (pull_request) Successful in 2s
CI/CD Pipeline / Validate - Python (mypy + alembic) (push) Successful in 3m58s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 54s
CI/CD Pipeline / Integration Tests (push) Successful in 4m1s
CI/CD Pipeline / Validate - Style (push) Successful in 4m29s
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 5m27s
CI/CD Pipeline / Staging E2E Tests (push) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (push) Has been cancelled
CI/CD Pipeline / Build Production API Image (push) Has been cancelled
CI/CD Pipeline / Build Production Web Image (push) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Production (push) Has been cancelled
CI/CD Pipeline / Build Staging API Image (push) Has been cancelled
CI/CD Pipeline / Retag skipped Staging API Image (push) Has been cancelled
CI/CD Pipeline / Retag skipped Staging Web Image (push) Has been cancelled
CI/CD Pipeline / Retag skipped Staging Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Has been cancelled
CI/CD Pipeline / Production Browser E2E (push) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (push) Has been cancelled
CI/CD Pipeline / Canary Release to Production (push) Has been cancelled
CI/CD Pipeline / CI Gate (push) Has been cancelled
CI/CD Pipeline / Validate - Security (push) Has been cancelled
CI/CD Pipeline / Unit Tests (push) Has been cancelled
AI Code Review / AI Code Review (pull_request) Has been cancelled
Preview Deploy / Deploy Preview Environment (pull_request) Has been cancelled
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
120 lines
5.1 KiB
Python
120 lines
5.1 KiB
Python
"""视频生成任务 finalize 服务(#2024)。
|
||
|
||
Worker 渲染+上传完成后不再自动入库,标记为 awaiting_cover;用户在 Step5 选好封面
|
||
点「完成」时由 API 调用本服务:创建 GeneratedVideo 成品库记录(复用 worker 预计算
|
||
的查重结果)、绑定封面、推进任务到 completed。
|
||
|
||
与 AI 数字人 ``ai_avatar_render_service.finalize_job`` 模式一致,
|
||
只是走 GenerationTask 而非 AiAvatarRenderJob。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
from datetime import UTC, datetime
|
||
from typing import Optional
|
||
|
||
from sqlalchemy.orm import Session
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
class GenerationFinalizeError(Exception):
|
||
"""finalize 业务错误,code 供 API 层映射 HTTP 状态码。"""
|
||
|
||
def __init__(self, message: str, code: str = "FinalizeError", status_code: int = 400):
|
||
super().__init__(message)
|
||
self.code = code
|
||
self.status_code = status_code
|
||
|
||
|
||
class GenerationFinalizeService:
|
||
def __init__(self, db: Session):
|
||
self.db = db
|
||
|
||
def finalize_task(self, task_id: str, user_id: str, cover_url: Optional[str] = None):
|
||
"""执行 finalize:状态校验 → 幂等 → 绑定封面 → 入库 → 推进 completed。
|
||
|
||
Returns:
|
||
GeneratedVideo 领域对象
|
||
"""
|
||
from packages.adapters.sqlalchemy_impl.generated_video_repository import (
|
||
SQLAlchemyGeneratedVideoRepository,
|
||
)
|
||
from packages.adapters.sqlalchemy_impl.generation_task_repository import (
|
||
SQLAlchemyGenerationTaskRepository,
|
||
)
|
||
from packages.adapters.sqlalchemy_impl.models import GeneratedVideoModel
|
||
from packages.application.generated_video_finalize import finalize_generated_video
|
||
|
||
task_repo = SQLAlchemyGenerationTaskRepository(self.db)
|
||
video_repo = SQLAlchemyGeneratedVideoRepository(self.db)
|
||
|
||
task = task_repo.get(task_id)
|
||
if task is None:
|
||
raise GenerationFinalizeError(f"任务 {task_id} 不存在", "TaskNotFound", 404)
|
||
|
||
# ── 幂等:已入库直接返回 ─────────────────────────────────
|
||
existing = self.db.query(GeneratedVideoModel).filter(GeneratedVideoModel.generation_task_id == task_id).first()
|
||
if existing is not None:
|
||
logger.info("[finalize] 幂等命中 task=%s video=%s", task_id, existing.id)
|
||
if cover_url and cover_url.strip() and existing.thumbnail_url != cover_url.strip():
|
||
existing.thumbnail_url = cover_url.strip()
|
||
task.cover_url = cover_url.strip()
|
||
self.db.commit()
|
||
if task.status.value != "completed":
|
||
try:
|
||
task.mark_completed(result_count=1)
|
||
if cover_url and cover_url.strip():
|
||
task.cover_url = cover_url.strip()
|
||
task_repo.update(task)
|
||
self.db.commit()
|
||
except Exception as e:
|
||
logger.warning("[finalize] 幂等补 mark_completed 失败: %s", e)
|
||
self.db.rollback()
|
||
return video_repo.get(existing.id)
|
||
|
||
# ── 状态校验 ─────────────────────────────────────────────
|
||
if task.status.value != "awaiting_cover":
|
||
raise GenerationFinalizeError(
|
||
f"任务当前状态 {task.status.value},无法 finalize(需 awaiting_cover)",
|
||
"InvalidTaskStatus",
|
||
400,
|
||
)
|
||
|
||
# ── 封面 ─────────────────────────────────────────────────
|
||
effective_cover = (cover_url or "").strip() if cover_url else (task.cover_url or "").strip()
|
||
|
||
# ── 入库+查重(复用 worker 预计算结果) ──────────────────
|
||
try:
|
||
result = finalize_generated_video(
|
||
task=task,
|
||
session=self.db,
|
||
effective_cover_url=effective_cover,
|
||
)
|
||
except ValueError as e:
|
||
raise GenerationFinalizeError(str(e), "RenderedOutputMissing", 400) from e
|
||
|
||
video_id = result["video_id"]
|
||
|
||
# ── 推进任务 ─────────────────────────────────────────────
|
||
task.mark_completed(result_count=1)
|
||
task.cover_url = effective_cover
|
||
# 清理 rendered_output(体积较大,入库后不再需要)
|
||
meta = dict(task.extra_meta or {})
|
||
meta.pop("rendered_output", None)
|
||
task.extra_meta = meta
|
||
task.updated_at = datetime.now(UTC)
|
||
task_repo.update(task)
|
||
self.db.commit()
|
||
|
||
video = video_repo.get(video_id)
|
||
logger.info(
|
||
"[finalize] task=%s finalized -> video=%s cover=%s dup=%s",
|
||
task_id,
|
||
video_id,
|
||
bool(effective_cover),
|
||
result.get("is_duplicate", False),
|
||
)
|
||
return video
|