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
@@ -113,7 +113,7 @@ def test_auto_allocator_schedules_retry_only_after_failed_allocation(db):
order_match_started_at=datetime.now(UTC) - timedelta(days=1),
)
)
moment = datetime.now(UTC)
moment = datetime.now(UTC).replace(microsecond=0)
task = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="10",
@@ -202,3 +202,94 @@ def test_auto_allocator_prioritizes_oldest_paid_task_after_due_check(db):
auto_allocator.run_auto_allocation_once(db, limit=1, now=moment)
assert calls == [older.id]
def test_failed_task_retry_does_not_block_other_due_tasks(db):
actor = User(username="auto-rotate-actor", password_hash="h", role="super_admin")
moment = datetime.now(UTC)
db.add(
PlatformFulfillmentSetting(
id=1,
lease_duration_minutes=None,
order_match_started_at=moment - timedelta(days=1),
)
)
failing = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="30",
source_order_item_id="30",
internal_sku_key="skin",
commodity_id="commodity-1",
execution_status="ready",
binding_status="not_started",
exchange_status="not_started",
paid_at=moment - timedelta(minutes=10),
next_allocate_at=moment,
updated_at=moment,
)
waiting = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="31",
source_order_item_id="31",
internal_sku_key="skin",
commodity_id="commodity-1",
execution_status="ready",
binding_status="not_started",
exchange_status="not_started",
paid_at=moment - timedelta(minutes=1),
next_allocate_at=moment,
updated_at=moment,
)
db.add_all([actor, failing, waiting])
db.commit()
calls: list[int] = []
with (
patch.object(auto_allocator.service, "_integration_actor", return_value=actor),
patch.object(
auto_allocator.service,
"allocate_task",
side_effect=lambda _db, *, task_id, current: (
calls.append(task_id) or {"task": {"account": None}}
),
),
):
auto_allocator.run_auto_allocation_once(db, limit=1, now=moment)
auto_allocator.run_auto_allocation_once(
db,
limit=1,
now=moment + timedelta(seconds=1),
)
assert calls == [failing.id, waiting.id]
def test_schedule_retry_preserves_longer_capability_retry(db):
actor = User(username="auto-backoff-actor", password_hash="h", role="super_admin")
moment = datetime.now(UTC).replace(microsecond=0)
retry_at = moment + timedelta(minutes=5)
task = PlatformFulfillmentTask(
source_system="order_site",
source_order_id="40",
source_order_item_id="40",
internal_sku_key="skin",
commodity_id="commodity-1",
execution_status="ready",
binding_status="not_started",
exchange_status="not_started",
paid_at=moment,
next_allocate_at=retry_at,
updated_at=moment,
)
db.add_all([actor, task])
db.commit()
auto_allocator._schedule_retry(
db,
task_id=task.id,
moment=moment,
retry_seconds=45,
)
db.refresh(task)
assert task.next_allocate_at == retry_at