fix(fulfillment): 队列满载时仍保留全局能力补扫

This commit is contained in:
yml
2026-11-28 21:09:29 +08:00
parent 371027d730
commit 5d54954762
5 changed files with 67 additions and 25 deletions
@@ -82,6 +82,19 @@ def _order_queue_target_pending() -> int:
return 70
def _backfill_reserved_slots() -> int:
import os
raw = os.getenv(
"ACCOUNT_CAPABILITY_BACKFILL_RESERVED_SLOTS",
str(DEFAULT_BACKFILL_RESERVED_SLOTS),
)
try:
return max(0, min(int(raw), 100))
except ValueError:
return DEFAULT_BACKFILL_RESERVED_SLOTS
def _build_select_query(
db: Session,
job: AutomationJob,
@@ -115,41 +128,39 @@ def _refresh_candidates(
moment: datetime,
*,
batch_limit: int | None = None,
backfill_floor: int = 0,
) -> tuple[list[Account], set[int], list[str]]:
"""先补待履约商品,再用剩余水位补全其他 CK 有效账号。"""
demand_rows = _pending_demand_rows(db, moment)
commodity_ids = [str(row[0]) for row in demand_rows if row[0]]
batch = max(
1,
0,
min(
int(batch_limit if batch_limit is not None else job.batch_size or 100),
100,
),
)
backfill_reserved = min(
DEFAULT_BACKFILL_RESERVED_SLOTS,
max(0, batch // 4),
)
demand_candidates = (
capability_refresh_candidate_accounts(
db,
commodity_ids=commodity_ids,
scope="elite",
limit=batch - backfill_reserved,
limit=batch,
now=moment,
)
if commodity_ids and batch > backfill_reserved
if commodity_ids and batch > 0
else []
)
demand_ids = {int(account.id) for account in demand_candidates}
backfill_limit = max(backfill_floor, batch - len(demand_candidates))
backfill_candidates = (
storehome_refresh_candidate_accounts(
db,
limit=batch - len(demand_candidates),
limit=backfill_limit,
now=moment,
exclude_account_ids=tuple(demand_ids),
)
if batch > len(demand_candidates)
if backfill_limit > 0
else []
)
accounts = [*demand_candidates, *backfill_candidates]
@@ -175,23 +186,13 @@ def run_account_capability_refresh(
exclude_reason_prefix=SUPPLY_REASON_PREFIX,
)
headroom = max(0, _order_queue_target_pending() - pending)
if headroom <= 0:
return {
"scanned": 0,
"success": 0,
"invalid": 0,
"skipped": pending,
"message": (
f"账号同步队列仍有 {pending} 条待处理,本轮不继续补货,"
"先交给独立同步槽消化"
),
"detail": {"queued": 0, "pending": pending, "headroom": 0},
}
backfill_reserved = _backfill_reserved_slots()
accounts, demand_ids, commodity_ids = _refresh_candidates(
db,
job,
now,
batch_limit=headroom,
backfill_floor=backfill_reserved,
)
for account in accounts:
is_demand_refresh = int(account.id) in demand_ids
@@ -224,6 +225,7 @@ def run_account_capability_refresh(
"pending": pending,
"order_cap": _order_queue_target_pending(),
"headroom": headroom,
"backfill_reserved": backfill_reserved,
"commodities": commodity_ids[:20],
"demand_refresh": len(demand_ids),
"backfill_refresh": max(0, queued - len(demand_ids)),