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",
|
||||
|
||||
Reference in New Issue
Block a user