perf(platform-fulfillment): 优化本地选号与失败任务调度

This commit is contained in:
yml
2026-11-22 19:15:44 +08:00
parent 6a0e88055d
commit e652d30283
10 changed files with 701 additions and 131 deletions
@@ -19,7 +19,7 @@ from . import service
DEFAULT_INTERVAL_SECONDS = 20
DEFAULT_MIN_INTERVAL_SECONDS = 5
DEFAULT_BATCH_SIZE = 2
DEFAULT_BATCH_SIZE = 20
DEFAULT_RETRY_SECONDS = 45
_stop_event = threading.Event()
@@ -103,7 +103,9 @@ def _schedule_retry(
task = db.get(PlatformFulfillmentTask, int(task_id))
if task is None or task.execution_status in {"completed", "cancelled"}:
return
task.next_allocate_at = moment + timedelta(seconds=retry_seconds)
scheduled_retry = moment + timedelta(seconds=retry_seconds)
if task.next_allocate_at is None or task.next_allocate_at < scheduled_retry:
task.next_allocate_at = scheduled_retry
db.commit()
@@ -151,6 +153,9 @@ def run_auto_allocation_once(
)
candidates = (
query.order_by(
# 失败任务会被推进到未来;按 next_allocate_at 轮转可保证队首
# 两个永久无库存任务不会每轮都抢走全部执行槽。
PlatformFulfillmentTask.next_allocate_at.asc(),
PlatformFulfillmentTask.paid_at.asc(),
PlatformFulfillmentTask.id.asc(),
)