fix(douyu): 补全账号能力目录并回收失效租约
This commit is contained in:
@@ -8,6 +8,7 @@ from web.backend.models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountLease,
|
||||
AccountSyncState,
|
||||
AccountWorkItem,
|
||||
AutomationJob,
|
||||
PlatformFulfillmentSetting,
|
||||
@@ -61,7 +62,7 @@ def _state(
|
||||
return row
|
||||
|
||||
|
||||
def test_selects_only_stale_demand_accounts(db):
|
||||
def test_selects_stale_and_never_scanned_demand_accounts(db):
|
||||
user = User(
|
||||
username="cap-refresh-admin",
|
||||
password_hash="h",
|
||||
@@ -111,7 +112,7 @@ def test_selects_only_stale_demand_accounts(db):
|
||||
|
||||
selected = job.build_select_query(db, automation_job, NOW).all()
|
||||
|
||||
assert [row.id for row in selected] == [stale.id]
|
||||
assert [row.id for row in selected] == [stale.id, other.id, leased.id]
|
||||
|
||||
|
||||
def test_no_pending_task_means_no_refresh(db):
|
||||
@@ -177,6 +178,16 @@ def test_refresh_ignores_tasks_before_match_cutoff(db):
|
||||
commodity_id="NEW",
|
||||
valid_until=NOW - timedelta(minutes=1),
|
||||
)
|
||||
db.add(
|
||||
AccountSyncState(
|
||||
account_id=old_account.id,
|
||||
platform="douyu",
|
||||
domain="storehome",
|
||||
status="success",
|
||||
last_success_at=NOW,
|
||||
valid_until=NOW + timedelta(hours=1),
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
automation_job = AutomationJob(
|
||||
key=job.JOB_KEY,
|
||||
@@ -225,7 +236,91 @@ def test_run_enqueues_stale_accounts_without_external_query(db):
|
||||
assert result["scanned"] == 1
|
||||
assert result["success"] == 1
|
||||
assert work.status == "pending"
|
||||
assert work.priority == 40
|
||||
assert work.priority == 10
|
||||
|
||||
|
||||
def test_refresh_skips_accounts_with_active_sync_work(db):
|
||||
db.add(
|
||||
PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id="3",
|
||||
source_order_item_id="3",
|
||||
internal_sku_key="skin",
|
||||
commodity_id="C",
|
||||
execution_status="ready",
|
||||
binding_status="not_started",
|
||||
exchange_status="not_started",
|
||||
paid_at=NOW,
|
||||
)
|
||||
)
|
||||
active = _account(db, "cap-active-work")
|
||||
next_account = _account(db, "cap-next-work")
|
||||
_state(db, active, valid_until=NOW - timedelta(minutes=1))
|
||||
_state(db, next_account, valid_until=NOW - timedelta(minutes=1))
|
||||
db.add(
|
||||
AccountWorkItem(
|
||||
account_id=active.id,
|
||||
work_type="storehome_sync",
|
||||
priority=10,
|
||||
status="pending",
|
||||
due_at=NOW,
|
||||
dedupe_key=f"storehome_sync:{active.id}",
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
automation_job = AutomationJob(
|
||||
key=job.JOB_KEY,
|
||||
title="履约商品能力补充刷新",
|
||||
interval_seconds=3600,
|
||||
batch_size=10,
|
||||
concurrency=3,
|
||||
)
|
||||
|
||||
selected = job.build_select_query(db, automation_job, NOW).all()
|
||||
|
||||
assert [row.id for row in selected] == [next_account.id]
|
||||
|
||||
|
||||
def test_refresh_stops_at_queue_target(db, monkeypatch):
|
||||
monkeypatch.setenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "1")
|
||||
db.add(
|
||||
PlatformFulfillmentTask(
|
||||
source_system="order_site",
|
||||
source_order_id="4",
|
||||
source_order_item_id="4",
|
||||
internal_sku_key="skin",
|
||||
commodity_id="C",
|
||||
execution_status="ready",
|
||||
binding_status="not_started",
|
||||
exchange_status="not_started",
|
||||
paid_at=NOW,
|
||||
)
|
||||
)
|
||||
account = _account(db, "cap-queue-full")
|
||||
_state(db, account, valid_until=NOW - timedelta(minutes=1))
|
||||
db.add(
|
||||
AccountWorkItem(
|
||||
account_id=account.id,
|
||||
work_type="storehome_sync",
|
||||
priority=10,
|
||||
status="pending",
|
||||
due_at=NOW,
|
||||
dedupe_key=f"storehome_sync:{account.id}",
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
automation_job = AutomationJob(
|
||||
key=job.JOB_KEY,
|
||||
title="履约商品能力补充刷新",
|
||||
interval_seconds=3600,
|
||||
batch_size=10,
|
||||
concurrency=3,
|
||||
)
|
||||
|
||||
result = job.run_account_capability_refresh(db, automation_job)
|
||||
|
||||
assert result["scanned"] == 0
|
||||
assert db.query(AccountWorkItem).count() == 1
|
||||
|
||||
|
||||
def test_default_enabled_job_is_created_enabled(db, monkeypatch):
|
||||
|
||||
@@ -295,6 +295,80 @@ def test_supply_shortage_enqueues_missing_coverage(db):
|
||||
assert work.priority == 35
|
||||
|
||||
|
||||
def test_supply_shortage_does_not_reselect_account_with_active_sync(db):
|
||||
first = _account("supply-active-work", points=400)
|
||||
second = _account("supply-next-work", points=400)
|
||||
db.add_all([first, second])
|
||||
db.flush()
|
||||
enqueue_account_work(
|
||||
db,
|
||||
account_id=first.id,
|
||||
work_type="storehome_sync",
|
||||
priority=10,
|
||||
due_at=NOW,
|
||||
)
|
||||
db.add(
|
||||
AccountSupplyTarget(
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="C",
|
||||
value_class=VALUE_HIGH,
|
||||
target_covered=1,
|
||||
target_available=1,
|
||||
target_allocatable=1,
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
result = enqueue_supply_replenishment(db, now=NOW, max_per_target=10)
|
||||
|
||||
queued = (
|
||||
db.query(AccountWorkItem)
|
||||
.filter(AccountWorkItem.work_type == "storehome_sync")
|
||||
.order_by(AccountWorkItem.account_id.asc())
|
||||
.all()
|
||||
)
|
||||
assert result.queued == 1
|
||||
assert [row.account_id for row in queued] == [first.id, second.id]
|
||||
|
||||
|
||||
def test_supply_replenishment_respects_sync_queue_target(db, monkeypatch):
|
||||
monkeypatch.setenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "1")
|
||||
queued_account = _account("supply-queue-full", points=400)
|
||||
missing = _account("supply-wait-next-round", points=400)
|
||||
db.add_all([queued_account, missing])
|
||||
db.flush()
|
||||
enqueue_account_work(
|
||||
db,
|
||||
account_id=queued_account.id,
|
||||
work_type="storehome_sync",
|
||||
priority=10,
|
||||
due_at=NOW,
|
||||
)
|
||||
db.add(
|
||||
AccountSupplyTarget(
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="C",
|
||||
value_class=VALUE_HIGH,
|
||||
target_covered=1,
|
||||
target_available=1,
|
||||
target_allocatable=1,
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
result = enqueue_supply_replenishment(db, now=NOW, max_per_target=10)
|
||||
|
||||
assert result.queued == 0
|
||||
assert (
|
||||
db.query(AccountWorkItem)
|
||||
.filter(AccountWorkItem.work_type == "storehome_sync")
|
||||
.count()
|
||||
== 1
|
||||
)
|
||||
|
||||
|
||||
def test_supply_target_configuration_is_idempotent(db):
|
||||
first = upsert_supply_target(
|
||||
db,
|
||||
|
||||
@@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261122_0115"
|
||||
HEAD_REVISION = "20261122_0116"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
|
||||
@@ -1118,6 +1118,39 @@ class TestAccountSelection(_Base):
|
||||
assert detail["task"]["executionStatus"] == "allocated"
|
||||
self.refresh_mock.assert_not_called()
|
||||
|
||||
def test_allocation_reports_role_cooldown_without_fast_retry_loop(
|
||||
self,
|
||||
monkeypatch,
|
||||
):
|
||||
self.account(
|
||||
"capability-role-cooldown",
|
||||
points=2000,
|
||||
role_available_at=NOW + timedelta(days=2),
|
||||
)
|
||||
task = self.task()
|
||||
monkeypatch.setenv("PLATFORM_FULFILLMENT_MATCH_MODE", "capability")
|
||||
|
||||
detail = service.allocate_task(
|
||||
self.session,
|
||||
task_id=task.id,
|
||||
current=self.admin,
|
||||
)
|
||||
|
||||
self.session.refresh(task)
|
||||
assert detail["task"]["account"] is None
|
||||
assert detail["task"]["issueCode"] == "account_role_cooldown"
|
||||
assert task.next_allocate_at is not None
|
||||
assert task.next_allocate_at >= NOW + timedelta(minutes=4)
|
||||
event = (
|
||||
self.session.query(PlatformFulfillmentEvent)
|
||||
.filter(PlatformFulfillmentEvent.event_type == "account_allocation_failed")
|
||||
.order_by(PlatformFulfillmentEvent.id.desc())
|
||||
.first()
|
||||
)
|
||||
assert event.detail_json["retryReason"] == "account_role_cooldown"
|
||||
assert event.detail_json["availabilityCounts"]["account:eligible"] == 1
|
||||
assert event.detail_json["availabilityCounts"]["account:roleCooldown"] == 1
|
||||
|
||||
def test_hybrid_without_capability_does_not_fall_back_to_live_scan(
|
||||
self,
|
||||
monkeypatch,
|
||||
@@ -2863,6 +2896,66 @@ class TestBindingAndCompletion(_Base):
|
||||
assert task.execution_status == "manual_required"
|
||||
assert task.issue_code == "lease_expired"
|
||||
|
||||
def test_binding_failed_lease_uses_bounded_cleanup_ttl(self):
|
||||
task = self._allocated()
|
||||
account = self.session.get(Account, task.account_id)
|
||||
account.role_bind_status = "unbound"
|
||||
bind_task = DouyuTask(
|
||||
batch_id="bind-timeout-cleanup",
|
||||
account_id=task.account_id,
|
||||
account_assigned_to=self.admin_id,
|
||||
task_type="get_bind_qr",
|
||||
handbook_scope="elite",
|
||||
status="failed",
|
||||
message="绑定二维码已过期,未识别到新扫码角色",
|
||||
result={"bind_phase": "role_timeout"},
|
||||
created_by=self.admin_id,
|
||||
)
|
||||
self.session.add(bind_task)
|
||||
self.session.flush()
|
||||
task.bind_task_id = bind_task.id
|
||||
task.binding_status = "qr_pending"
|
||||
task.execution_status = "waiting_bind"
|
||||
self.session.commit()
|
||||
|
||||
service.refresh_binding(self.session, task_id=task.id)
|
||||
|
||||
lease = self.session.query(AccountLease).one()
|
||||
self.session.refresh(task)
|
||||
assert task.issue_code == "binding_failed"
|
||||
assert lease.expires_at is not None
|
||||
assert lease.expires_at >= datetime.now(UTC) + timedelta(minutes=119)
|
||||
assert lease.expires_at <= datetime.now(UTC) + timedelta(minutes=121)
|
||||
|
||||
def test_reaper_recovers_stale_binding_failed_unbound_lease(self):
|
||||
task = self._allocated()
|
||||
lease = self.session.query(AccountLease).one()
|
||||
account = self.session.get(Account, task.account_id)
|
||||
account.role_bind_status = "unbound"
|
||||
task.execution_status = "manual_required"
|
||||
task.binding_status = "failed"
|
||||
task.issue_code = "binding_failed"
|
||||
task.updated_at = NOW - timedelta(hours=3)
|
||||
lease.expires_at = None
|
||||
self.session.commit()
|
||||
|
||||
job = AutomationJob(key=reaper.JOB_KEY, batch_size=100)
|
||||
result = reaper.run_platform_fulfillment_lease_reaper(
|
||||
self.session,
|
||||
job,
|
||||
now=NOW,
|
||||
)
|
||||
|
||||
self.session.refresh(task)
|
||||
self.session.refresh(lease)
|
||||
assert result["stale_binding_recovered"] == 1
|
||||
assert result["expired"] == 1
|
||||
assert lease.status == "expired"
|
||||
assert task.account_id is None
|
||||
assert task.lease_id is None
|
||||
assert task.bind_task_id is None
|
||||
assert task.issue_code == "lease_expired"
|
||||
|
||||
def test_unlimited_lease_is_ignored_by_reaper(self):
|
||||
task = self._allocated()
|
||||
lease = self.session.query(AccountLease).one()
|
||||
|
||||
Reference in New Issue
Block a user