perf(douyu): 拆分任务槽并改造绑定扫码调度

This commit is contained in:
yml
2026-11-20 22:03:20 +08:00
parent 8869e427d2
commit 071d45ad1d
40 changed files with 1635 additions and 245 deletions
@@ -2,6 +2,7 @@
from __future__ import annotations
import asyncio
import os
import threading
from collections.abc import Iterable
@@ -12,7 +13,7 @@ from loguru import logger
from sqlalchemy import or_
from sqlalchemy.orm import Session
from ...database import SessionLocal
from ...database import WorkerSessionLocal
from ...models import PlatformFulfillmentTask
from . import service
@@ -142,6 +143,21 @@ def run_auto_allocation_once(
if allocated_task is not None:
allocated_task.next_allocate_at = None
db.commit()
try:
asyncio.run(
service.start_binding(
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(
@@ -171,6 +187,48 @@ def run_auto_allocation_once(
}
def start_allocated_bindings_once(
db: Session,
*,
limit: int = DEFAULT_BATCH_SIZE,
) -> int:
"""Start durable binding preparation for allocated, unstarted sessions."""
rows = (
db.query(PlatformFulfillmentTask)
.filter(
PlatformFulfillmentTask.execution_status == "allocated",
PlatformFulfillmentTask.binding_status == "not_started",
PlatformFulfillmentTask.account_id.isnot(None),
PlatformFulfillmentTask.lease_id.isnot(None),
)
.order_by(PlatformFulfillmentTask.updated_at.asc(), PlatformFulfillmentTask.id.asc())
.limit(max(1, limit))
.all()
)
if not rows:
return 0
actor = service._integration_actor(db)
started = 0
for task in rows:
try:
asyncio.run(
service.start_binding(
db,
task_id=int(task.id),
current=actor,
)
)
started += 1
except Exception as exc: # noqa: BLE001 - one session cannot block the rest
db.rollback()
logger.warning(
"直播账号履约补启动绑定失败 task_id={}: {}",
task.id,
exc,
)
return started
def _run() -> None:
interval = _env_int(
"PLATFORM_FULFILLMENT_AUTO_ALLOCATE_SECONDS",
@@ -179,7 +237,7 @@ def _run() -> None:
3600,
)
while not _stop_event.is_set():
db = SessionLocal()
db = WorkerSessionLocal()
try:
pending_ids = _take_pending_task_ids(
_env_int(
@@ -190,8 +248,11 @@ def _run() -> None:
)
)
result = run_auto_allocation_once(db, task_ids=pending_ids or None)
started = start_allocated_bindings_once(db)
if result["checked"]:
logger.info("直播账号履约自动选号: {}", result)
if started:
logger.info("直播账号履约补启动绑定: {} 条", started)
except Exception as exc: # noqa: BLE001 - worker 不能因单轮异常退出
db.rollback()
logger.warning("直播账号履约自动选号轮次失败: {}", exc)