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
+1 -1
View File
@@ -1,6 +1,6 @@
# 直播账号运营后台
斗鱼 / 虎牙账号运营 Web 平台:批量登录拿 Cookie、按工作台跑活动任务、应用宝点券充值。版本见 `pyproject.toml`(当前 0.4.11)。
斗鱼 / 虎牙账号运营 Web 平台:批量登录拿 Cookie、按工作台跑活动任务、应用宝点券充值。版本见 `pyproject.toml`(当前 0.4.12)。
## 能做什么
+1 -1
View File
@@ -1,6 +1,6 @@
[project]
name = "live-hub-py"
version = "0.4.11"
version = "0.4.12"
description = "直播账号运营 Web 后台"
readme = "README.md"
requires-python = ">=3.12,<3.13"
+41 -1
View File
@@ -195,7 +195,8 @@ def test_demand_refresh_runs_before_global_backfill(db):
def test_busy_demand_keeps_reserved_backfill_slots(db, monkeypatch):
monkeypatch.setenv("ACCOUNT_ORDER_QUEUE_TARGET_PENDING", "4")
monkeypatch.setenv("ACCOUNT_ORDER_QUEUE_TARGET_PENDING", "3")
monkeypatch.setenv("ACCOUNT_CAPABILITY_BACKFILL_RESERVED_SLOTS", "1")
db.add(
PlatformFulfillmentTask(
source_system="order_site",
@@ -464,6 +465,45 @@ def test_refresh_stops_at_queue_target(db, monkeypatch):
assert db.query(AccountWorkItem).count() == 1
def test_full_queue_still_allows_reserved_global_backfill(db, monkeypatch):
monkeypatch.setenv("ACCOUNT_ORDER_QUEUE_TARGET_PENDING", "1")
monkeypatch.setenv("ACCOUNT_CAPABILITY_BACKFILL_RESERVED_SLOTS", "1")
active = _account(db, "cap-full-queue-active")
backfill = _account(db, "cap-full-queue-backfill")
_state(db, active, valid_until=NOW - timedelta(minutes=1))
_state(db, backfill, commodity_id="OTHER", valid_until=NOW - timedelta(minutes=1))
db.add(
AccountWorkItem(
account_id=active.id,
work_type="storehome_sync",
priority=10,
status="pending",
due_at=NOW,
dedupe_key=f"storehome_sync:{active.id}",
)
)
db.commit()
automation_job = AutomationJob(
key=job.JOB_KEY,
title="履约商品能力补充刷新",
interval_seconds=3600,
batch_size=10,
concurrency=3,
)
result = job.run_account_capability_refresh(db, automation_job)
work = (
db.query(AccountWorkItem)
.filter_by(account_id=backfill.id, work_type="storehome_sync")
.one()
)
assert result["scanned"] == 1
assert result["detail"]["headroom"] == 0
assert result["detail"]["backfill_reserved"] == 1
assert work.priority == 30
def test_default_enabled_job_is_created_enabled(db, monkeypatch):
from web.backend.services.automation import scheduler
Generated
+1 -1
View File
@@ -381,7 +381,7 @@ wheels = [
[[package]]
name = "live-hub-py"
version = "0.4.11"
version = "0.4.12"
source = { editable = "." }
dependencies = [
{ name = "alembic" },
@@ -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)),