fix(douyu): 补全账号能力目录并回收失效租约

This commit is contained in:
yml
2026-11-22 20:36:40 +08:00
parent 1906f924bc
commit af05c54c3f
18 changed files with 839 additions and 121 deletions
+93
View File
@@ -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()