diff --git a/README.md b/README.md index ac53a75..9f7bc68 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 直播账号运营后台 -斗鱼 / 虎牙账号运营 Web 平台:批量登录拿 Cookie、按工作台跑活动任务、应用宝点券充值。版本见 `pyproject.toml`(当前 0.4.12)。 +斗鱼 / 虎牙账号运营 Web 平台:批量登录拿 Cookie、按工作台跑活动任务、应用宝点券充值。版本见 `pyproject.toml`(当前 0.4.13)。 ## 能做什么 diff --git a/pyproject.toml b/pyproject.toml index 547f392..dd97cd4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "live-hub-py" -version = "0.4.12" +version = "0.4.13" 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 fa389ed..d7d679d 100644 --- a/tests/test_account_capability_refresh_job.py +++ b/tests/test_account_capability_refresh_job.py @@ -17,6 +17,7 @@ from web.backend.models import ( ) from web.backend.services.automation.jobs import account_capability_refresh as job from web.backend.services.automation.scheduler import JobSpec, ensure_jobs, register_job +from web.backend.services.platform_fulfillment.policy import default_allocation_policy NOW = datetime.now(UTC).replace(microsecond=0) @@ -504,6 +505,91 @@ def test_full_queue_still_allows_reserved_global_backfill(db, monkeypatch): assert work.priority == 30 +def test_global_backfill_prefers_known_full_bundle_accounts(db, monkeypatch): + monkeypatch.setenv("ACCOUNT_ORDER_QUEUE_TARGET_PENDING", "1") + monkeypatch.setenv("ACCOUNT_CAPABILITY_BACKFILL_RESERVED_SLOTS", "1") + db.add( + PlatformFulfillmentSetting( + id=1, + allocation_policy_json=default_allocation_policy(), + ) + ) + db.add( + PlatformFulfillmentTask( + source_system="order_site", + source_order_id="bundle-demand", + source_order_item_id="bundle-demand", + internal_sku_key="skin", + commodity_id="20260120QYOOB_bag412", + target_platform="douyu", + scope="elite", + execution_status="ready", + binding_status="not_started", + exchange_status="not_started", + paid_at=NOW, + ) + ) + active = _account(db, "cap-bundle-queue-active") + full = _account(db, "cap-bundle-full") + partial = _account(db, "cap-bundle-partial") + for commodity_id in ( + "20260120QYOOB_bag412", + "20260120QYOOB_bag413", + "20260120QYOOB_bag414", + ): + _state( + db, + full, + commodity_id=commodity_id, + valid_until=NOW - timedelta(minutes=1), + ) + for commodity_id in ( + "20260120QYOOB_bag412", + "20260120QYOOB_bag413", + ): + _state( + db, + partial, + commodity_id=commodity_id, + 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) + + queued_ids = { + int(account_id) + for (account_id,) in ( + db.query(AccountWorkItem.account_id) + .filter( + AccountWorkItem.work_type == "storehome_sync", + AccountWorkItem.priority == 30, + ) + .all() + ) + } + assert result["detail"]["backfill_refresh"] == 1 + assert queued_ids == {full.id} + assert partial.id not in queued_ids + + def test_default_enabled_job_is_created_enabled(db, monkeypatch): from web.backend.services.automation import scheduler diff --git a/uv.lock b/uv.lock index 563b7f4..baf1ec5 100644 --- a/uv.lock +++ b/uv.lock @@ -381,7 +381,7 @@ wheels = [ [[package]] name = "live-hub-py" -version = "0.4.12" +version = "0.4.13" 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 98eb1d1..23c7d86 100644 --- a/web/backend/services/automation/jobs/account_capability_refresh.py +++ b/web/backend/services/automation/jobs/account_capability_refresh.py @@ -31,6 +31,10 @@ from ...platform_fulfillment.account_selector import ( capability_refresh_candidate_accounts, storehome_refresh_candidate_accounts, ) +from ...platform_fulfillment.policy import ( + bundle_requirements_for_commodity, + normalize_allocation_policy, +) JOB_KEY = "account_capability_refresh" DEFAULT_BACKFILL_RESERVED_SLOTS = 8 @@ -95,6 +99,27 @@ def _backfill_reserved_slots() -> int: return DEFAULT_BACKFILL_RESERVED_SLOTS +def _preferred_bundle_groups( + db: Session, + commodity_ids: list[str], +) -> tuple[tuple[str, ...], ...]: + if not commodity_ids: + return () + row = db.get(PlatformFulfillmentSetting, 1) + policy = normalize_allocation_policy( + row.allocation_policy_json if row is not None else None + ) + groups: list[tuple[str, ...]] = [] + for commodity_id in commodity_ids: + required, _ = bundle_requirements_for_commodity(policy, commodity_id) + if not required: + continue + group = tuple(dict.fromkeys((commodity_id, *required))) + if group not in groups: + groups.append(group) + return tuple(groups) + + def _build_select_query( db: Session, job: AutomationJob, @@ -159,6 +184,8 @@ def _refresh_candidates( limit=backfill_limit, now=moment, exclude_account_ids=tuple(demand_ids), + scope="elite", + preferred_commodity_groups=_preferred_bundle_groups(db, commodity_ids), ) if backfill_limit > 0 else [] diff --git a/web/backend/services/platform_fulfillment/account_selector.py b/web/backend/services/platform_fulfillment/account_selector.py index 6d60cc9..1e2d2c3 100644 --- a/web/backend/services/platform_fulfillment/account_selector.py +++ b/web/backend/services/platform_fulfillment/account_selector.py @@ -21,7 +21,7 @@ from ...models import ( AccountLease, AccountSyncState, ) -from ..account_capability_service import OFFER_STATE_ACTIVE +from ..account_capability_service import OFFER_STATE_ACTIVE, OFFER_STATE_BEFORE from ..account_events_service import ( CATEGORY_OPS, EVENT_LEASE_ACQUIRED, @@ -723,6 +723,8 @@ def storehome_refresh_candidate_accounts( limit: int = 20, now: datetime | None = None, exclude_account_ids: tuple[int, ...] | list[int] = (), + scope: str = "elite", + preferred_commodity_groups: tuple[tuple[str, ...], ...] = (), ) -> list[Account]: """选择 CK 有效但缺少新鲜 storehome 快照的账号做全量能力补全。 @@ -745,6 +747,44 @@ def storehome_refresh_candidate_accounts( excluded_ids = tuple( dict.fromkeys(int(account_id) for account_id in exclude_account_ids) ) + preferred_groups = tuple( + dict.fromkeys( + tuple( + dict.fromkeys( + str(commodity_id).strip() + for commodity_id in group + if str(commodity_id).strip() + ) + ) + for group in preferred_commodity_groups + ) + ) + preferred_groups = tuple(group for group in preferred_groups if len(group) >= 2) + preferred_exprs = [ + and_( + *[ + select(AccountCommodityState.id) + .where( + AccountCommodityState.account_id == Account.id, + AccountCommodityState.platform == "douyu", + AccountCommodityState.scope == scope, + AccountCommodityState.commodity_id == commodity_id, + AccountCommodityState.account_eligibility == "eligible", + AccountCommodityState.offer_state.in_( + (OFFER_STATE_ACTIVE, OFFER_STATE_BEFORE) + ), + or_( + AccountCommodityState.last_exchange_at.is_(None), + AccountCommodityState.last_exchange_at + < moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS), + ), + ) + .exists() + for commodity_id in group + ] + ) + for group in preferred_groups + ] query = db.query(Account).filter( ~fresh_storehome, cookie_present_filter(), @@ -755,11 +795,11 @@ def storehome_refresh_candidate_accounts( ) if excluded_ids: query = query.filter(~Account.id.in_(excluded_ids)) - return ( - query.order_by(*_refresh_candidate_order_by(moment)) - .limit(max(1, min(int(limit), 100))) - .all() - ) + order_by: list[Any] = [] + if preferred_exprs: + order_by.append(case((or_(*preferred_exprs), 0), else_=1)) + order_by.extend(_refresh_candidate_order_by(moment)) + return query.order_by(*order_by).limit(max(1, min(int(limit), 100))).all() def has_recent_exchange(