e539105256
Deploy / Build Production Runtime Images (push) Has been skipped
Deploy / Deploy Production (push) Has been skipped
Deploy / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Failing after 163h53m1s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 163h53m6s
Deploy / Deploy Staging (push) Failing after 164h23m43s
CI/CD Pipeline / Frontend Lint (push) Failing after 164h23m43s
CI/CD Pipeline / Validate Code Quality And Tests (push) Failing after 164h23m50s
P1-1 (阻塞性): TTS 合成完整异步链路 - CosyVoiceService 新增 submit_synthesize_task() + poll_synthesize_task() - 新建 TTSWorkflowService 编排层 (packages/application/tts_job/workflow.py) - 新建 Celery 任务 process_tts_synthesis (apps/worker/worker_app/tasks/tts_synthesis.py) - 注册到 celery_app.conf.imports + tasks/__init__.py 懒加载 - TTS 路由 synthesize() 增加 CosyVoice 提交 + Celery 调度 P2-1: voice_clone.py 添加详细 Celery 重试策略注释 P2-2: 修复 voice_clone.py Session 泄漏 (session=None 安全模式) P2-3: ListVoiceLibraryUseCase 返回 (items, count) 元组,消除重复 count_by_user() P2-4: 新增 find_profile_ids_by_voice_ids() 批量方法,填充 voice_clone_profile_id 测试: 749 passed, 0 failed
109 lines
3.5 KiB
Python
109 lines
3.5 KiB
Python
"""TTS synthesis tasks - process TTS synthesis requests via CosyVoice API."""
|
|
|
|
import logging
|
|
|
|
from celery import Task
|
|
from celery.exceptions import Retry
|
|
from worker_app.celery_app import celery_app
|
|
from worker_app.db import SessionLocal
|
|
|
|
from packages.adapters.sqlalchemy_impl.tts_job_repository import (
|
|
SQLAlchemyTTSJobRepository,
|
|
)
|
|
from packages.application.cosyvoice_service import (
|
|
CosyVoiceError,
|
|
CosyVoiceService,
|
|
CosyVoiceTimeoutError,
|
|
)
|
|
from packages.application.tts_job.workflow import TTSWorkflowService
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@celery_app.task(bind=True, max_retries=3, name="worker.process_tts_synthesis")
|
|
def process_tts_synthesis(self: Task, job_id: str) -> dict:
|
|
"""处理 TTS 合成任务。
|
|
|
|
通过 TTSWorkflowService.poll_and_process_synthesis() 轮询 CosyVoice
|
|
合成任务状态,更新 TTSJob。
|
|
超时自动重试(最多 3 次),其他错误标记 job 为 failed。
|
|
|
|
Args:
|
|
job_id: TTSJob ID
|
|
|
|
Returns:
|
|
dict: {"ok": True, "job_id": str, "audio_url": str} 或
|
|
{"ok": False, "job_id": str, "error": str}
|
|
"""
|
|
session = None
|
|
try:
|
|
session = SessionLocal()
|
|
repo = SQLAlchemyTTSJobRepository(session)
|
|
workflow = TTSWorkflowService(
|
|
repository=repo, cosyvoice_service=CosyVoiceService(),
|
|
)
|
|
|
|
updated_job = workflow.poll_and_process_synthesis(job_id, timeout=120)
|
|
session.commit()
|
|
|
|
logger.info(
|
|
f"TTS synthesis completed: job_id={job_id}, "
|
|
f"audio_url={updated_job.output_audio_url}"
|
|
)
|
|
return {
|
|
"ok": True,
|
|
"job_id": job_id,
|
|
"audio_url": updated_job.output_audio_url,
|
|
}
|
|
|
|
except Retry:
|
|
# Celery Retry 异常必须向上传播,不能被后续 except 捕获
|
|
raise
|
|
|
|
except CosyVoiceTimeoutError as e:
|
|
logger.warning(f"TTS synthesis timeout for {job_id}: {e}")
|
|
if session is not None:
|
|
session.rollback()
|
|
# 超时重试,指数退避
|
|
raise self.retry(exc=e, countdown=30)
|
|
|
|
except CosyVoiceError as e:
|
|
logger.error(f"TTS synthesis failed for {job_id}: {e}")
|
|
if session is not None:
|
|
session.rollback()
|
|
# 标记 job 为 failed
|
|
try:
|
|
if session is not None:
|
|
job = repo.get(job_id)
|
|
if job is not None:
|
|
job.mark_failed(str(e))
|
|
repo.update(job)
|
|
session.commit()
|
|
except Exception as inner_e:
|
|
logger.error(f"Failed to mark job as failed: {inner_e}")
|
|
if session is not None:
|
|
session.rollback()
|
|
return {"ok": False, "job_id": job_id, "error": str(e)}
|
|
|
|
except Exception as e:
|
|
logger.error(f"TTS synthesis unexpected error for {job_id}: {e}")
|
|
if session is not None:
|
|
session.rollback()
|
|
# 标记 job 为 failed
|
|
try:
|
|
if session is not None:
|
|
job = repo.get(job_id)
|
|
if job is not None:
|
|
job.mark_failed(str(e))
|
|
repo.update(job)
|
|
session.commit()
|
|
except Exception as inner_e:
|
|
logger.error(f"Failed to mark job as failed: {inner_e}")
|
|
if session is not None:
|
|
session.rollback()
|
|
return {"ok": False, "job_id": job_id, "error": str(e)}
|
|
|
|
finally:
|
|
if session is not None:
|
|
session.close()
|