fix(platform-fulfillment): 释放空闲连接并合并选号请求

This commit is contained in:
yml
2026-11-21 17:35:34 +08:00
parent f74573a9c0
commit 7f7943a1e8
17 changed files with 690 additions and 74 deletions
+44 -14
View File
@@ -7,6 +7,7 @@ from urllib.parse import quote_plus
from sqlalchemy import DateTime, create_engine, event
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy.pool import NullPool
from sqlalchemy.types import TypeDecorator
PROJECT_ROOT = Path(__file__).resolve().parents[2]
@@ -21,6 +22,11 @@ def _env_int(name: str, default: int) -> int:
return value if value > 0 else default
def _pool_mode(name: str, default: str = "queue") -> str:
mode = os.getenv(name, default).strip().lower()
return mode if mode in {"queue", "null"} else default
def _get_database_url() -> str:
"""优先使用完整连接串,否则按 DB_* 环境变量构造 MySQL 连接。"""
database_url = os.getenv("DATABASE_URL", "").strip()
@@ -61,13 +67,16 @@ engine_options = {
"echo": False,
}
if not DATABASE_URL.startswith("sqlite"):
engine_options.update(
pool_pre_ping=True,
pool_size=_env_int("DB_POOL_SIZE", 20),
max_overflow=_env_int("DB_MAX_OVERFLOW", 20),
pool_timeout=_env_int("DB_POOL_TIMEOUT", 30),
pool_recycle=_env_int("DB_POOL_RECYCLE", 1800),
)
if _pool_mode("DB_POOL_MODE") == "null":
engine_options["poolclass"] = NullPool
else:
engine_options["pool_pre_ping"] = True
engine_options.update(
pool_size=_env_int("DB_POOL_SIZE", 20),
max_overflow=_env_int("DB_MAX_OVERFLOW", 20),
pool_timeout=_env_int("DB_POOL_TIMEOUT", 30),
pool_recycle=_env_int("DB_POOL_RECYCLE", 1800),
)
engine = create_engine(DATABASE_URL, **engine_options)
@@ -76,13 +85,22 @@ worker_engine_options = {
"echo": False,
}
if not DATABASE_URL.startswith("sqlite"):
worker_engine_options.update(
pool_pre_ping=True,
pool_size=_env_int("WORKER_DB_POOL_SIZE", 12),
max_overflow=_env_int("WORKER_DB_MAX_OVERFLOW", 4),
pool_timeout=_env_int("WORKER_DB_POOL_TIMEOUT", 30),
pool_recycle=_env_int("DB_POOL_RECYCLE", 1800),
)
if (
_pool_mode(
"WORKER_DB_POOL_MODE",
_pool_mode("DB_POOL_MODE"),
)
== "null"
):
worker_engine_options["poolclass"] = NullPool
else:
worker_engine_options["pool_pre_ping"] = True
worker_engine_options.update(
pool_size=_env_int("WORKER_DB_POOL_SIZE", 12),
max_overflow=_env_int("WORKER_DB_MAX_OVERFLOW", 4),
pool_timeout=_env_int("WORKER_DB_POOL_TIMEOUT", 30),
pool_recycle=_env_int("DB_POOL_RECYCLE", 1800),
)
worker_engine = create_engine(DATABASE_URL, **worker_engine_options)
@@ -140,6 +158,18 @@ WorkerSessionLocal = sessionmaker(
Base = declarative_base()
def dispose_engines() -> None:
"""关闭应用中所有持久连接。
`NullPool` 模式下通常没有空闲连接可关闭;保留这个统一出口是为了:
- queue 模式进程退出时主动释放 Sleep 连接;
- uvicorn reload / 测试重建应用时避免旧 engine 残留;
- 任务线程停止后再统一收口,顺序可预测。
"""
engine.dispose()
worker_engine.dispose()
class UTCDateTime(TypeDecorator):
"""时间列统一走这里:**库里存 naive UTC,Python 侧永远是 tz-aware UTC**。
+2 -1
View File
@@ -57,7 +57,7 @@ async def lifespan(app: FastAPI):
# 进程重启后内存批次丢失,清理历史 pending/running,避免前端被假活跃批次锁死。
from loguru import logger
from .database import SessionLocal
from .database import SessionLocal, dispose_engines
from .services.douyu_service import cleanup_orphan_douyu_tasks
from .services.huya_service import cleanup_orphan_huya_tasks
from .services.login_service import (
@@ -138,6 +138,7 @@ async def lifespan(app: FastAPI):
stop_integration_outbox_worker()
stop_scheduler()
shutdown_task_runtime(wait=False)
dispose_engines()
app = FastAPI(
+8 -2
View File
@@ -352,15 +352,21 @@ def allocate_task(
db: Session = Depends(get_db),
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = service.allocate_task(db, task_id=task_id, current=current)
result = service.prepare_manual_allocation(db, task_id=task_id, current=current)
record_audit(
db,
current,
action="platform_fulfillment:allocate",
target=f"platform_fulfillment_task:{task_id}",
detail={"accountId": result.get("task", {}).get("account", {})},
detail={"requested": True},
)
db.commit()
from ..services.platform_fulfillment.auto_allocator import queue_task_allocation
result["allocationQueued"] = (
result.get("task", {}).get("executionStatus") == "ready"
and queue_task_allocation(task_id)
)
return result
@@ -27,6 +27,7 @@ _thread: threading.Thread | None = None
_lock = threading.Lock()
_pending_lock = threading.Lock()
_pending_task_ids: set[int] = set()
_running_task_ids: set[int] = set()
_wake_event = threading.Event()
@@ -39,14 +40,33 @@ def _env_int(name: str, default: int, minimum: int, maximum: int) -> int:
def wake_auto_allocator(task_ids: Iterable[int] | None = None) -> None:
"""立即唤醒选号 worker;传入任务 ID 时优先处理这些新任务。"""
"""立即唤醒选号 worker;同一任务在排队或执行中时不会重复入队。"""
normalized = {int(task_id) for task_id in (task_ids or []) if int(task_id) > 0}
if normalized:
with _pending_lock:
_pending_task_ids.update(normalized)
for task_id in normalized:
if task_id not in _running_task_ids:
_pending_task_ids.add(task_id)
_wake_event.set()
def queue_task_allocation(task_id: int) -> bool:
"""把一次人工选号请求合并到后台 worker。
返回值只表示本次调用是否新增了队列项。重复点击、已经在执行以及已经排队的
请求都返回 False,调用方不应把它当成失败。
"""
normalized = int(task_id)
if normalized <= 0:
return False
with _pending_lock:
if normalized in _running_task_ids or normalized in _pending_task_ids:
return False
_pending_task_ids.add(normalized)
_wake_event.set()
return True
def _take_pending_task_ids(limit: int) -> list[int]:
with _pending_lock:
selected = sorted(_pending_task_ids)[:limit]
@@ -55,6 +75,19 @@ def _take_pending_task_ids(limit: int) -> list[int]:
return selected
def _claim_running_task_ids(task_ids: Iterable[int]) -> list[int]:
normalized = [int(task_id) for task_id in task_ids]
with _pending_lock:
_running_task_ids.update(normalized)
_pending_task_ids.difference_update(normalized)
return normalized
def _release_running_task_ids(task_ids: Iterable[int]) -> None:
with _pending_lock:
_running_task_ids.difference_update(int(task_id) for task_id in task_ids)
def _has_pending_task_ids() -> bool:
with _pending_lock:
return bool(_pending_task_ids)
@@ -127,37 +160,50 @@ def run_auto_allocation_once(
if not candidates:
return {"checked": 0, "allocated": 0, "failed": 0}
actor = service._integration_actor(db)
allocated = 0
failed = 0
for task in candidates:
candidate_task_id = int(task.id)
try:
detail: dict[str, Any] = service.allocate_task(
db,
task_id=candidate_task_id,
current=actor,
)
if detail.get("task", {}).get("account"):
allocated += 1
allocated_task = db.get(PlatformFulfillmentTask, candidate_task_id)
if allocated_task is not None:
allocated_task.next_allocate_at = None
db.commit()
try:
service.start_binding_sync(
db,
task_id=candidate_task_id,
current=actor,
)
except Exception as exc: # noqa: BLE001 - allocation remains valid
db.rollback()
logger.warning(
"直播账号履约自动开始绑定失败 task_id={}: {}",
candidate_task_id,
exc,
)
else:
candidate_task_ids = _claim_running_task_ids(
int(task.id) for task in candidates
)
try:
actor = service._integration_actor(db)
allocated = 0
failed = 0
for task in candidates:
candidate_task_id = int(task.id)
try:
detail: dict[str, Any] = service.allocate_task(
db,
task_id=candidate_task_id,
current=actor,
)
if detail.get("task", {}).get("account"):
allocated += 1
allocated_task = db.get(PlatformFulfillmentTask, candidate_task_id)
if allocated_task is not None:
allocated_task.next_allocate_at = None
db.commit()
try:
service.start_binding_sync(
db,
task_id=candidate_task_id,
current=actor,
)
except Exception as exc: # noqa: BLE001 - allocation remains valid
db.rollback()
logger.warning(
"直播账号履约自动开始绑定失败 task_id={}: {}",
candidate_task_id,
exc,
)
else:
failed += 1
_schedule_retry(
db,
task_id=candidate_task_id,
moment=moment,
retry_seconds=retry_seconds,
)
except Exception as exc: # noqa: BLE001 - 单个任务异常不能中断整批
db.rollback()
failed += 1
_schedule_retry(
db,
@@ -165,25 +211,18 @@ def run_auto_allocation_once(
moment=moment,
retry_seconds=retry_seconds,
)
except Exception as exc: # noqa: BLE001 - 单个任务异常不能中断整批
db.rollback()
failed += 1
_schedule_retry(
db,
task_id=candidate_task_id,
moment=moment,
retry_seconds=retry_seconds,
)
logger.warning(
"直播账号履约自动选号失败 task_id={}: {}",
candidate_task_id,
exc,
)
return {
"checked": len(candidates),
"allocated": allocated,
"failed": failed,
}
logger.warning(
"直播账号履约自动选号失败 task_id={}: {}",
candidate_task_id,
exc,
)
return {
"checked": len(candidates),
"allocated": allocated,
"failed": failed,
}
finally:
_release_running_task_ids(candidate_task_ids)
def start_allocated_bindings_once(
@@ -282,6 +321,7 @@ def start_platform_fulfillment_auto_allocator() -> bool:
_wake_event.clear()
with _pending_lock:
_pending_task_ids.clear()
_running_task_ids.clear()
_thread = threading.Thread(
target=_run,
name="platform-fulfillment-auto-allocator",
@@ -2045,6 +2045,34 @@ def upsert_mapping(
return update_mapping(db, mapping_id=row.id, **values)
def prepare_manual_allocation(
db: Session,
*,
task_id: int,
current: User,
) -> dict[str, Any]:
"""在短请求内完成人工选号校验,并把任务标记为立即可领取。
真正的官网账号检测由后台 auto allocator 执行。这样 API 请求不会在多次外部
HTTP 调用期间一直持有数据库事务,也不会因为重复点击占满连接池。
"""
task = _load_task(db, task_id)
if task.execution_status in {"completed", "cancelled"}:
raise HTTPException(status_code=409, detail="已结束任务不能重新分配账号")
if not task.commodity_id or not task.required_points:
raise HTTPException(status_code=409, detail="请先完成商品映射")
if task.account_id and task.lease_id:
lease = db.query(AccountLease).filter(AccountLease.id == task.lease_id).first()
if lease and lease.status == "active":
return get_task_detail(db, task.id)
if task.execution_status != "ready":
raise HTTPException(status_code=409, detail="只有待分配任务可以重新选号")
task.next_allocate_at = _now()
task.updated_at = _now()
db.flush()
return get_task_detail(db, task.id)
def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]:
task = _load_task(db, task_id)
if task.execution_status in {"completed", "cancelled"}:
@@ -3310,6 +3338,7 @@ __all__ = [
"list_tasks",
"mapping_impact",
"mark_manual_required",
"prepare_manual_allocation",
"reconcile_binding_state_outbox",
"record_douyu_operation_result",
"record_douyu_operation_started",
+2 -1
View File
@@ -1,5 +1,6 @@
import api from './client';
import type {
PlatformFulfillmentAllocationResult,
PlatformFulfillmentDetail,
PlatformFulfillmentCollabSyncResult,
PlatformFulfillmentListResponse,
@@ -105,7 +106,7 @@ export const platformFulfillmentApi = {
groups: Array<{ executionStatus: string; saleMode: string; count: number }>;
}>(`/platform-fulfillment/mappings/${mappingId}/impact`),
allocate: (taskId: number) =>
api.post<PlatformFulfillmentDetail, PlatformFulfillmentDetail>(
api.post<PlatformFulfillmentAllocationResult, PlatformFulfillmentAllocationResult>(
`/platform-fulfillment/tasks/${taskId}/allocate`,
),
startBinding: (taskId: number) =>
+4
View File
@@ -2278,6 +2278,10 @@ export interface PlatformFulfillmentDetail {
events: PlatformFulfillmentEvent[];
}
export interface PlatformFulfillmentAllocationResult extends PlatformFulfillmentDetail {
allocationQueued: boolean;
}
export interface PlatformFulfillmentMapping {
id: number;
targetPlatform: string;
@@ -348,6 +348,7 @@ export default function PlatformFulfillmentPage() {
const [syncing, setSyncing] = useState(false);
const [collabSyncing, setCollabSyncing] = useState(false);
const [rowCollabSyncingId, setRowCollabSyncingId] = useState<number | null>(null);
const [allocatingTaskId, setAllocatingTaskId] = useState<number | null>(null);
const [restoreTarget, setRestoreTarget] = useState<PlatformFulfillmentTask | null>(null);
const [restoreReason, setRestoreReason] = useState('');
const [restoring, setRestoring] = useState(false);
@@ -603,6 +604,26 @@ export default function PlatformFulfillmentPage() {
}
};
const requestAllocation = async (task: PlatformFulfillmentTask) => {
if (allocatingTaskId !== null) return;
setAllocatingTaskId(task.id);
try {
const result = await platformFulfillmentApi.allocate(task.id);
setDetail(result);
setSelectedId(result.task.id);
await loadTasks();
if (result.allocationQueued) {
message.info('选号请求已提交,后台完成后会自动刷新');
} else {
message.info('该任务已在选号队列中或已经分配,请稍候刷新');
}
} catch (error) {
message.error(getErrorMessage(error));
} finally {
setAllocatingTaskId(null);
}
};
const resetBinding = (task: PlatformFulfillmentTask) => {
Modal.confirm({
title: '重置角色资料和绑定记录?',
@@ -1156,7 +1177,14 @@ export default function PlatformFulfillmentPage() {
</Button>
) : null}
{task.executionStatus === 'ready' ? (
<Button size="small" type="primary" icon={<UserSwitchOutlined />} onClick={() => void runTaskAction(() => platformFulfillmentApi.allocate(task.id))}>
<Button
size="small"
type="primary"
icon={<UserSwitchOutlined />}
loading={allocatingTaskId === task.id}
disabled={allocatingTaskId !== null && allocatingTaskId !== task.id}
onClick={() => void requestAllocation(task)}
>
人工重试选号
</Button>
) : null}