diff --git a/README.md b/README.md index 539860f..72821cc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 直播账号运营后台 -斗鱼 / 虎牙账号运营 Web 平台:批量登录拿 Cookie、按工作台跑活动任务、应用宝点券充值。版本见 `pyproject.toml`(当前 0.4.10)。 +斗鱼 / 虎牙账号运营 Web 平台:批量登录拿 Cookie、按工作台跑活动任务、应用宝点券充值。版本见 `pyproject.toml`(当前 0.4.11)。 ## 能做什么 diff --git a/pyproject.toml b/pyproject.toml index 56820a9..6876741 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "live-hub-py" -version = "0.4.10" +version = "0.4.11" description = "直播账号运营 Web 后台" readme = "README.md" requires-python = ">=3.12,<3.13" diff --git a/tests/test_account_capability_refresh_job.py b/tests/test_account_capability_refresh_job.py index c2d8473..099daa3 100644 --- a/tests/test_account_capability_refresh_job.py +++ b/tests/test_account_capability_refresh_job.py @@ -194,6 +194,55 @@ def test_demand_refresh_runs_before_global_backfill(db): assert [row.id for row in selected] == [demand.id, backfill.id] +def test_busy_demand_keeps_reserved_backfill_slots(db, monkeypatch): + monkeypatch.setenv("ACCOUNT_ORDER_QUEUE_TARGET_PENDING", "4") + db.add( + PlatformFulfillmentTask( + source_system="order_site", + source_order_id="reserve-demand", + source_order_item_id="reserve-demand", + internal_sku_key="skin", + commodity_id="C", + execution_status="ready", + binding_status="not_started", + exchange_status="not_started", + paid_at=NOW, + ) + ) + demand_accounts = [_account(db, f"cap-demand-{index}") for index in range(5)] + backfill = _account(db, "cap-reserved-backfill") + for account in demand_accounts: + _state(db, account, commodity_id="C", valid_until=NOW - timedelta(minutes=1)) + _state( + db, + backfill, + commodity_id="OTHER", + valid_until=NOW - timedelta(minutes=1), + ) + db.commit() + automation_job = AutomationJob( + key=job.JOB_KEY, + title="履约商品能力补充刷新", + interval_seconds=3600, + batch_size=4, + concurrency=3, + ) + + result = job.run_account_capability_refresh(db, automation_job) + + assert result["scanned"] == 4 + assert result["detail"]["demand_refresh"] == 3 + assert result["detail"]["backfill_refresh"] == 1 + works = ( + db.query(AccountWorkItem) + .filter(AccountWorkItem.work_type == "storehome_sync") + .order_by(AccountWorkItem.priority.asc()) + .all() + ) + assert len([work for work in works if work.priority == 10]) == 3 + assert len([work for work in works if work.priority == 30]) == 1 + + def test_refresh_ignores_tasks_before_match_cutoff(db): db.add( PlatformFulfillmentSetting( diff --git a/uv.lock b/uv.lock index a34ff59..d949fd7 100644 --- a/uv.lock +++ b/uv.lock @@ -381,7 +381,7 @@ wheels = [ [[package]] name = "live-hub-py" -version = "0.4.10" +version = "0.4.11" source = { editable = "." } dependencies = [ { name = "alembic" }, diff --git a/web/backend/services/automation/jobs/account_capability_refresh.py b/web/backend/services/automation/jobs/account_capability_refresh.py index b91f9a4..a0678e7 100644 --- a/web/backend/services/automation/jobs/account_capability_refresh.py +++ b/web/backend/services/automation/jobs/account_capability_refresh.py @@ -33,6 +33,7 @@ from ...platform_fulfillment.account_selector import ( ) JOB_KEY = "account_capability_refresh" +DEFAULT_BACKFILL_RESERVED_SLOTS = 8 def _pending_demand_rows( @@ -125,27 +126,30 @@ def _refresh_candidates( 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, + limit=batch - backfill_reserved, now=moment, ) - if commodity_ids + if commodity_ids and batch > backfill_reserved else [] ) demand_ids = {int(account.id) for account in demand_candidates} - remaining = batch - len(demand_candidates) backfill_candidates = ( storehome_refresh_candidate_accounts( db, - limit=remaining, + limit=batch - len(demand_candidates), now=moment, exclude_account_ids=tuple(demand_ids), ) - if remaining > 0 + if batch > len(demand_candidates) else [] ) accounts = [*demand_candidates, *backfill_candidates]