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
+46 -7
View File
@@ -27,9 +27,13 @@ from .account_query_filters import (
role_change_ready_filter,
valid_ck_filter,
)
from .account_sync_runner import WORK_STOREHOME_SYNC
from .account_sync_runner import ACCOUNT_SYNC_WORK_TYPES, WORK_STOREHOME_SYNC
from .account_value import is_high_value_expr
from .account_work_queue import enqueue_account_work
from .account_work_queue import (
active_account_work_count,
active_account_work_exists,
enqueue_account_work,
)
VALUE_HIGH = "high_value"
VALUE_ORDINARY = "ordinary"
@@ -523,13 +527,40 @@ def _sync_candidate_query(
)
.exists()
)
return query.filter(~state_exists).order_by(
Account.overview_queried_at.is_(None).desc(),
Account.overview_queried_at.asc(),
Account.id.asc(),
return (
query.filter(
~state_exists,
~active_account_work_exists(work_type=WORK_STOREHOME_SYNC),
assigned_to_pool_filter(),
)
.order_by(
case((active_lease_exists(), 1), else_=0),
case((active_collab_order_exists(), 1), else_=0),
case((role_change_ready_filter(now), 0), else_=1),
case((Account.role_bind_status == "unbound", 0), else_=1),
Account.overview_queried_at.is_(None).desc(),
Account.overview_queried_at.asc(),
Account.id.asc(),
)
)
def _account_sync_queue_target_pending() -> int:
raw = os.getenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "60")
try:
total = max(1, int(raw))
except ValueError:
total = 60
# 通用供给补货只占低水位;剩余水位留给有明确订单的履约商品,
# 防止注册顺序靠前的普通补货把待分配订单的紧急刷新挤到下一轮。
raw_supply = os.getenv("ACCOUNT_SUPPLY_QUEUE_TARGET_PENDING", "20")
try:
supply = max(1, int(raw_supply))
except ValueError:
supply = 20
return min(total, supply)
def enqueue_supply_replenishment(
db: Session,
*,
@@ -541,10 +572,17 @@ def enqueue_supply_replenishment(
inventories = evaluate_supply_targets(db, now=moment)
queued = 0
shortages = 0
pending = active_account_work_count(
db,
work_types=ACCOUNT_SYNC_WORK_TYPES,
)
headroom = max(0, _account_sync_queue_target_pending() - pending)
for inventory in inventories:
if not inventory.needs_replenishment:
continue
shortages += 1
if headroom <= 0:
continue
target = db.get(AccountSupplyTarget, inventory.target_id)
if target is None:
continue
@@ -553,7 +591,7 @@ def enqueue_supply_replenishment(
inventory.available_shortage,
inventory.allocatable_shortage,
)
limit = max(1, min(int(max_per_target), missing))
limit = max(1, min(int(max_per_target), missing, headroom))
accounts = (
_sync_candidate_query(
db,
@@ -576,6 +614,7 @@ def enqueue_supply_replenishment(
),
)
queued += 1
headroom -= 1
if queued:
db.commit()
return SupplyReplenishmentResult(