From 4bb01fa31556096b33730a8caaae1912e1b9f356 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Sat, 1 Aug 2026 16:36:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(#1197):=20=E4=BF=AE=E5=A4=8DAI=20Code=20Rev?= =?UTF-8?q?iew=E9=98=BB=E5=A1=9E=E7=BA=A7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 预览分辨率按video_ratio动态计算(支持9:16竖屏/1:1/4:3等) 2. 入队失败时标记任务为failed,避免僵尸pending数据 --- apps/api/app/api/routes/generation_preview.py | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/apps/api/app/api/routes/generation_preview.py b/apps/api/app/api/routes/generation_preview.py index 2867b084c..bd843f31c 100755 --- a/apps/api/app/api/routes/generation_preview.py +++ b/apps/api/app/api/routes/generation_preview.py @@ -39,6 +39,30 @@ router = APIRouter() PREVIEW_RESOLUTION = "854x480" +def _calc_preview_resolution(video_ratio: str = "") -> str: + """根据视频比例计算预览分辨率(短边 480,长边按比例)。 + + 支持的比例:16:9, 9:16, 1:1, 4:3, 3:4, 其他默认 16:9。 + """ + ratio_map = { + "16:9": "854x480", + "9:16": "480x854", + "1:1": "480x480", + "4:3": "640x480", + "3:4": "480x640", + } + return ratio_map.get(video_ratio.strip(), PREVIEW_RESOLUTION) + + +def _mark_task_failed(repo, task, reason: str) -> None: + """入队失败时将任务标记为 failed,避免产生僵尸 pending 数据。""" + try: + task.mark_failed(error_message=f"入队失败:{reason}") + repo.update(task) + except Exception: + logger.exception("[预览生成] 标记任务失败时异常: task_id=%s", task.id) + + def _to_preview_response(task, generated_videos: list | None = None) -> PreviewGenerationTaskResponse: """将领域任务对象转换为预览响应 DTO。 @@ -152,7 +176,7 @@ def create_preview_generation_task( asset_select_mode="", batch_id="", video_title=request.video_title, - resolution=PREVIEW_RESOLUTION, + resolution=_calc_preview_resolution(request.video_ratio), bgm_config=request.bgm_config or {}, auto_retry_enabled=False, auto_retry_max=0, @@ -166,7 +190,7 @@ def create_preview_generation_task( logger.error("[预览生成] 创建失败: %s", e, exc_info=True) raise HTTPException(status_code=500, detail="创建预览生成任务失败,请稍后重试") from e - # 入队执行 + # 入队执行;若入队失败则标记任务为 failed 避免僵尸数据 try: if not safe_enqueue_generation_task( task, @@ -176,13 +200,16 @@ def create_preview_generation_task( log_task_status=True, ): logger.warning("[预览生成] 任务入队失败: task_id=%s", task.id) + _mark_task_failed(generation_task_repository, task, "任务入队失败") raise HTTPException(status_code=500, detail="任务入队失败,请稍后重试") except UserPendingLimitExceeded: + _mark_task_failed(generation_task_repository, task, "待处理任务超限") raise HTTPException( status_code=429, detail="您的待处理任务过多,请等待完成后再提交", ) from None except GlobalQueueFull: + _mark_task_failed(generation_task_repository, task, "系统队列已满") raise HTTPException( status_code=503, detail="系统繁忙,请稍后再试",