perf(platform-fulfillment): 优化本地选号与失败任务调度
This commit is contained in:
@@ -165,7 +165,10 @@ def test_sync_account_goods_states_writes_capabilities_and_exchange_stats(db):
|
||||
assert states["C"].availability == "no_stock"
|
||||
assert states["D"].availability == "unknown"
|
||||
assert states["A"].valid_until == NOW + timedelta(
|
||||
seconds=service.capability_ttl_seconds()
|
||||
seconds=service.capability_ttl_seconds("available")
|
||||
)
|
||||
assert states["C"].valid_until == NOW + timedelta(
|
||||
seconds=service.capability_ttl_seconds("no_stock")
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ from web.backend.models import (
|
||||
AccountCommodityState,
|
||||
AccountEvent,
|
||||
AccountLease,
|
||||
AccountWorkItem,
|
||||
AutomationJob,
|
||||
DouyuExternalOrder,
|
||||
DouyuGoodsSnapshot,
|
||||
@@ -31,6 +32,7 @@ from web.backend.models import (
|
||||
from web.backend.permissions import ROLE_PERMISSIONS
|
||||
from web.backend.schemas import DouyuCollabOrderOut
|
||||
from web.backend.services import douyu_collab_service as collab
|
||||
from web.backend.services.account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from web.backend.services.automation.jobs import (
|
||||
platform_fulfillment_lease_reaper as reaper,
|
||||
)
|
||||
@@ -94,6 +96,8 @@ class _Base:
|
||||
points: int = 2000,
|
||||
opened: bool = True,
|
||||
role_available_at: datetime | None = None,
|
||||
capability: bool = True,
|
||||
availability: str = "available",
|
||||
) -> Account:
|
||||
row = Account(
|
||||
username=name,
|
||||
@@ -110,6 +114,21 @@ class _Base:
|
||||
role_change_available_at=role_available_at,
|
||||
)
|
||||
self.session.add(row)
|
||||
self.session.flush()
|
||||
if capability:
|
||||
self.session.add(
|
||||
AccountCommodityState(
|
||||
account_id=row.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id=COMMODITY,
|
||||
commodity_name=INTERNAL_SKU,
|
||||
availability=availability,
|
||||
observed_at=NOW,
|
||||
valid_until=NOW + timedelta(hours=6),
|
||||
source="test_fixture",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
return row
|
||||
|
||||
@@ -1058,8 +1077,8 @@ class TestAccountSelection(_Base):
|
||||
assert old_retry_at is None or task.next_allocate_at >= old_retry_at
|
||||
self.refresh_mock.assert_not_called()
|
||||
|
||||
def test_capability_match_checks_only_selected_candidate(self, monkeypatch):
|
||||
account = self.account("capability-fast")
|
||||
def test_capability_match_uses_local_index_without_live_probe(self, monkeypatch):
|
||||
account = self.account("capability-fast", capability=False)
|
||||
task = self.task()
|
||||
self.session.add(
|
||||
AccountCommodityState(
|
||||
@@ -1085,47 +1104,14 @@ class TestAccountSelection(_Base):
|
||||
|
||||
assert detail["task"]["account"]["id"] == account.id
|
||||
assert detail["task"]["executionStatus"] == "allocated"
|
||||
self.refresh_mock.assert_called_once()
|
||||
self.refresh_mock.assert_not_called()
|
||||
|
||||
def test_hybrid_falls_back_to_live_scan_when_capability_candidate_fails(
|
||||
def test_hybrid_without_capability_does_not_fall_back_to_live_scan(
|
||||
self,
|
||||
monkeypatch,
|
||||
):
|
||||
capability_account = self.account("capability-stale")
|
||||
live_account = self.account("live-fallback")
|
||||
self.account("live-fallback", capability=False)
|
||||
task = self.task()
|
||||
self.session.add(
|
||||
AccountCommodityState(
|
||||
account_id=capability_account.id,
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id=COMMODITY,
|
||||
availability="available",
|
||||
observed_at=NOW,
|
||||
valid_until=NOW + timedelta(minutes=15),
|
||||
source="overview",
|
||||
)
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
def live_state(_db, account, **_kwargs):
|
||||
return DouyuLiveAccountState(
|
||||
account_id=account.id,
|
||||
account_name=account.username,
|
||||
checked_at=NOW,
|
||||
ck_verdict="valid",
|
||||
ck_message="有效",
|
||||
elite_opened=True,
|
||||
points=2000,
|
||||
limited_ids=(
|
||||
frozenset({COMMODITY})
|
||||
if account.id == capability_account.id
|
||||
else frozenset()
|
||||
),
|
||||
role_change_available_at=None,
|
||||
)
|
||||
|
||||
self.refresh_mock.side_effect = live_state
|
||||
monkeypatch.setenv("PLATFORM_FULFILLMENT_MATCH_MODE", "hybrid")
|
||||
|
||||
detail = service.allocate_task(
|
||||
@@ -1134,10 +1120,16 @@ class TestAccountSelection(_Base):
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
assert detail["task"]["account"]["id"] == live_account.id
|
||||
assert self.refresh_mock.call_count == 2
|
||||
assert detail["task"]["account"] is None
|
||||
assert detail["task"]["executionStatus"] == "ready"
|
||||
assert detail["task"]["issueCode"] == "capability_cache_miss"
|
||||
self.refresh_mock.assert_not_called()
|
||||
work_item = self.session.query(AccountWorkItem).one()
|
||||
assert work_item.work_type == WORK_STOREHOME_SYNC
|
||||
assert work_item.priority == 20
|
||||
|
||||
def test_allocation_refreshes_live_state_instead_of_cached_filters(self):
|
||||
def test_legacy_mode_keeps_live_scan_fallback(self, monkeypatch):
|
||||
monkeypatch.setenv("PLATFORM_FULFILLMENT_MATCH_MODE", "legacy")
|
||||
account = Account(
|
||||
username="live-refresh-account",
|
||||
password="p",
|
||||
@@ -1226,19 +1218,6 @@ class TestAccountSelection(_Base):
|
||||
|
||||
def test_elite_not_opened_is_soft_and_does_not_block_allocation(self):
|
||||
account = self.account("elite-closed", opened=False)
|
||||
self.refresh_mock.side_effect = lambda _db, row, **_kwargs: (
|
||||
DouyuLiveAccountState(
|
||||
account_id=row.id,
|
||||
account_name=row.username,
|
||||
checked_at=NOW,
|
||||
ck_verdict="valid",
|
||||
ck_message="有效",
|
||||
elite_opened=False,
|
||||
points=2000,
|
||||
limited_ids=frozenset(),
|
||||
role_change_available_at=None,
|
||||
)
|
||||
)
|
||||
task = self.task()
|
||||
|
||||
detail = service.allocate_task(
|
||||
@@ -1253,27 +1232,10 @@ class TestAccountSelection(_Base):
|
||||
assert detail["task"]["issueCode"] == "elite_not_opened"
|
||||
|
||||
def test_presale_not_open_allows_allocation_and_advance_binding(self):
|
||||
account = self.account("presale-future")
|
||||
account = self.account("presale-future", availability="not_open")
|
||||
task = self.task()
|
||||
task.sale_mode = "presale"
|
||||
self.session.commit()
|
||||
self.refresh_mock.side_effect = lambda _db, row, **_kwargs: (
|
||||
DouyuLiveAccountState(
|
||||
account_id=row.id,
|
||||
account_name=row.username,
|
||||
checked_at=NOW,
|
||||
ck_verdict="valid",
|
||||
ck_message="有效",
|
||||
elite_opened=True,
|
||||
points=2000,
|
||||
limited_ids=frozenset(),
|
||||
role_change_available_at=None,
|
||||
commodity_availability={COMMODITY: "not_open"},
|
||||
commodity_blocking_reasons={
|
||||
COMMODITY: "限时兑换未开始,开始于 2026-11-26T10:00:00+00:00"
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
detail = service.allocate_task(
|
||||
self.session,
|
||||
@@ -1287,27 +1249,10 @@ class TestAccountSelection(_Base):
|
||||
assert detail["task"]["issueCode"] == "commodity_not_open"
|
||||
|
||||
def test_direct_sale_still_blocks_when_commodity_not_open(self):
|
||||
self.account("direct-future")
|
||||
self.account("direct-future", availability="not_open")
|
||||
task = self.task()
|
||||
task.sale_mode = "direct"
|
||||
self.session.commit()
|
||||
self.refresh_mock.side_effect = lambda _db, row, **_kwargs: (
|
||||
DouyuLiveAccountState(
|
||||
account_id=row.id,
|
||||
account_name=row.username,
|
||||
checked_at=NOW,
|
||||
ck_verdict="valid",
|
||||
ck_message="有效",
|
||||
elite_opened=True,
|
||||
points=2000,
|
||||
limited_ids=frozenset(),
|
||||
role_change_available_at=None,
|
||||
commodity_availability={COMMODITY: "not_open"},
|
||||
commodity_blocking_reasons={
|
||||
COMMODITY: "限时兑换未开始,开始于 2026-11-26T10:00:00+00:00"
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
detail = service.allocate_task(
|
||||
self.session,
|
||||
@@ -1317,10 +1262,10 @@ class TestAccountSelection(_Base):
|
||||
|
||||
assert detail["task"]["account"] is None
|
||||
assert detail["task"]["executionStatus"] == "ready"
|
||||
assert detail["task"]["issueCode"] == "no_eligible_account"
|
||||
assert detail["task"]["issueCode"] == "commodity_not_open"
|
||||
|
||||
def test_capability_pool_includes_future_not_open_for_presale_only(self):
|
||||
account = self.account("capability-future")
|
||||
account = self.account("capability-future", capability=False)
|
||||
self.session.add(
|
||||
AccountCommodityState(
|
||||
account_id=account.id,
|
||||
@@ -1378,7 +1323,11 @@ class TestAccountSelection(_Base):
|
||||
assert [row.username for row in candidates] == ["small", "large"]
|
||||
|
||||
def test_active_collab_order_blocks_all_automatic_candidate_pools(self):
|
||||
account = self.account("active-collab-candidate", points=2000)
|
||||
account = self.account(
|
||||
"active-collab-candidate",
|
||||
points=2000,
|
||||
capability=False,
|
||||
)
|
||||
self.session.add(
|
||||
AccountCommodityState(
|
||||
account_id=account.id,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user