perf(platform-fulfillment): 优化本地选号与失败任务调度
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user