feat(douyu): 接入重登工作队列与平台状态观测

This commit is contained in:
yml
2026-11-21 21:58:31 +08:00
parent e178a393e4
commit 3b2d4f89fc
8 changed files with 456 additions and 5 deletions
+68
View File
@@ -21,8 +21,11 @@ from web.backend.services.account_sync_service import (
sync_storehome_snapshot,
)
from web.backend.services.account_work_queue import (
claim_account_work,
complete_account_work,
enqueue_account_work,
fail_account_work_type,
reschedule_account_work_type,
)
from web.backend.services.automation.jobs import ck_health
from web.backend.services.douyu_account_state import DouyuLiveAccountState
@@ -175,6 +178,71 @@ def test_account_work_queue_deduplicates_only_active_items(db):
assert db.query(AccountWorkItem).count() == 2
def test_account_work_claim_can_be_scoped_for_relogin(db):
first = _account("relogin-scope-a")
second = _account("relogin-scope-b")
db.add_all([first, second])
db.commit()
for account in (first, second):
enqueue_account_work(
db,
account_id=account.id,
work_type="login_relogin",
max_attempts=20,
)
db.commit()
claimed = claim_account_work(
db,
work_types=("login_relogin",),
account_ids=(first.id,),
limit=10,
)
assert [item.account_id for item in claimed] == [first.id]
assert claimed[0].attempt_count == 1
assert claimed[0].max_attempts == 20
def test_relogin_work_reschedules_then_closes_for_manual(db):
account = _account("relogin-work-state")
db.add(account)
db.commit()
enqueue_account_work(
db,
account_id=account.id,
work_type="login_relogin",
max_attempts=20,
)
db.commit()
item = claim_account_work(
db,
work_types=("login_relogin",),
account_ids=(account.id,),
limit=1,
)[0]
retry_at = datetime.now(UTC) + timedelta(hours=1)
reschedule_account_work_type(
db,
account_id=account.id,
work_type="login_relogin",
due_at=retry_at,
reason="验证码失败",
)
assert item.status == "pending"
assert item.due_at == retry_at.replace(microsecond=0)
fail_account_work_type(
db,
account_id=account.id,
work_type="login_relogin",
reason="转人工",
)
assert item.status == "failed"
assert item.last_error == "转人工"
def test_ck_verdict_updates_lifecycle_and_moves_relogin_work(db):
issued_at = datetime.now(UTC) - timedelta(days=7, hours=9)
account = _account("ck-lifecycle", cookie_updated_at=issued_at)
+47
View File
@@ -9,17 +9,20 @@ from web.backend.models import (
AccountCommodityState,
AccountLease,
AccountSupplyTarget,
AccountSyncState,
AccountWorkItem,
User,
)
from web.backend.routers.douyu import create_supply_target, list_supply_targets
from web.backend.schemas import DouyuSupplyTargetUpsert
from web.backend.services.account_observability import account_platform_status
from web.backend.services.account_supply_service import (
VALUE_HIGH,
enqueue_supply_replenishment,
evaluate_supply_target,
upsert_supply_target,
)
from web.backend.services.account_work_queue import enqueue_account_work
NOW = datetime.now(UTC).replace(microsecond=0)
@@ -191,3 +194,47 @@ def test_supply_target_configuration_route_returns_inventory(db):
assert created.covered_shortage == 12
assert listed[0].id == created.id
assert listed[0].needs_replenishment is True
def test_account_platform_status_aggregates_queue_sync_and_supply(db):
account = _account("platform-status", points=400)
account.ck_expected_expires_at = NOW + timedelta(hours=12)
db.add(account)
db.flush()
db.add_all(
[
_state(account),
AccountSyncState(
account_id=account.id,
platform="douyu",
domain="storehome",
status="success",
last_success_at=NOW,
valid_until=NOW + timedelta(hours=1),
),
AccountSupplyTarget(
platform="douyu",
scope="elite",
commodity_id="C",
value_class=VALUE_HIGH,
target_covered=2,
target_available=2,
target_allocatable=1,
),
]
)
enqueue_account_work(
db,
account_id=account.id,
work_type="login_relogin",
)
db.commit()
status = account_platform_status(db, now=NOW)
assert status["accounts"]["ck_valid"] == 1
assert status["accounts"]["ck_expiring_24h"] == 1
assert status["work_queue"]["login_relogin"]["pending"] == 1
assert status["sync_domains"]["storehome"]["fresh"] == 1
assert status["commodities"]["fresh_available"] == 1
assert status["supply_targets"][0]["covered_shortage"] == 1