f42fe18269
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 2s
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 2s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m38s
CI/CD Pipeline / Build Staging API Image (pull_request) 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 / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (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 2m55s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m7s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 3m24s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 3m28s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 3m54s
CI/CD Pipeline / Validate - Style (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 / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Security (pull_request) Has been cancelled
AI Code Review / AI Code Review (pull_request) Has been cancelled
PR Automation / Auto Merge on CI Green + Approved (pull_request) Has been cancelled
- 新增 test_gpu_lipsync_routes.py:register 透传 cancel_task、cancel 接受 processing - gpu_lipsync_service 测试:心跳检测取消、cancelled 不回退 pending、wait_for_result 终态 - lipsync_service 测试(真实 SQLite):cancel_job 同步取消 GPU 任务、非 gpu 前缀不动 GPU 表 - celery 测试:cancelled GPU 任务不回退 MediaKit - worker 测试:TaskHeartbeat 检测 cancel 调 /cancel、取消上报不重试 - 修复 build_engine 给 SQLite 传 pool_size/max_overflow/pool_timeout 导致 TypeError
104 lines
3.2 KiB
Python
104 lines
3.2 KiB
Python
from __future__ import annotations
|
||
|
||
from sqlalchemy import create_engine, text
|
||
from sqlalchemy.engine import URL, make_url
|
||
from sqlalchemy.orm import sessionmaker
|
||
|
||
from packages.adapters.sqlalchemy_impl.models import Base
|
||
|
||
SCHEMA_INIT_LOCK_ID = 2026061501
|
||
SessionLocal = None
|
||
|
||
|
||
def build_engine(
|
||
database_url: str,
|
||
*,
|
||
pool_size: int = 20,
|
||
max_overflow: int = 40,
|
||
pool_timeout: int = 30,
|
||
pool_recycle: int = 3600,
|
||
):
|
||
# SQLite 不支持 QueuePool 的 pool_size/max_overflow/pool_timeout,
|
||
# 传了会在 create_engine 阶段直接 TypeError,这里只对非 SQLite 传连接池参数。
|
||
if _is_sqlite(database_url):
|
||
return create_engine(database_url, pool_recycle=pool_recycle)
|
||
return create_engine(
|
||
database_url,
|
||
pool_size=pool_size,
|
||
max_overflow=max_overflow,
|
||
pool_timeout=pool_timeout,
|
||
pool_recycle=pool_recycle,
|
||
)
|
||
|
||
|
||
def build_session_factory(
|
||
database_url: str,
|
||
*,
|
||
pool_size: int = 20,
|
||
max_overflow: int = 40,
|
||
pool_timeout: int = 30,
|
||
pool_recycle: int = 3600,
|
||
):
|
||
engine = build_engine(
|
||
database_url,
|
||
pool_size=pool_size,
|
||
max_overflow=max_overflow,
|
||
pool_timeout=pool_timeout,
|
||
pool_recycle=pool_recycle,
|
||
)
|
||
session_factory = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||
global SessionLocal
|
||
SessionLocal = session_factory
|
||
return engine, session_factory
|
||
|
||
|
||
def _is_sqlite(database_url: str) -> bool:
|
||
"""检测是否为 SQLite 数据库 URL."""
|
||
return database_url.startswith("sqlite")
|
||
|
||
|
||
def _build_admin_url(database_url: str) -> URL:
|
||
url = make_url(database_url)
|
||
return url.set(database="postgres")
|
||
|
||
|
||
def ensure_database_exists(database_url: str) -> None:
|
||
"""确保数据库存在(仅 PostgreSQL 需要,SQLite 自动创建)."""
|
||
if _is_sqlite(database_url):
|
||
return
|
||
target_url = make_url(database_url)
|
||
admin_engine = create_engine(_build_admin_url(database_url), isolation_level="AUTOCOMMIT")
|
||
try:
|
||
with admin_engine.connect() as connection:
|
||
exists = connection.execute(
|
||
text("SELECT 1 FROM pg_database WHERE datname = :database_name"),
|
||
{"database_name": target_url.database},
|
||
).scalar()
|
||
if exists:
|
||
return
|
||
connection.execute(text(f'CREATE DATABASE "{target_url.database}"'))
|
||
finally:
|
||
admin_engine.dispose()
|
||
|
||
|
||
def initialize_database(engine) -> None:
|
||
"""初始化数据库 schema。
|
||
|
||
PostgreSQL 使用 advisory lock 防止并发初始化冲突;
|
||
SQLite 直接 create_all(单文件,无并发风险)。
|
||
"""
|
||
if _is_sqlite(str(engine.url)):
|
||
Base.metadata.create_all(bind=engine)
|
||
return
|
||
with engine.connect() as connection:
|
||
connection.execute(text("SELECT pg_advisory_lock(:lock_id)"), {"lock_id": SCHEMA_INIT_LOCK_ID})
|
||
try:
|
||
Base.metadata.create_all(bind=connection)
|
||
connection.commit()
|
||
finally:
|
||
connection.execute(
|
||
text("SELECT pg_advisory_unlock(:lock_id)"),
|
||
{"lock_id": SCHEMA_INIT_LOCK_ID},
|
||
)
|
||
connection.commit()
|