Files
xiaoxia-saas/apps/api/app/services/generation_finalize_service.py
T
saas-backend-agent 48f6f49f7b
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m22s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 2m32s
AI Code Review / AI Code Review (pull_request) Successful in 6m51s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 10m53s
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 0s
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 Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web 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 30s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 29s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 3m46s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 4m59s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 5m33s
CI/CD Pipeline / Validate - Security (pull_request) Successful in 10m57s
CI/CD Pipeline / Unit Tests (pull_request) Failing after 11m4s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web 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
feat(generation): #2024 defer video finalization until cover confirmed
Two-phase pipeline: worker renders and precomputes dedup metadata, but does
not create GeneratedVideo until user confirms cover in Step 5.

Domain
- Add GenerationTaskStatus.AWAITING_COVER (non-terminal)
- Add mark_awaiting_cover(): progress=100, clears error, leaves completed_at unset
- Transitions: running → {awaiting_cover, completed, failed, cancelled};
  awaiting_cover → {completed, failed, cancelled}
- _missing_ aliases: waiting_cover/video_ready/rendered/pending_cover
- Keep running→completed for backward compat / legacy paths

Worker (apps/worker/worker_app/tasks/generation.py)
- Replace _record_video_and_dedup with _precompute_render_metadata:
  calls compute_render_fingerprint_and_dedup, returns dict without DB writes
- Remove runtime batch-rerender decision (should_rerender_for_batch_dedup path);
  dedup now happens at finalize time against all finished records
- Persist precomputed metadata into task.extra_meta['rendered_output']
- Final status: mark_awaiting_cover instead of mark_completed

Dedup helpers (apps/worker/video_processing/dedup_helpers.py)
- New compute_render_fingerprint_and_dedup(video_path,...): local fingerprint
  + historical dedup + batch dedup, returns fully serializable dict
  (fingerprint_dict, fingerprint_chunks list, is_duplicate, duplicate_of, etc.)
- create_video_record_and_dedup() retained for compat/tests; now supports
  pre_dedup_result to reuse worker-precomputed data without re-reading video
- VideoFingerprint.from_dict() added to reconstruct from serialized form

Application layer (packages/application/generated_video_finalize.py)
- RenderedOutput dataclass + from_dict() for deserializing worker output
- finalize_generated_video(): creates GeneratedVideo, bulk-inserts
  VideoFingerprintChunk rows, commits; API-layer free

API service (apps/api/app/services/generation_finalize_service.py)
- GenerationFinalizeService.finalize_task(task_id, user_id, cover_url):
  * permission / existence check
  * idempotent: if GeneratedVideo already exists for this task, just ensure
    task is marked completed and return (safe for double-click)
  * status gate: only awaiting_cover (or legacy completed) accepted
  * cover resolution: explicit cover_url arg > task.cover_url
  * delegates to finalize_generated_video, marks task completed,
    clears extra_meta['rendered_output']

API endpoint (apps/api/app/api/routes/generation_tasks.py)
- POST /api/v1/generation/tasks/{task_id}/finalize
- Body: { cover_url?: string }; returns video_id/cover_url/file_url/is_duplicate
- Adjust confirm_generation fast path: mark_confirmed keeps task in
  awaiting_cover (don't auto-complete); historical 'completed' previews
  migrated back to awaiting_cover
- Preview-reuse accepts awaiting_cover tasks

Preview / task center
- GET /preview/{task_id} constructs lightweight _PreviewVideo from
  extra_meta.rendered_output when task is awaiting_cover
- Task center step map: awaiting_cover → 等待确认封面; status filter includes it

Tests
- tests/unit/test_finalize_generation.py: 13 new tests covering status
  transitions, mark_awaiting_cover, RenderedOutput parsing, finalize use case
  (success / missing metadata / cover fallback)
- test_generation_task.py updated for 6th status value
- Full suite: 15978 passed, 28 skipped (matches #2023 baseline, no regressions)
- black/isort/ruff clean

Out of scope (intentionally untouched)
- AI avatar pipeline uses separate AiAvatarRenderJob; finalize_job and
  /{job_id}/finalize are unchanged
- PR #2023 subtitle font scaling files (ass_subtitle_builder,
  video_filter_builder, subtitle_generator) not modified
- No DB migration: GenerationTaskModel.status is String(20) without CHECK
- Temp file cleanup: leave to existing periodic job
2026-09-24 11:29:36 +08:00

120 lines
5.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""视频生成任务 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