fix: TTS 合成接口失败时返回 201 + failed 状态,不抛 500
Deploy / Staging E2E Tests (push) Failing after 91h45m45s
Deploy / Deploy Staging (push) Failing after 91h47m59s
CI/CD Pipeline / Frontend Lint (push) Failing after 91h48m15s
CI/CD Pipeline / Validate Code Quality And Tests (push) Failing after 91h48m24s
Deploy / Production Browser E2E (push) Failing after 1750h23m19s
Deploy / Build Production Runtime Images (push) Failing after 1750h23m26s
Deploy / Deploy Production (push) Failing after 1750h54m47s

与音色克隆接口保持一致:
- 路由层增加 try/except 兜底,捕获 workflow 未处理的意外异常(DB错误等)
- CosyVoice 失败时标记 job 为 failed,返回 201 + failed 状态任务记录
- Celery 调度失败时也标记 job 为 failed,不抛 500
- 新增 10 个单元测试覆盖各种失败场景

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
灵应
2026-07-05 17:05:17 +08:00
parent ff660b1438
commit 1f8bbe37dd
2 changed files with 278 additions and 2 deletions
+20 -2
View File
@@ -2,6 +2,7 @@
from __future__ import annotations
import logging
from typing import Optional
from app.auth import AuthenticatedUser, get_current_user
@@ -30,6 +31,8 @@ from packages.application.tts_job.use_cases import (
)
from packages.application.tts_job.workflow import TTSWorkflowService
logger = logging.getLogger(__name__)
router = APIRouter()
@@ -74,6 +77,7 @@ def synthesize(
"""发起 TTS 合成任务。
创建 TTS 任务 → 提交 CosyVoice 合成 → 触发 Celery 异步轮询。
与音色克隆接口保持一致:CosyVoice 失败时不抛 500,而是返回 201 + failed 状态任务记录。
"""
user_id = authenticated_user.user.id
use_case = CreateTTSJobUseCase(repository)
@@ -91,7 +95,18 @@ def synthesize(
repository=repository,
cosyvoice_service=cosyvoice_service,
)
job = workflow.start_synthesis(job.id)
try:
job = workflow.start_synthesis(job.id)
except Exception as e:
# 兜底:workflow 内部已捕获 CosyVoiceError / ValueError,
# 但 DB 异常、网络异常等意外错误可能逃逸。
# 与音色克隆接口保持一致:标记 failed,返回 201,不抛 500。
logger.error(f"TTS 合成异常: job_id={job.id}, error={e}", exc_info=True)
try:
job = workflow.process_synthesis_failure(job.id, str(e))
except Exception as inner_e:
logger.error(f"标记 TTS job 失败时出错: job_id={job.id}, error={inner_e}")
# 若任务处于 processing 状态(异步模式),触发 Celery 后台轮询
if job.status.value == "processing":
@@ -103,7 +118,10 @@ def synthesize(
process_tts_synthesis.delay(job.id)
except Exception as e:
# Celery 调度失败,标记 job 为 failed
workflow.process_synthesis_failure(job.id, f"Celery 任务调度失败: {e}")
try:
workflow.process_synthesis_failure(job.id, f"Celery 任务调度失败: {e}")
except Exception as inner_e:
logger.error(f"Celery 调度后标记失败时出错: job_id={job.id}, error={inner_e}")
return TTSSynthesizeResponse(
job_id=job.id,