fix(automation): 修复账号供给补货队列饥饿
- 拆分订单补货和通用供给水位,供给保留独立 pending 配额 - 每批固定供给槽位,避免订单补货占满队列 - 只补覆盖/可兑换缺口,可分配缺口不再无效入队 - 重登工作超过 24h 自动转人工,清理待处理积压 - 补货阻塞告警和队列诊断信息 - 增加供给公平调度/过期清理回归测试
This commit is contained in:
@@ -42,7 +42,11 @@ from web.backend.services.account_supply_service import (
|
||||
refresh_supply_snapshots,
|
||||
upsert_supply_target,
|
||||
)
|
||||
from web.backend.services.account_work_queue import enqueue_account_work
|
||||
from web.backend.services.account_work_queue import (
|
||||
ORDER_REFRESH_REASON_PREFIX,
|
||||
SUPPLY_REASON_PREFIX,
|
||||
enqueue_account_work,
|
||||
)
|
||||
|
||||
NOW = datetime.now(UTC).replace(microsecond=0)
|
||||
|
||||
@@ -811,3 +815,75 @@ def test_account_platform_status_alerts_on_persistently_low_trends(db):
|
||||
"login_success_rate_low",
|
||||
"relogin_failure_trend_high",
|
||||
} <= codes
|
||||
|
||||
|
||||
def test_supply_replenishment_queues_when_order_queue_is_busy(db):
|
||||
# 60 个订单补货工作把旧版“总待处理 60 - 供给水位 20”占满;
|
||||
# 新版供给有独立水位,仍应至少排入一个账号。
|
||||
accounts = [_account(f"supply-busy-{index}", points=400) for index in range(65)]
|
||||
db.add_all(accounts)
|
||||
db.flush()
|
||||
for account in accounts[:60]:
|
||||
enqueue_account_work(
|
||||
db,
|
||||
account_id=account.id,
|
||||
work_type="storehome_sync",
|
||||
priority=10,
|
||||
reason=f"{ORDER_REFRESH_REASON_PREFIX} C",
|
||||
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=0,
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
result = enqueue_supply_replenishment(db, now=NOW, max_per_target=10)
|
||||
|
||||
assert result.pending_sync == 60
|
||||
assert result.pending_supply == 0
|
||||
assert result.total_cap == 120
|
||||
assert result.supply_cap == 50
|
||||
assert result.queued == 1
|
||||
supply_work = (
|
||||
db.query(AccountWorkItem)
|
||||
.filter(AccountWorkItem.reason.like(f"{SUPPLY_REASON_PREFIX}%"))
|
||||
.one()
|
||||
)
|
||||
assert supply_work.account_id in {account.id for account in accounts[60:]}
|
||||
|
||||
|
||||
def test_supply_replenishment_skips_allocatable_only_shortage(db):
|
||||
account = _account("supply-allocatable-only", points=400)
|
||||
db.add(account)
|
||||
db.flush()
|
||||
db.add_all(
|
||||
[
|
||||
_state(account),
|
||||
AccountSupplyTarget(
|
||||
platform="douyu",
|
||||
scope="elite",
|
||||
commodity_id="C",
|
||||
value_class=VALUE_HIGH,
|
||||
target_covered=1,
|
||||
target_available=1,
|
||||
target_allocatable=2,
|
||||
),
|
||||
]
|
||||
)
|
||||
db.commit()
|
||||
|
||||
result = enqueue_supply_replenishment(db, now=NOW, max_per_target=10)
|
||||
|
||||
assert result.shortages == 1
|
||||
assert result.refreshable_shortages == 0
|
||||
assert result.capacity_shortages == 1
|
||||
assert result.queued == 0
|
||||
assert db.query(AccountWorkItem).count() == 0
|
||||
|
||||
@@ -17,12 +17,16 @@ from web.backend.services.account_sync_runner import (
|
||||
WORK_ESPORTS_SYNC,
|
||||
WORK_EXCHANGE_RECORDS_SYNC,
|
||||
WORK_STOREHOME_SYNC,
|
||||
claim_sync_work_batch,
|
||||
execute_account_work_item,
|
||||
run_account_sync_queue,
|
||||
)
|
||||
from web.backend.services.account_work_queue import (
|
||||
ORDER_REFRESH_REASON_PREFIX,
|
||||
SUPPLY_REASON_PREFIX,
|
||||
claim_account_work,
|
||||
enqueue_account_work,
|
||||
expire_stale_login_work,
|
||||
recover_running_account_work,
|
||||
)
|
||||
from web.backend.services.douyu_read_gateway import StorehomeSnapshot
|
||||
@@ -337,3 +341,65 @@ def test_restart_recovers_running_work(db):
|
||||
assert row.status == "pending"
|
||||
assert row.due_at <= datetime.now(UTC)
|
||||
assert "服务重启" in row.last_error
|
||||
|
||||
|
||||
def test_claim_sync_work_batch_reserves_supply_quota(db, monkeypatch):
|
||||
monkeypatch.setenv("ACCOUNT_SUPPLY_QUEUE_QUOTA", "5")
|
||||
moment = datetime.now(UTC).replace(microsecond=0)
|
||||
accounts = [_account(f"quota-{index}") for index in range(25)]
|
||||
db.add_all(accounts)
|
||||
db.flush()
|
||||
for account in accounts[:20]:
|
||||
enqueue_account_work(
|
||||
db,
|
||||
account_id=account.id,
|
||||
work_type=WORK_STOREHOME_SYNC,
|
||||
priority=10,
|
||||
reason=f"{ORDER_REFRESH_REASON_PREFIX} C",
|
||||
due_at=moment,
|
||||
)
|
||||
for account in accounts[20:]:
|
||||
enqueue_account_work(
|
||||
db,
|
||||
account_id=account.id,
|
||||
work_type=WORK_STOREHOME_SYNC,
|
||||
priority=35,
|
||||
reason=f"{SUPPLY_REASON_PREFIX} elite/all/high_value",
|
||||
due_at=moment,
|
||||
)
|
||||
db.commit()
|
||||
|
||||
supply, normal = claim_sync_work_batch(db, limit=20, now=moment)
|
||||
|
||||
assert len(supply) == 5
|
||||
assert len(normal) == 15
|
||||
assert all(
|
||||
item.reason.startswith(SUPPLY_REASON_PREFIX) for item in supply
|
||||
)
|
||||
assert all(
|
||||
not item.reason.startswith(SUPPLY_REASON_PREFIX) for item in normal
|
||||
)
|
||||
|
||||
|
||||
def test_expire_stale_login_work(db):
|
||||
moment = datetime.now(UTC).replace(microsecond=0)
|
||||
account = _account("relogin-stale")
|
||||
db.add(account)
|
||||
db.flush()
|
||||
item = enqueue_account_work(
|
||||
db,
|
||||
account_id=account.id,
|
||||
work_type="login_relogin",
|
||||
priority=30,
|
||||
due_at=moment - timedelta(days=2),
|
||||
max_attempts=20,
|
||||
reason="CK 已判定失效,等待受预算控制的重登",
|
||||
)
|
||||
db.commit()
|
||||
|
||||
expired = expire_stale_login_work(db, now=moment, ttl_seconds=86400)
|
||||
|
||||
db.refresh(item)
|
||||
assert expired == 1
|
||||
assert item.status == "failed"
|
||||
assert "已转人工" in item.last_error
|
||||
|
||||
Reference in New Issue
Block a user