8a396303b8
CI/CD Pipeline / Check if frontend-only change (push) Has been skipped
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (push) Successful in 1s
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 3s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 4s
CI/CD Pipeline / Frontend Lint (push) Has been skipped
CI/CD Pipeline / PR Build API Image (push) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (push) Has been skipped
CI/CD Pipeline / PR Build Worker Image (push) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Check push changed paths (push) Successful in 10s
CI/CD Pipeline / Validate - Style (pull_request) Has been skipped
CI/CD Pipeline / Validate - Security (pull_request) Has been skipped
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) 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 / 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 / Integration Tests (push) Successful in 3m12s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m25s
CI/CD Pipeline / Validate - Python (mypy + alembic) (push) Successful in 3m39s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 3m11s
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
PR Automation / Auto Merge on CI Green + Approved (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (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
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 3m46s
CI/CD Pipeline / Build Staging API Image (push) Successful in 3m32s
CI/CD Pipeline / Validate - Style (push) Successful in 4m16s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 4m30s
CI/CD Pipeline / Build Staging Worker Image (push) Successful in 1m14s
CI/CD Pipeline / Build Staging Web Image (push) Successful in 2m24s
CI/CD Pipeline / Retag skipped Staging API Image (push) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (push) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (push) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Successful in 50s
CI/CD Pipeline / Frontend Unit Tests (push) Successful in 6m43s
AI Code Review / AI Code Review (pull_request) Successful in 7m14s
CI/CD Pipeline / Validate - Security (push) Successful in 7m47s
CI/CD Pipeline / ACR Image Cleanup (push) Successful in 1m47s
CI/CD Pipeline / Staging API Integration Tests (push) Successful in 3m0s
CI/CD Pipeline / Staging E2E Tests (push) Failing after 4m6s
CI/CD Pipeline / Unit Tests (push) Successful in 12m7s
CI/CD Pipeline / Build Production API Image (push) Has been skipped
CI/CD Pipeline / Build Production Web Image (push) Has been skipped
CI/CD Pipeline / CI Gate (push) Has been skipped
CI/CD Pipeline / Build Production Worker Image (push) Has been skipped
CI/CD Pipeline / Deploy Production (push) Has been skipped
CI/CD Pipeline / Canary Release to Production (push) Has been skipped
CI/CD Pipeline / Production Browser E2E (push) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Failing after 12m11s
CI/CD Pipeline / CI Gate (pull_request) Failing after 1s
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
192 lines
7.1 KiB
Python
192 lines
7.1 KiB
Python
"""SQLAlchemy implementation of VoiceCloneProfileRepository."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from packages.adapters.sqlalchemy_impl.models import VoiceCloneProfileModel
|
|
from packages.domain.voice_clone_profile import VoiceCloneProfile, VoiceCloneStatus
|
|
|
|
|
|
class SQLAlchemyVoiceCloneProfileRepository:
|
|
"""SQLAlchemy 音色克隆档案仓储。"""
|
|
|
|
def __init__(self, session: Session) -> None:
|
|
self.session = session
|
|
|
|
def create(self, profile: VoiceCloneProfile) -> VoiceCloneProfile:
|
|
model = VoiceCloneProfileModel(
|
|
id=profile.id,
|
|
user_id=profile.user_id,
|
|
name=profile.name,
|
|
description=profile.description,
|
|
source_audio_url=profile.source_audio_url,
|
|
voice_id=profile.voice_id,
|
|
voice_model=profile.voice_model,
|
|
language=profile.language,
|
|
gender=profile.gender,
|
|
status=profile.status,
|
|
error_message=profile.error_message,
|
|
retry_count=profile.retry_count,
|
|
max_retries=profile.max_retries,
|
|
metadata_=profile.metadata,
|
|
)
|
|
self.session.add(model)
|
|
self.session.commit()
|
|
self.session.refresh(model)
|
|
return self._model_to_entity(model)
|
|
|
|
def get(self, profile_id: str) -> Optional[VoiceCloneProfile]:
|
|
model = (
|
|
self.session.query(VoiceCloneProfileModel)
|
|
.filter(
|
|
VoiceCloneProfileModel.id == profile_id,
|
|
VoiceCloneProfileModel.status != "deleted",
|
|
)
|
|
.first()
|
|
)
|
|
if model is None:
|
|
return None
|
|
return self._model_to_entity(model)
|
|
|
|
def update(self, profile: VoiceCloneProfile) -> VoiceCloneProfile:
|
|
model = self.session.query(VoiceCloneProfileModel).filter(VoiceCloneProfileModel.id == profile.id).first()
|
|
if model is None:
|
|
raise ValueError(f"VoiceCloneProfile {profile.id} not found")
|
|
model.name = profile.name
|
|
model.description = profile.description
|
|
model.source_audio_url = profile.source_audio_url
|
|
model.voice_id = profile.voice_id
|
|
model.voice_model = profile.voice_model
|
|
model.language = profile.language
|
|
model.gender = profile.gender
|
|
model.status = profile.status
|
|
model.error_message = profile.error_message
|
|
model.retry_count = profile.retry_count
|
|
model.max_retries = profile.max_retries
|
|
model.metadata_ = profile.metadata
|
|
self.session.commit()
|
|
self.session.refresh(model)
|
|
return self._model_to_entity(model)
|
|
|
|
def delete(self, profile_id: str) -> bool:
|
|
model = self.session.query(VoiceCloneProfileModel).filter(VoiceCloneProfileModel.id == profile_id).first()
|
|
if model is None:
|
|
return False
|
|
model.status = "deleted"
|
|
self.session.commit()
|
|
return True
|
|
|
|
def list_by_user(
|
|
self,
|
|
user_id: str,
|
|
*,
|
|
status: Optional[str] = None,
|
|
limit: int = 50,
|
|
offset: int = 0,
|
|
) -> list[VoiceCloneProfile]:
|
|
query = self.session.query(VoiceCloneProfileModel).filter(
|
|
VoiceCloneProfileModel.user_id == user_id,
|
|
VoiceCloneProfileModel.status != "deleted",
|
|
)
|
|
if status:
|
|
query = query.filter(VoiceCloneProfileModel.status == status)
|
|
query = query.order_by(VoiceCloneProfileModel.created_at.desc())
|
|
models = query.offset(offset).limit(limit).all()
|
|
return [self._model_to_entity(m) for m in models]
|
|
|
|
def count_by_user(self, user_id: str, *, status: Optional[str] = None) -> int:
|
|
query = self.session.query(VoiceCloneProfileModel).filter(
|
|
VoiceCloneProfileModel.user_id == user_id,
|
|
VoiceCloneProfileModel.status != "deleted",
|
|
)
|
|
if status:
|
|
query = query.filter(VoiceCloneProfileModel.status == status)
|
|
return query.count()
|
|
|
|
def find_by_voice_id(self, voice_id: str) -> Optional[VoiceCloneProfile]:
|
|
model = (
|
|
self.session.query(VoiceCloneProfileModel)
|
|
.filter(
|
|
VoiceCloneProfileModel.voice_id == voice_id,
|
|
VoiceCloneProfileModel.status != "deleted",
|
|
)
|
|
.first()
|
|
)
|
|
if model is None:
|
|
return None
|
|
return self._model_to_entity(model)
|
|
|
|
def find_profile_ids_by_voice_ids(self, voice_ids: list[str]) -> dict[str, str]:
|
|
"""批量查询 voice_id → profile_id 映射。用于填充统一列表的 voice_clone_profile_id。"""
|
|
if not voice_ids:
|
|
return {}
|
|
rows = (
|
|
self.session.query(
|
|
VoiceCloneProfileModel.voice_id,
|
|
VoiceCloneProfileModel.id,
|
|
)
|
|
.filter(
|
|
VoiceCloneProfileModel.voice_id.in_(voice_ids),
|
|
VoiceCloneProfileModel.status != "deleted",
|
|
)
|
|
.all()
|
|
)
|
|
return {voice_id: profile_id for voice_id, profile_id in rows}
|
|
|
|
def cleanup_stale_processing(self, timeout_minutes: int = 10) -> int:
|
|
"""清理超时卡在 processing 的克隆档案。
|
|
|
|
worker 重启、Celery 任务丢失或 OOM 被杀时,processing 档案会永久卡住。
|
|
updated_at < NOW() - timeout_minutes 的 processing 记录,标记为 failed
|
|
并附带明确错误信息,用户可在前端点击「重试」。
|
|
|
|
Args:
|
|
timeout_minutes: 超时分钟数,默认 10 分钟(正常克隆 < 5 分钟)
|
|
|
|
Returns:
|
|
清理的记录数
|
|
"""
|
|
from datetime import UTC, datetime, timedelta
|
|
|
|
cutoff = datetime.now(UTC) - timedelta(minutes=timeout_minutes)
|
|
models = (
|
|
self.session.query(VoiceCloneProfileModel)
|
|
.filter(
|
|
VoiceCloneProfileModel.status == "processing",
|
|
VoiceCloneProfileModel.updated_at < cutoff,
|
|
)
|
|
.all()
|
|
)
|
|
count = 0
|
|
for model in models:
|
|
model.status = "failed"
|
|
model.error_message = f"克隆任务执行超时(超过 {timeout_minutes} 分钟未更新,可能因服务重启中断),请重试"
|
|
count += 1
|
|
if count > 0:
|
|
self.session.commit()
|
|
return count
|
|
|
|
@staticmethod
|
|
def _model_to_entity(model: VoiceCloneProfileModel) -> VoiceCloneProfile:
|
|
return VoiceCloneProfile(
|
|
id=model.id,
|
|
user_id=model.user_id,
|
|
name=model.name,
|
|
description=model.description or "",
|
|
source_audio_url=model.source_audio_url or "",
|
|
voice_id=model.voice_id or "",
|
|
voice_model=model.voice_model or "",
|
|
language=model.language or "zh-CN",
|
|
gender=model.gender or "unknown",
|
|
status=VoiceCloneStatus(model.status) if model.status else VoiceCloneStatus.PENDING,
|
|
error_message=model.error_message or "",
|
|
retry_count=model.retry_count or 0,
|
|
max_retries=model.max_retries or 3,
|
|
metadata=model.metadata_ or {},
|
|
created_at=model.created_at,
|
|
updated_at=model.updated_at,
|
|
)
|