193 lines
5.4 KiB
Python
193 lines
5.4 KiB
Python
"""按直播履约需求把过期商品能力账号送入内部同步队列。
|
|
|
|
本作业只做本地候选筛选,不直接请求官网。真正的 ``storehome`` 查询由
|
|
``account_sync_queue`` 作业在独立执行槽中低并发消费,避免补货查询抢占
|
|
履约实时复核、登录或批量任务的执行资源。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
from typing import Any
|
|
|
|
from sqlalchemy import case, func, select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ....models import (
|
|
Account,
|
|
AutomationJob,
|
|
PlatformFulfillmentSetting,
|
|
PlatformFulfillmentTask,
|
|
)
|
|
from ...account_sync_runner import ACCOUNT_SYNC_WORK_TYPES, WORK_STOREHOME_SYNC
|
|
from ...account_work_queue import (
|
|
active_account_work_count,
|
|
enqueue_account_work,
|
|
)
|
|
from ...platform_fulfillment.account_selector import (
|
|
capability_refresh_candidate_accounts,
|
|
)
|
|
|
|
JOB_KEY = "account_capability_refresh"
|
|
|
|
|
|
def _pending_demand_rows(
|
|
db: Session,
|
|
now: datetime,
|
|
) -> list[tuple[str, int]]:
|
|
"""返回待分配商品及订单数,按缺口大小排序。"""
|
|
pending_commodities_query = (
|
|
select(
|
|
PlatformFulfillmentTask.commodity_id,
|
|
func.count(PlatformFulfillmentTask.id).label("waiting"),
|
|
)
|
|
.where(
|
|
PlatformFulfillmentTask.execution_status == "ready",
|
|
PlatformFulfillmentTask.account_id.is_(None),
|
|
PlatformFulfillmentTask.commodity_id != "",
|
|
PlatformFulfillmentTask.target_platform == "douyu",
|
|
PlatformFulfillmentTask.scope == "elite",
|
|
)
|
|
)
|
|
match_started_at = (
|
|
db.query(PlatformFulfillmentSetting.order_match_started_at)
|
|
.filter(PlatformFulfillmentSetting.id == 1)
|
|
.scalar()
|
|
)
|
|
if match_started_at is not None:
|
|
pending_commodities_query = pending_commodities_query.where(
|
|
PlatformFulfillmentTask.paid_at >= match_started_at
|
|
)
|
|
statement = (
|
|
pending_commodities_query
|
|
.group_by(PlatformFulfillmentTask.commodity_id)
|
|
.order_by(func.count(PlatformFulfillmentTask.id).desc())
|
|
)
|
|
return list(db.execute(statement).all())
|
|
|
|
|
|
def _queue_target_pending() -> int:
|
|
import os
|
|
|
|
try:
|
|
return max(1, int(os.getenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "60")))
|
|
except ValueError:
|
|
return 60
|
|
|
|
|
|
def _build_select_query(
|
|
db: Session,
|
|
job: AutomationJob,
|
|
now: datetime | None = None,
|
|
*,
|
|
batch_limit: int | None = None,
|
|
):
|
|
moment = now or datetime.now(UTC)
|
|
demand_rows = _pending_demand_rows(db, moment)
|
|
commodity_ids = [str(row[0]) for row in demand_rows if row[0]]
|
|
if not commodity_ids:
|
|
return db.query(Account).filter(Account.id == -1)
|
|
batch = max(
|
|
1,
|
|
min(
|
|
int(batch_limit if batch_limit is not None else job.batch_size or 100),
|
|
100,
|
|
),
|
|
)
|
|
candidates = capability_refresh_candidate_accounts(
|
|
db,
|
|
commodity_ids=commodity_ids,
|
|
scope="elite",
|
|
limit=batch,
|
|
now=moment,
|
|
)
|
|
if not candidates:
|
|
return db.query(Account).filter(Account.id == -1)
|
|
rank = {int(account.id): index for index, account in enumerate(candidates)}
|
|
return (
|
|
db.query(Account)
|
|
.filter(Account.id.in_(tuple(rank)))
|
|
.order_by(
|
|
case(rank, value=Account.id),
|
|
Account.id.asc(),
|
|
)
|
|
)
|
|
|
|
|
|
def build_select_query(
|
|
db: Session,
|
|
job: AutomationJob,
|
|
now: datetime | None = None,
|
|
):
|
|
return _build_select_query(db, job, now)
|
|
|
|
|
|
def run_account_capability_refresh(
|
|
db: Session,
|
|
job: AutomationJob,
|
|
) -> dict[str, Any]:
|
|
now = datetime.now(UTC)
|
|
pending = active_account_work_count(
|
|
db,
|
|
work_types=ACCOUNT_SYNC_WORK_TYPES,
|
|
)
|
|
headroom = max(0, _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},
|
|
}
|
|
demand_rows = _pending_demand_rows(db, now)
|
|
commodity_ids = [str(row[0]) for row in demand_rows if row[0]]
|
|
accounts = _build_select_query(
|
|
db,
|
|
job,
|
|
now,
|
|
batch_limit=headroom,
|
|
).all()
|
|
for account in accounts:
|
|
enqueue_account_work(
|
|
db,
|
|
account_id=int(account.id),
|
|
work_type=WORK_STOREHOME_SYNC,
|
|
priority=10,
|
|
reason=(
|
|
"待履约商品补货: "
|
|
+ ",".join(commodity_ids[:5])
|
|
)[:255],
|
|
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 "当前履约商品没有可补充的新鲜 CK 候选账号"
|
|
),
|
|
"detail": {
|
|
"queued": queued,
|
|
"pending": pending,
|
|
"headroom": headroom,
|
|
"commodities": commodity_ids[:20],
|
|
},
|
|
}
|
|
|
|
|
|
__all__ = [
|
|
"JOB_KEY",
|
|
"build_select_query",
|
|
"run_account_capability_refresh",
|
|
]
|