116 lines
3.5 KiB
Python
116 lines
3.5 KiB
Python
"""按直播履约需求刷新账号商品能力。
|
||
|
||
全量资料补全仍由 ``account_overview_refresh`` 负责;本作业只处理“有 pending
|
||
任务、但对应商品能力快照已经过期”的账号,避免为了秒匹配把两万多个账号
|
||
无条件轮询一遍。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import UTC, datetime
|
||
from typing import Any
|
||
|
||
from sqlalchemy import exists, or_, select
|
||
from sqlalchemy.orm import Session
|
||
|
||
from ....models import (
|
||
Account,
|
||
AccountCommodityState,
|
||
AccountLease,
|
||
AutomationJob,
|
||
PlatformFulfillmentTask,
|
||
)
|
||
from ...account_lifecycle import MANUAL_STATES
|
||
from ...douyu_service import cookie_operable_filter
|
||
from .account_overview_refresh import run_refresh_for_accounts
|
||
|
||
JOB_KEY = "account_capability_refresh"
|
||
|
||
|
||
def _base_query(db: Session, now: datetime):
|
||
pending_commodities = (
|
||
select(PlatformFulfillmentTask.commodity_id)
|
||
.where(
|
||
PlatformFulfillmentTask.execution_status == "ready",
|
||
PlatformFulfillmentTask.account_id.is_(None),
|
||
PlatformFulfillmentTask.commodity_id != "",
|
||
)
|
||
.distinct()
|
||
)
|
||
active_lease = exists(
|
||
select(AccountLease.id).where(
|
||
AccountLease.active_account_id == Account.id,
|
||
)
|
||
)
|
||
# JOIN 比相关 EXISTS 更容易使用商品能力索引。当前生产账号池约 3 万,
|
||
# 原写法会按账号逐个探测商品状态,慢查询实测扫描 40 万行。
|
||
return (
|
||
db.query(Account)
|
||
.join(
|
||
AccountCommodityState,
|
||
AccountCommodityState.account_id == Account.id,
|
||
)
|
||
.filter(
|
||
AccountCommodityState.platform == "douyu",
|
||
AccountCommodityState.scope == "elite",
|
||
AccountCommodityState.commodity_id.in_(pending_commodities),
|
||
or_(
|
||
AccountCommodityState.valid_until.is_(None),
|
||
AccountCommodityState.valid_until <= now,
|
||
AccountCommodityState.next_check_at.is_(None),
|
||
AccountCommodityState.next_check_at <= now,
|
||
),
|
||
cookie_operable_filter(),
|
||
Account.auto_managed.is_(True),
|
||
Account.status.notin_(MANUAL_STATES),
|
||
Account.ck_check_status == "valid",
|
||
~active_lease,
|
||
)
|
||
.distinct()
|
||
)
|
||
|
||
|
||
def build_select_query(
|
||
db: Session,
|
||
job: AutomationJob,
|
||
now: datetime | None = None,
|
||
):
|
||
moment = now or datetime.now(UTC)
|
||
batch = max(1, int(job.batch_size or 100))
|
||
return (
|
||
_base_query(db, moment)
|
||
.order_by(
|
||
Account.overview_queried_at.is_(None).desc(),
|
||
Account.overview_queried_at.asc(),
|
||
Account.id.asc(),
|
||
)
|
||
.limit(batch)
|
||
)
|
||
|
||
|
||
def run_account_capability_refresh(
|
||
db: Session,
|
||
job: AutomationJob,
|
||
) -> dict[str, Any]:
|
||
now = datetime.now(UTC)
|
||
accounts = build_select_query(db, job, now).all()
|
||
return run_refresh_for_accounts(
|
||
db,
|
||
job,
|
||
accounts,
|
||
now=now,
|
||
# 精确剩余量要完整评估 accounts × commodity EXISTS,生产实测单次约
|
||
# 1.3 秒、扫描 38 万行;它只用于展示,直接省略,避免每轮制造慢查询。
|
||
remaining_count=None,
|
||
source="capability_demand_refresh",
|
||
log_label="capability",
|
||
empty_message="当前履约商品没有到期待刷新的账号能力",
|
||
)
|
||
|
||
|
||
__all__ = [
|
||
"JOB_KEY",
|
||
"build_select_query",
|
||
"run_account_capability_refresh",
|
||
]
|