perf(fulfillment): Webhook 新订单即时选号
This commit is contained in:
@@ -78,9 +78,12 @@ def _take_pending_task_ids(limit: int) -> list[int]:
|
||||
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
|
||||
claimed = [
|
||||
task_id for task_id in normalized if task_id not in _running_task_ids
|
||||
]
|
||||
_running_task_ids.update(claimed)
|
||||
_pending_task_ids.difference_update(claimed)
|
||||
return claimed
|
||||
|
||||
|
||||
def _release_running_task_ids(task_ids: Iterable[int]) -> None:
|
||||
@@ -165,9 +168,11 @@ def run_auto_allocation_once(
|
||||
if not candidates:
|
||||
return {"checked": 0, "allocated": 0, "failed": 0}
|
||||
|
||||
candidate_task_ids = _claim_running_task_ids(
|
||||
int(task.id) for task in candidates
|
||||
)
|
||||
candidate_task_ids = _claim_running_task_ids(int(task.id) for task in candidates)
|
||||
if not candidate_task_ids:
|
||||
return {"checked": 0, "allocated": 0, "failed": 0}
|
||||
claimed_task_ids = set(candidate_task_ids)
|
||||
candidates = [task for task in candidates if int(task.id) in claimed_task_ids]
|
||||
try:
|
||||
actor = service._integration_actor(db)
|
||||
allocated = 0
|
||||
@@ -230,6 +235,35 @@ def run_auto_allocation_once(
|
||||
_release_running_task_ids(candidate_task_ids)
|
||||
|
||||
|
||||
def run_immediate_allocation(
|
||||
db: Session,
|
||||
*,
|
||||
task_ids: Iterable[int],
|
||||
) -> dict[str, int]:
|
||||
"""Webhook 已同步新订单后立即尝试选号,避免等待低优先级后台线程。"""
|
||||
normalized_ids = [
|
||||
int(task_id) for task_id in dict.fromkeys(task_ids or []) if int(task_id) > 0
|
||||
]
|
||||
if not normalized_ids:
|
||||
return {"checked": 0, "allocated": 0, "failed": 0}
|
||||
try:
|
||||
return run_auto_allocation_once(
|
||||
db,
|
||||
limit=min(len(normalized_ids), DEFAULT_BATCH_SIZE),
|
||||
task_ids=normalized_ids,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - webhook sync remains committed
|
||||
db.rollback()
|
||||
logger.warning("直播账号履约即时选号失败: {}", exc)
|
||||
return {
|
||||
"checked": len(normalized_ids),
|
||||
"allocated": 0,
|
||||
"failed": len(normalized_ids),
|
||||
}
|
||||
finally:
|
||||
wake_auto_allocator(normalized_ids)
|
||||
|
||||
|
||||
def start_allocated_bindings_once(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -244,7 +278,9 @@ def start_allocated_bindings_once(
|
||||
PlatformFulfillmentTask.account_id.isnot(None),
|
||||
PlatformFulfillmentTask.lease_id.isnot(None),
|
||||
)
|
||||
.order_by(PlatformFulfillmentTask.updated_at.asc(), PlatformFulfillmentTask.id.asc())
|
||||
.order_by(
|
||||
PlatformFulfillmentTask.updated_at.asc(), PlatformFulfillmentTask.id.asc()
|
||||
)
|
||||
.limit(max(1, limit))
|
||||
.all()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user