128 lines
3.8 KiB
Python
128 lines
3.8 KiB
Python
"""按直播履约需求把过期商品能力账号送入内部同步队列。
|
|
|
|
本作业只做本地候选筛选,不直接请求官网。真正的 ``storehome`` 查询由
|
|
``account_sync_queue`` 作业在独立执行槽中低并发消费,避免补货查询抢占
|
|
履约实时复核、登录或批量任务的执行资源。
|
|
"""
|
|
|
|
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 ...account_sync_runner import WORK_STOREHOME_SYNC
|
|
from ...account_work_queue import enqueue_account_work
|
|
from ...douyu_service import cookie_operable_filter
|
|
|
|
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()
|
|
for account in accounts:
|
|
enqueue_account_work(
|
|
db,
|
|
account_id=int(account.id),
|
|
work_type=WORK_STOREHOME_SYNC,
|
|
priority=40,
|
|
reason="履约商品能力过期,等待补货刷新",
|
|
due_at=now,
|
|
)
|
|
db.commit()
|
|
queued = len(accounts)
|
|
return {
|
|
"scanned": len(accounts),
|
|
"success": queued,
|
|
"invalid": 0,
|
|
"skipped": 0,
|
|
"message": (
|
|
f"本轮筛选 {len(accounts)} 个到期账号,新入队 {queued} 个"
|
|
if accounts
|
|
else "当前履约商品没有到期待刷新的账号能力"
|
|
),
|
|
"detail": {"queued": queued},
|
|
}
|
|
|
|
|
|
__all__ = [
|
|
"JOB_KEY",
|
|
"build_select_query",
|
|
"run_account_capability_refresh",
|
|
]
|