fix(platform-fulfillment): 释放空闲连接并合并选号请求
This commit is contained in:
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user