perf(fulfillment): Webhook 新订单即时选号

This commit is contained in:
yml
2026-11-23 14:09:04 +08:00
parent 05a7a56c4e
commit 11c2a0dba7
4 changed files with 123 additions and 12 deletions
+7 -3
View File
@@ -99,10 +99,14 @@ def sync_tasks(
current: User = Depends(require_permission("platform_fulfillment:use")),
) -> dict[str, Any]:
result = service.sync_pending_tasks(db, limit=limit)
if result.get("taskIds"):
from ..services.platform_fulfillment.auto_allocator import wake_auto_allocator
from ..services.platform_fulfillment.auto_allocator import (
run_immediate_allocation,
)
wake_auto_allocator(result["taskIds"])
result["immediateAllocation"] = run_immediate_allocation(
db,
task_ids=result.get("taskIds", []),
)
record_audit(
db,
current,
@@ -107,8 +107,15 @@ async def sync_order_event(
result = service.sync_order_item_ids(db, item_ids, commit=False)
inbox.processed_at = datetime.now(UTC)
db.commit()
auto_allocator.wake_auto_allocator(result.get("taskIds", []))
return {**result, "duplicate": False}
task_ids = [
int(task_id) for task_id in result.get("taskIds", []) if str(task_id).isdigit()
]
immediate = auto_allocator.run_immediate_allocation(db, task_ids=task_ids)
return {
**result,
"duplicate": False,
"immediateAllocation": immediate,
}
@router.post(
@@ -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()
)