fix(from-assets): 修复AI Code Review阻塞问题
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 3m2s
AI Code Review / AI Code Review (pull_request) Failing after 3m43s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 3m49s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 3m51s
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 3m55s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 4m7s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 1m22s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 2m6s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 5m59s
CI/CD Pipeline / Validate - Code Quality (pull_request) Has been cancelled
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
CI/CD Pipeline / PR Build Web Image (pull_request) Failing after 459h28m21s
CI/CD Pipeline / Frontend Lint (pull_request) Failing after 459h28m23s
CI/CD Pipeline / ACR Image Cleanup (pull_request) Failing after 459h31m18s
CI/CD Pipeline / Build Staging Worker Image (pull_request) Failing after 459h31m26s
CI/CD Pipeline / Build Staging Web Image (pull_request) Failing after 459h31m27s
CI/CD Pipeline / Staging API Integration Tests (pull_request) Failing after 459h31m20s
CI/CD Pipeline / Staging E2E Tests (pull_request) Failing after 459h31m22s
CI/CD Pipeline / Build Staging API Image (pull_request) Failing after 459h31m27s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Failing after 460h2m23s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Failing after 460h5m24s

1. 复用全局SessionLocal替代build_session_factory,避免每次创建新连接池
2. 分批获取所有片段替代硬编码limit=500,防止长模板截断
3. 初始化used_segments包含所有片段的当前随机时间,确保冲突检测正确
This commit is contained in:
CI Bot
2026-08-28 11:54:17 +08:00
parent a461d8c25c
commit a496c6da04
@@ -690,16 +690,16 @@ def _update_mediakit_recommendations_async( # pragma: no cover
此函数在后台异步执行,不影响接口响应时间。
失败时静默处理,不影响已创建的片段。
"""
from app.config import settings
from packages.adapters.sqlalchemy_impl.asset_repository import SQLAlchemyAssetRepository
from packages.adapters.sqlalchemy_impl.session import build_session_factory
from packages.adapters.sqlalchemy_impl.session import SessionLocal
db = None
try:
# 创建独立的数据库 session(不复用请求 session)
_, session_factory = build_session_factory(settings.DATABASE_URL)
db = session_factory()
# 复用应用全局 Session(避免每次创建新连接池导致资源泄漏)
if SessionLocal is None:
logger.warning("后台任务: SessionLocal 未初始化,跳过 MediaKit 更新")
return
db = SessionLocal()
# 初始化服务
asset_repo = SQLAlchemyAssetRepository(db)
@@ -711,14 +711,32 @@ def _update_mediakit_recommendations_async( # pragma: no cover
logger.info("后台任务: MediaKit 无推荐结果,跳过更新")
return
# 查询该 plan 的所有片段
clips = plan_svc.list_clips(plan_id, skip=0, limit=500)
# 查询该 plan 的所有片段(分批获取,避免硬编码 limit 截断)
batch_size = 500
all_clips = []
offset = 0
while True:
batch = plan_svc.list_clips(plan_id, skip=offset, limit=batch_size)
if not batch:
break
all_clips.extend(batch)
if len(batch) < batch_size:
break
offset += batch_size
clips = all_clips
if not clips:
logger.info("后台任务: plan_id=%s 无片段,跳过更新", plan_id)
return
# 按 asset_id 分组,跟踪已使用时间段
# 按 asset_id 分组,初始化已使用时间段(包含所有片段的当前随机时间)
used_segments: dict[str, list[tuple[float, float]]] = {}
for clip in clips:
aid = getattr(clip, "asset_id", "") or ""
if aid and clip.start_time is not None:
used_segments.setdefault(aid, []).append(
(clip.start_time, clip.start_time + clip.duration)
)
# 遍历片段,按 asset_id 匹配推荐时间
for clip in clips: