feat(fulfillment): 增加账号商品能力索引与生命周期事件
This commit is contained in:
@@ -6,6 +6,12 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from .jobs.account_capability_refresh import (
|
||||
JOB_KEY as ACCOUNT_CAPABILITY_REFRESH_KEY,
|
||||
)
|
||||
from .jobs.account_capability_refresh import (
|
||||
run_account_capability_refresh,
|
||||
)
|
||||
from .jobs.account_overview_refresh import (
|
||||
JOB_KEY as ACCOUNT_OVERVIEW_REFRESH_KEY,
|
||||
)
|
||||
@@ -45,6 +51,7 @@ from .scheduler import (
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"ACCOUNT_CAPABILITY_REFRESH_KEY",
|
||||
"ACCOUNT_OVERVIEW_REFRESH_KEY",
|
||||
"CK_HEALTH_KEY",
|
||||
"CK_RELOGIN_KEY",
|
||||
@@ -58,6 +65,7 @@ __all__ = [
|
||||
"register_job",
|
||||
"registered_jobs",
|
||||
"reset_registry",
|
||||
"run_account_capability_refresh",
|
||||
"run_account_overview_refresh",
|
||||
"run_ck_health",
|
||||
"run_ck_relogin",
|
||||
@@ -74,6 +82,18 @@ __all__ = [
|
||||
|
||||
def register_default_jobs() -> None:
|
||||
"""登记内置作业(幂等)。"""
|
||||
register_job(
|
||||
JobSpec(
|
||||
key=ACCOUNT_CAPABILITY_REFRESH_KEY,
|
||||
title="履约商品能力补充刷新",
|
||||
# 只刷新有 pending 履约需求且能力快照已过期的账号。
|
||||
interval_seconds=3600,
|
||||
run_interval_seconds=5 * 60,
|
||||
batch_size=100,
|
||||
concurrency=3,
|
||||
run=run_account_capability_refresh,
|
||||
)
|
||||
)
|
||||
register_job(
|
||||
JobSpec(
|
||||
key=POINTS_EXPIRE_KEY,
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
"""按直播履约需求刷新账号商品能力。
|
||||
|
||||
全量资料补全仍由 ``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()
|
||||
)
|
||||
stale_demand_state = exists(
|
||||
select(AccountCommodityState.id).where(
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
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,
|
||||
),
|
||||
)
|
||||
)
|
||||
active_lease = exists(
|
||||
select(AccountLease.id).where(
|
||||
AccountLease.active_account_id == Account.id,
|
||||
)
|
||||
)
|
||||
return db.query(Account).filter(
|
||||
cookie_operable_filter(),
|
||||
Account.auto_managed.is_(True),
|
||||
Account.status.notin_(MANUAL_STATES),
|
||||
Account.ck_check_status == "valid",
|
||||
stale_demand_state,
|
||||
~active_lease,
|
||||
)
|
||||
|
||||
|
||||
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 count_remaining(
|
||||
db: Session,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
) -> int:
|
||||
return _base_query(db, now or datetime.now(UTC)).count()
|
||||
|
||||
|
||||
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,
|
||||
remaining_count=count_remaining(db, now=now),
|
||||
source="capability_demand_refresh",
|
||||
log_label="capability",
|
||||
empty_message="当前履约商品没有到期待刷新的账号能力",
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"JOB_KEY",
|
||||
"build_select_query",
|
||||
"count_remaining",
|
||||
"run_account_capability_refresh",
|
||||
]
|
||||
@@ -159,10 +159,18 @@ def _system_user_id(db: Session) -> int:
|
||||
return int(user.id)
|
||||
|
||||
|
||||
def run_account_overview_refresh(db: Session, job: AutomationJob) -> dict[str, Any]:
|
||||
"""本轮:选一批 → 建任务 → 就地跑完 → 看失败率决定要不要熔断。"""
|
||||
now = datetime.now(UTC)
|
||||
accounts = build_select_query(db, job, now).all()
|
||||
def run_refresh_for_accounts(
|
||||
db: Session,
|
||||
job: AutomationJob,
|
||||
accounts: list[Account],
|
||||
*,
|
||||
now: datetime,
|
||||
remaining_count: int | None = None,
|
||||
source: str = "scheduled_refresh",
|
||||
log_label: str = "overview",
|
||||
empty_message: str = "全库暂无待补全或到期的账号",
|
||||
) -> dict[str, Any]:
|
||||
"""对指定账号执行资料刷新;能力刷新作业也复用这条写入链。"""
|
||||
if not accounts:
|
||||
return {
|
||||
"scanned": 0,
|
||||
@@ -170,7 +178,7 @@ def run_account_overview_refresh(db: Session, job: AutomationJob) -> dict[str, A
|
||||
"invalid": 0,
|
||||
"succeeded": 0,
|
||||
"failed": 0,
|
||||
"message": "全库暂无待补全或到期的账号",
|
||||
"message": empty_message,
|
||||
}
|
||||
|
||||
account_ids = [a.id for a in accounts]
|
||||
@@ -181,7 +189,7 @@ def run_account_overview_refresh(db: Session, job: AutomationJob) -> dict[str, A
|
||||
"elite",
|
||||
_system_user_id(db),
|
||||
# 标上来源,便于在任务列表里区分「定时巡检」与「人工点查询」
|
||||
{"source": "scheduled_refresh"},
|
||||
{"source": source},
|
||||
)
|
||||
if count == 0:
|
||||
return {
|
||||
@@ -220,19 +228,18 @@ def run_account_overview_refresh(db: Session, job: AutomationJob) -> dict[str, A
|
||||
db.commit()
|
||||
paused = True
|
||||
logger.warning(
|
||||
f"[overview] 失败率 {error_ratio:.0%} 过高,已自动关闭作业"
|
||||
f"[{log_label}] 失败率 {error_ratio:.0%} 过高,已自动关闭作业"
|
||||
f"(batch={batch_id},failed={failed}/{len(rows)})—— 请确认是否被风控"
|
||||
)
|
||||
|
||||
message = (
|
||||
f"本轮查 {len(rows)} 个:成功 {succeeded} / 失败 {failed}"
|
||||
f"(剩 {count_remaining(db, job, now)} 个待查)"
|
||||
)
|
||||
message = f"本轮查 {len(rows)} 个:成功 {succeeded} / 失败 {failed}"
|
||||
if remaining_count is not None:
|
||||
message += f"(剩 {remaining_count} 个待查)"
|
||||
if paused:
|
||||
# 把**可能的原因**写进消息里:运维看作业历史时才知道该去查什么,
|
||||
# 否则只看到"失败率高"会一头雾水。
|
||||
message += ";失败率过高,已自动关闭作业(大面积失败通常是**被风控**或接口抖动,请先确认再重开)"
|
||||
logger.info(f"[overview] {message}")
|
||||
logger.info(f"[{log_label}] {message}")
|
||||
return {
|
||||
"scanned": len(rows),
|
||||
"valid": succeeded,
|
||||
@@ -243,3 +250,16 @@ def run_account_overview_refresh(db: Session, job: AutomationJob) -> dict[str, A
|
||||
"paused": paused,
|
||||
"message": message,
|
||||
}
|
||||
|
||||
|
||||
def run_account_overview_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,
|
||||
remaining_count=count_remaining(db, job, now),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user