perf(automation): 持久队列驱动并优化履约热路径

This commit is contained in:
yml
2026-11-23 15:07:31 +08:00
parent 11c2a0dba7
commit 2549511598
31 changed files with 1658 additions and 235 deletions
+74
View File
@@ -11,6 +11,7 @@ from web.backend.models import (
AccountCommodityState,
AccountEvent,
AccountLease,
AccountSupplySnapshot,
AccountSupplyTarget,
AccountSyncRun,
AccountSyncState,
@@ -37,6 +38,8 @@ from web.backend.services.account_supply_service import (
ensure_default_supply_targets,
evaluate_supply_target,
evaluate_supply_targets,
load_supply_snapshot_inventories,
refresh_supply_snapshots,
upsert_supply_target,
)
from web.backend.services.account_work_queue import enqueue_account_work
@@ -122,6 +125,77 @@ def test_supply_inventory_separates_available_and_allocatable(db):
assert inventory.needs_replenishment is True
def test_fresh_supply_snapshot_avoids_live_account_scan(db, monkeypatch):
account = _account("supply-snapshot", points=400)
db.add(account)
db.flush()
db.add_all(
[
_state(account, commodity_id="SNAP"),
AccountSupplyTarget(
platform="douyu",
scope="elite",
commodity_id="SNAP",
value_class=VALUE_HIGH,
target_covered=2,
target_available=2,
target_allocatable=2,
),
]
)
db.commit()
refreshed = refresh_supply_snapshots(db, now=NOW)
assert refreshed[0].covered == 1
assert db.query(AccountSupplySnapshot).count() == 1
monkeypatch.setattr(
"web.backend.services.account_supply_service.evaluate_supply_targets",
lambda *_args, **_kwargs: (_ for _ in ()).throw(
AssertionError("fresh snapshot must not rescan accounts")
),
)
loaded = load_supply_snapshot_inventories(
db,
now=NOW + timedelta(minutes=5),
)
assert (loaded[0].covered, loaded[0].available, loaded[0].allocatable) == (
1,
1,
1,
)
def test_stale_supply_snapshot_is_rebuilt(db):
account = _account("supply-snapshot-stale", points=400)
db.add(account)
db.flush()
db.add_all(
[
_state(account, commodity_id="STALE"),
AccountSupplyTarget(
platform="douyu",
scope="elite",
commodity_id="STALE",
value_class=VALUE_HIGH,
target_covered=1,
target_available=1,
target_allocatable=1,
),
]
)
db.commit()
refresh_supply_snapshots(db, now=NOW)
later = NOW + timedelta(minutes=20)
loaded = load_supply_snapshot_inventories(db, now=later)
snapshot = db.query(AccountSupplySnapshot).one()
assert loaded[0].available == 1
assert snapshot.checked_at == later
def test_supply_allocatable_uses_the_same_pool_owner_filter_as_selection(db):
customer = User(
username="supply-customer",