feat(douyu): 建立账号同步队列与供给库存

This commit is contained in:
yml
2026-11-21 21:48:51 +08:00
parent 1d262cdfd9
commit 3baf15afda
16 changed files with 1158 additions and 33 deletions
+12 -13
View File
@@ -8,6 +8,7 @@ from web.backend.models import (
Account,
AccountCommodityState,
AccountLease,
AccountWorkItem,
AutomationJob,
PlatformFulfillmentTask,
User,
@@ -124,7 +125,7 @@ def test_no_pending_task_means_no_refresh(db):
assert job.build_select_query(db, automation_job, NOW).all() == []
def test_run_does_not_execute_exact_remaining_count(db, monkeypatch):
def test_run_enqueues_stale_accounts_without_external_query(db):
db.add(
PlatformFulfillmentTask(
source_system="order_site",
@@ -148,19 +149,17 @@ def test_run_does_not_execute_exact_remaining_count(db, monkeypatch):
batch_size=10,
concurrency=3,
)
captured: dict[str, object] = {}
result = job.run_account_capability_refresh(db, automation_job)
def fake_run_refresh(_db, _job, accounts, **kwargs):
captured["accounts"] = [row.id for row in accounts]
captured["remaining_count"] = kwargs.get("remaining_count")
return {"scanned": len(accounts)}
monkeypatch.setattr(job, "run_refresh_for_accounts", fake_run_refresh)
job.run_account_capability_refresh(db, automation_job)
assert captured["accounts"] == [stale.id]
assert captured["remaining_count"] is None
work = (
db.query(AccountWorkItem)
.filter_by(account_id=stale.id, work_type="storehome_sync")
.one()
)
assert result["scanned"] == 1
assert result["success"] == 1
assert work.status == "pending"
assert work.priority == 40
def test_default_enabled_job_is_created_enabled(db, monkeypatch):
+124
View File
@@ -0,0 +1,124 @@
"""账号供给库存与定向补货测试。"""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from web.backend.models import (
Account,
AccountCommodityState,
AccountLease,
AccountSupplyTarget,
AccountWorkItem,
User,
)
from web.backend.services.account_supply_service import (
VALUE_HIGH,
enqueue_supply_replenishment,
evaluate_supply_target,
)
NOW = datetime.now(UTC).replace(microsecond=0)
def _account(name: str, *, points: int = 0) -> Account:
return Account(
username=name,
password="p",
email=f"{name}@example.com",
email_password="ep",
cookie="acf_uid=100",
cookie_updated_at=NOW,
ck_check_status="valid",
ck_checked_at=NOW,
status="active",
auto_managed=True,
points=points,
)
def _state(account: Account, commodity_id: str = "C") -> AccountCommodityState:
return AccountCommodityState(
account_id=account.id,
platform="douyu",
scope="elite",
commodity_id=commodity_id,
availability="available",
observed_at=NOW,
valid_until=NOW + timedelta(hours=1),
available_from=NOW - timedelta(hours=1),
available_until=NOW + timedelta(hours=1),
)
def test_supply_inventory_separates_available_and_allocatable(db):
user = User(
username="supply-owner",
password_hash="h",
role="super_admin",
is_active=True,
)
first = _account("supply-free", points=400)
leased = _account("supply-leased", points=400)
db.add_all([user, first, leased])
db.flush()
db.add_all(
[
_state(first),
_state(leased),
AccountLease(
account_id=leased.id,
holder_user_id=user.id,
scope="elite",
purpose="platform_fulfillment",
status="active",
),
AccountSupplyTarget(
platform="douyu",
scope="elite",
commodity_id="C",
value_class=VALUE_HIGH,
target_covered=2,
target_available=2,
target_allocatable=2,
),
]
)
db.commit()
target = db.query(AccountSupplyTarget).one()
inventory = evaluate_supply_target(db, target, now=NOW)
assert inventory.covered == 2
assert inventory.available == 2
assert inventory.allocatable == 1
assert inventory.allocatable_shortage == 1
assert inventory.needs_replenishment is True
def test_supply_shortage_enqueues_missing_coverage(db):
account = _account("supply-missing", points=400)
db.add(account)
db.flush()
db.add(
AccountSupplyTarget(
platform="douyu",
scope="elite",
commodity_id="C",
value_class=VALUE_HIGH,
target_covered=1,
target_available=1,
target_allocatable=0,
)
)
db.commit()
result = enqueue_supply_replenishment(db, now=NOW, max_per_target=10)
work = db.query(AccountWorkItem).one()
assert result.targets == 1
assert result.shortages == 1
assert result.queued == 1
assert work.account_id == account.id
assert work.work_type == "storehome_sync"
assert work.priority == 35
+219
View File
@@ -0,0 +1,219 @@
"""账号同步队列执行器测试。"""
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from tests.support.db import make_test_session
from web.backend.models import (
Account,
AccountCommodityState,
AccountSyncState,
AccountWorkItem,
DouyuConfig,
)
from web.backend.services.account_sync_runner import (
WORK_STOREHOME_SYNC,
execute_account_work_item,
run_account_sync_queue,
)
from web.backend.services.account_work_queue import (
claim_account_work,
enqueue_account_work,
recover_running_account_work,
)
from web.backend.services.douyu_read_gateway import StorehomeSnapshot
def _config(db) -> None:
db.add(
DouyuConfig(
manual_id="manual",
rid="room",
)
)
db.commit()
def _account(name: str = "sync-account") -> Account:
return Account(
username=name,
password="p",
email=f"{name}@example.com",
email_password="ep",
cookie="acf_uid=100",
cookie_updated_at=datetime.now(UTC),
ck_check_status="valid",
status="active",
auto_managed=True,
)
def _snapshot() -> StorehomeSnapshot:
items = (
{
"commodityId": "limited",
"commodityName": "限兑商品",
"status": 1,
},
{
"commodityId": "available",
"commodityName": "可兑商品",
"status": 0,
"startTime": str(int((datetime.now(UTC) - timedelta(hours=1)).timestamp())),
"endTime": str(int((datetime.now(UTC) + timedelta(hours=1)).timestamp())),
"timeLimited": 1,
},
)
return StorehomeSnapshot(
manual_id="manual",
rid="room",
observed_at=datetime.now(UTC),
items=items,
limited_ids=frozenset({"limited"}),
raw={"data": {"list": list(items)}},
)
def test_execute_work_item_syncs_all_goods_and_completes(db):
_config(db)
account = _account()
db.add(account)
db.commit()
work = enqueue_account_work(
db,
account_id=account.id,
work_type=WORK_STOREHOME_SYNC,
)
db.commit()
claimed = claim_account_work(
db,
work_types=(WORK_STOREHOME_SYNC,),
limit=1,
)
db.commit()
assert [item.id for item in claimed] == [work.id]
assert claimed[0].attempt_count == 1
db.commit()
result = execute_account_work_item(
work.id,
session_factory=make_test_session,
client_factory=lambda _cookie: object(),
snapshot_fetcher=lambda *_args, **_kwargs: _snapshot(),
)
with make_test_session() as verify:
work_row = verify.get(AccountWorkItem, work.id)
states = (
verify.query(AccountCommodityState)
.filter(AccountCommodityState.account_id == account.id)
.all()
)
sync_state = (
verify.query(AccountSyncState)
.filter_by(account_id=account.id, domain="storehome")
.one()
)
assert result["ok"] is True
assert result["item_count"] == 2
assert work_row.status == "success"
assert len(states) == 2
assert sync_state.status == "success"
def test_work_item_failure_returns_to_pending_for_retry(db):
_config(db)
account = _account("sync-retry")
db.add(account)
db.commit()
work = enqueue_account_work(
db,
account_id=account.id,
work_type=WORK_STOREHOME_SYNC,
)
db.commit()
claimed = claim_account_work(
db,
work_types=(WORK_STOREHOME_SYNC,),
limit=1,
)
db.commit()
assert claimed[0].attempt_count == 1
db.commit()
def fail(*_args, **_kwargs):
raise RuntimeError("network unavailable")
result = execute_account_work_item(
work.id,
session_factory=make_test_session,
client_factory=lambda _cookie: object(),
snapshot_fetcher=fail,
)
with make_test_session() as verify:
work_row = verify.get(AccountWorkItem, work.id)
assert result["ok"] is False
assert work_row.status == "pending"
assert work_row.attempt_count == 1
assert "network unavailable" in work_row.last_error
assert work_row.due_at > datetime.now(UTC)
def test_run_account_sync_queue_claims_and_executes(db):
_config(db)
account = _account("sync-runner")
db.add(account)
db.commit()
enqueue_account_work(
db,
account_id=account.id,
work_type=WORK_STOREHOME_SYNC,
)
db.commit()
result = run_account_sync_queue(
db,
batch_size=5,
session_factory=make_test_session,
client_factory=lambda _cookie: object(),
snapshot_fetcher=lambda *_args, **_kwargs: _snapshot(),
)
with make_test_session() as verify:
work = (
verify.query(AccountWorkItem)
.filter_by(account_id=account.id, work_type=WORK_STOREHOME_SYNC)
.one()
)
assert result["scanned"] == 1
assert result["success"] == 1
assert work.status == "success"
def test_restart_recovers_running_work(db):
account = _account("sync-recovery")
db.add(account)
db.commit()
work = enqueue_account_work(
db,
account_id=account.id,
work_type=WORK_STOREHOME_SYNC,
)
db.commit()
claim_account_work(
db,
work_types=(WORK_STOREHOME_SYNC,),
limit=1,
)
db.commit()
recovered = recover_running_account_work(db)
with make_test_session() as verify:
row = verify.get(AccountWorkItem, work.id)
assert recovered == 1
assert row.status == "pending"
assert row.due_at <= datetime.now(UTC)
assert "服务重启" in row.last_error
+2 -1
View File
@@ -408,7 +408,8 @@ class TestScheduler(_Base):
self.session.commit()
outcomes = run_due_jobs(self.session)
assert outcomes[0]["status"] == "failed"
failed = next(item for item in outcomes if item["job_key"] == "boom")
assert failed["status"] == "failed"
run = (
self.session.query(AutomationRun)
.filter(AutomationRun.job_key == "boom")