refactor(douyu): 完成阶段5旧链路清理
This commit is contained in:
@@ -4,13 +4,14 @@ from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from sqlalchemy import and_, or_, select
|
||||
from sqlalchemy import and_, func, or_, select
|
||||
|
||||
from ..models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountLease,
|
||||
DouyuExternalOrder,
|
||||
LoginTask,
|
||||
User,
|
||||
)
|
||||
from .account_capability_service import (
|
||||
@@ -31,6 +32,7 @@ ACTIVE_COLLAB_STATUSES: tuple[str, ...] = (
|
||||
)
|
||||
POOL_OWNER_ROLES: tuple[str, ...] = ("super_admin", "operation", "support")
|
||||
CK_FRESH_SECONDS = 24 * 60 * 60
|
||||
LOGIN_ATTEMPT_IN_FLIGHT_STATUSES = ("pending", "running")
|
||||
|
||||
|
||||
def pool_owner_ids():
|
||||
@@ -44,6 +46,26 @@ def cookie_present_filter():
|
||||
return and_(Account.cookie.isnot(None), Account.cookie != "")
|
||||
|
||||
|
||||
def cookie_holding_filter():
|
||||
"""账号当前是否持有 CK。"""
|
||||
return cookie_present_filter()
|
||||
|
||||
|
||||
def cookie_operable_filter():
|
||||
"""持有 CK,且该账号没有在途登录/检测尝试。"""
|
||||
in_flight = (
|
||||
select(LoginTask.id)
|
||||
.where(LoginTask.account_id == Account.id)
|
||||
.where(
|
||||
func.coalesce(LoginTask.attempt_status, "").in_(
|
||||
LOGIN_ATTEMPT_IN_FLIGHT_STATUSES
|
||||
)
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
return and_(cookie_holding_filter(), ~in_flight)
|
||||
|
||||
|
||||
def valid_ck_filter(*, checked_since: datetime | None = None):
|
||||
filters = [Account.ck_check_status == "valid"]
|
||||
if checked_since is not None:
|
||||
@@ -63,6 +85,14 @@ def automatable_account_filter():
|
||||
)
|
||||
|
||||
|
||||
def automation_account_filter(*, cookie_operable: bool = True):
|
||||
"""所有自动账号作业共用的账号生命周期准入。"""
|
||||
return and_(
|
||||
cookie_operable_filter() if cookie_operable else cookie_holding_filter(),
|
||||
automatable_account_filter(),
|
||||
)
|
||||
|
||||
|
||||
def assigned_to_pool_filter():
|
||||
return or_(
|
||||
Account.assigned_to.is_(None),
|
||||
@@ -124,11 +154,15 @@ def fresh_eligible_state_filter(
|
||||
__all__ = [
|
||||
"ACTIVE_COLLAB_STATUSES",
|
||||
"CK_FRESH_SECONDS",
|
||||
"LOGIN_ATTEMPT_IN_FLIGHT_STATUSES",
|
||||
"POOL_OWNER_ROLES",
|
||||
"active_collab_order_exists",
|
||||
"active_lease_exists",
|
||||
"assigned_to_pool_filter",
|
||||
"automatable_account_filter",
|
||||
"automation_account_filter",
|
||||
"cookie_holding_filter",
|
||||
"cookie_operable_filter",
|
||||
"cookie_present_filter",
|
||||
"fresh_ck_cutoff",
|
||||
"fresh_eligible_state_filter",
|
||||
|
||||
@@ -17,8 +17,9 @@ from sqlalchemy import and_, case, func, or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ....models import Account, AutomationJob
|
||||
from ...account_lifecycle import MANUAL_STATES, WORK_LOGIN_RELOGIN
|
||||
from ...account_lifecycle import WORK_LOGIN_RELOGIN
|
||||
from ...account_protection_service import load_protection_snapshot
|
||||
from ...account_query_filters import automation_account_filter
|
||||
from ...account_value import (
|
||||
HIGH_VALUE_TIER_MAX,
|
||||
high_value_recheck_seconds,
|
||||
@@ -35,7 +36,6 @@ from ...douyu_account_probe import (
|
||||
persist_probe_result,
|
||||
probe_session,
|
||||
)
|
||||
from ...douyu_service import cookie_holding_filter, cookie_operable_filter
|
||||
from ...task_runtime import TaskLane, submit
|
||||
|
||||
JOB_KEY = "ck_health"
|
||||
@@ -101,13 +101,11 @@ def _candidates_query(db: Session, job: AutomationJob, now: datetime):
|
||||
|
||||
query = (
|
||||
db.query(Account)
|
||||
.filter(cookie_operable_filter())
|
||||
.filter(Account.auto_managed.is_(True))
|
||||
.filter(automation_account_filter())
|
||||
# invalid 已进入自动重登队列,manual 已停止自动处理;普通 CK 探针
|
||||
# 再扫它们只会重复消耗官方请求,并掩盖真正需要发现的新失效。
|
||||
.filter(Account.ck_check_status.notin_(("invalid", "manual")))
|
||||
# 生命周期状态机:停用/归档的账号不参与任何自动化(见 account_lifecycle)。
|
||||
.filter(Account.status.notin_(MANUAL_STATES))
|
||||
.filter(due_filter)
|
||||
)
|
||||
if protected_ids:
|
||||
@@ -181,7 +179,7 @@ def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
|
||||
if not accounts:
|
||||
earliest_checked = (
|
||||
db.query(func.min(Account.ck_checked_at))
|
||||
.filter(cookie_holding_filter())
|
||||
.filter(automation_account_filter(cookie_operable=False))
|
||||
.scalar()
|
||||
)
|
||||
msg = "全库暂无到期待检账号"
|
||||
|
||||
@@ -35,7 +35,6 @@ from ....models import ProxyConfig as ProxyConfigModel
|
||||
from ...account_events_service import EVENT_RELOGIN_FAILED, emit
|
||||
from ...account_lifecycle import (
|
||||
MANUAL_KINDS,
|
||||
MANUAL_STATES,
|
||||
WORK_LOGIN_RELOGIN,
|
||||
on_login_success,
|
||||
sync_status,
|
||||
@@ -44,6 +43,7 @@ from ...account_protection_service import (
|
||||
ProtectionSnapshot,
|
||||
load_protection_snapshot,
|
||||
)
|
||||
from ...account_query_filters import automation_account_filter
|
||||
from ...account_value import value_tier_expr
|
||||
from ...account_work_queue import (
|
||||
claim_account_work,
|
||||
@@ -51,7 +51,6 @@ from ...account_work_queue import (
|
||||
fail_account_work_type,
|
||||
reschedule_account_work_type,
|
||||
)
|
||||
from ...douyu_service import cookie_holding_filter, cookie_operable_filter
|
||||
from ...login_failure import classify_failure
|
||||
from ...login_service import LoginBatchRunner, get_relogin_limits
|
||||
|
||||
@@ -121,10 +120,8 @@ def build_select_query(
|
||||
query = (
|
||||
db.query(Account)
|
||||
.filter(Account.ck_check_status == "invalid")
|
||||
.filter(cookie_operable_filter())
|
||||
.filter(Account.auto_managed.is_(True))
|
||||
.filter(automation_account_filter())
|
||||
# 停用/归档的账号一律不碰(生命周期状态机,见 account_lifecycle)。
|
||||
.filter(Account.status.notin_(MANUAL_STATES))
|
||||
.filter(Account.relogin_attempts < max_attempts)
|
||||
.filter(
|
||||
or_(
|
||||
@@ -248,10 +245,8 @@ def count_pending_relogin(db: Session, *, max_attempts: int = 2) -> int:
|
||||
query = (
|
||||
db.query(func.count(Account.id))
|
||||
.filter(Account.ck_check_status == "invalid")
|
||||
.filter(cookie_holding_filter())
|
||||
.filter(Account.auto_managed.is_(True))
|
||||
.filter(automation_account_filter(cookie_operable=False))
|
||||
# 停用/归档不计入积压(它们不会被自动处理,显示出来只会误导)
|
||||
.filter(Account.status.notin_(MANUAL_STATES))
|
||||
.filter(Account.relogin_attempts < max_attempts)
|
||||
.filter(
|
||||
func.coalesce(Account.relogin_failure_kind, "").notin_(
|
||||
|
||||
@@ -52,16 +52,21 @@ from ....models import AutomationJob
|
||||
|
||||
JOB_KEY = "douyu_tasks_retention"
|
||||
|
||||
#: 只读、看一眼就走,且前端**不**拿它派生账号状态 → 短保留。
|
||||
#: 体量最大的就在这一类(`query_limited_goods` / `query_change_bind_time`)。
|
||||
NOISE_TASK_TYPES: tuple[str, ...] = (
|
||||
#: 已淘汰但仍可能有历史行的任务类型。保留在清理名单中,
|
||||
#: 避免下线入口后旧日志永久占用空间。
|
||||
RETIRED_NOISE_TASK_TYPES: tuple[str, ...] = (
|
||||
"query_limited_goods",
|
||||
"refresh_goods",
|
||||
)
|
||||
|
||||
#: 只读、看一眼就走,且前端**不**拿它派生账号状态 → 短保留。
|
||||
NOISE_TASK_TYPES: tuple[str, ...] = (
|
||||
*RETIRED_NOISE_TASK_TYPES,
|
||||
"query_change_bind_time",
|
||||
"query_gold_balance",
|
||||
"query_xpd_balance",
|
||||
"query_xpd_fragments",
|
||||
"query_points",
|
||||
"refresh_goods",
|
||||
"refresh_esports_goods",
|
||||
"refresh_xpd_goods",
|
||||
"prefetch_csrf_token",
|
||||
|
||||
@@ -179,7 +179,6 @@ class DouyuBatchRunner(
|
||||
update_account_profile_from_cookie(account, cookie)
|
||||
|
||||
handler = {
|
||||
"refresh_goods": self._execute_refresh_goods,
|
||||
"refresh_esports_goods": self._execute_refresh_esports_goods,
|
||||
"get_bind_qr": self._execute_get_bind_qr,
|
||||
"confirm_bind": self._execute_confirm_bind,
|
||||
@@ -202,7 +201,6 @@ class DouyuBatchRunner(
|
||||
"exchange_esports_goods": self._execute_exchange_esports_goods,
|
||||
"query_game_name": self._execute_query_game_name,
|
||||
"query_change_bind_time": self._execute_query_change_bind_time,
|
||||
"query_limited_goods": self._execute_query_limited_goods,
|
||||
"query_account_overview": self._execute_query_account_overview,
|
||||
"query_gold_balance": self._execute_query_gold_balance,
|
||||
"query_exchange_records": self._execute_query_exchange_records,
|
||||
|
||||
@@ -13,18 +13,13 @@ from core.douyu import DouyuActivityClient, DouyuActivityError
|
||||
|
||||
from ..models import Account, DouyuEsportsGoodsSnapshot, DouyuGoodsSnapshot, DouyuTask
|
||||
from .account_exchange_sync_service import sync_exchange_records
|
||||
from .account_sync_service import sync_storehome_snapshot
|
||||
from .business_events import record_exchange
|
||||
from .douyu_activity_query import (
|
||||
query_activity_goods,
|
||||
query_activity_goods_result,
|
||||
query_csrf,
|
||||
query_elite_user_info,
|
||||
query_esports_goods_result,
|
||||
query_limited_goods_result,
|
||||
)
|
||||
from .douyu_goods_service import upsert_goods_snapshot
|
||||
from .douyu_read_gateway import storehome_snapshot_from_result
|
||||
from .high_value_service import (
|
||||
DEFAULT_HIGH_VALUE_GOODS,
|
||||
HIGH_VALUE_STATUS_OWNED,
|
||||
@@ -78,48 +73,6 @@ class GoodsMixin:
|
||||
def _push_log(self, level: str, message: str) -> None: ...
|
||||
def _mark_task(self, *args: Any, **kwargs: Any) -> None: ...
|
||||
|
||||
def _execute_refresh_goods(
|
||||
self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict
|
||||
):
|
||||
client = self._client(cookie)
|
||||
result = query_activity_goods_result(
|
||||
client,
|
||||
manual_id=str(config["manual_id"]),
|
||||
rid=str(config["rid"]),
|
||||
)
|
||||
goods = [
|
||||
dict(item)
|
||||
for item in result.get("goods") or []
|
||||
if isinstance(item, dict)
|
||||
]
|
||||
snapshot = storehome_snapshot_from_result(
|
||||
result,
|
||||
manual_id=str(config["manual_id"]),
|
||||
rid=str(config["rid"]),
|
||||
)
|
||||
upsert_goods_snapshot(db, goods, commit=False)
|
||||
sync_storehome_snapshot(
|
||||
db,
|
||||
account,
|
||||
snapshot,
|
||||
source="douyu_task.refresh_goods",
|
||||
task_id=task.id,
|
||||
)
|
||||
high_value_goods = extract_high_value_goods(goods, DEFAULT_HIGH_VALUE_GOODS, [])
|
||||
apply_high_value_goods(account, high_value_goods)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
self._mark_task(
|
||||
db,
|
||||
task,
|
||||
"success",
|
||||
f"已刷新商品 {len(goods)} 个",
|
||||
{
|
||||
"goods_count": len(goods),
|
||||
"goods": goods,
|
||||
"high_value_goods": high_value_goods,
|
||||
},
|
||||
)
|
||||
|
||||
def _refresh_high_value_after_exchange(
|
||||
self,
|
||||
client: DouyuActivityClient,
|
||||
@@ -736,52 +689,6 @@ class GoodsMixin:
|
||||
)
|
||||
self._mark_task(db, task, "success", message, result)
|
||||
|
||||
def _execute_query_limited_goods(
|
||||
self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict
|
||||
):
|
||||
client = self._client(cookie)
|
||||
result = query_limited_goods_result(
|
||||
client, manual_id=str(config["manual_id"]), rid=str(config["rid"])
|
||||
)
|
||||
limited = result["limited_goods"]
|
||||
snapshot = storehome_snapshot_from_result(
|
||||
result,
|
||||
manual_id=str(config["manual_id"]),
|
||||
rid=str(config["rid"]),
|
||||
)
|
||||
goods = list(snapshot.items)
|
||||
sync_storehome_snapshot(
|
||||
db,
|
||||
account,
|
||||
snapshot,
|
||||
source="douyu_task.query_limited_goods",
|
||||
task_id=task.id,
|
||||
)
|
||||
high_value_goods = extract_high_value_goods(goods, DEFAULT_HIGH_VALUE_GOODS, [])
|
||||
apply_high_value_goods(account, high_value_goods)
|
||||
names = [
|
||||
str(item.get("commodityName") or "")
|
||||
for item in limited
|
||||
if item.get("commodityName")
|
||||
]
|
||||
message = (
|
||||
"无受限商品(均可兑换)"
|
||||
if not names
|
||||
else f"已达上限 {len(names)} 个: {', '.join(names[:5])}"
|
||||
)
|
||||
account.updated_at = datetime.now(UTC)
|
||||
self._mark_task(
|
||||
db,
|
||||
task,
|
||||
"success",
|
||||
message,
|
||||
{
|
||||
"limited_count": len(limited),
|
||||
"limited_goods": limited,
|
||||
"high_value_goods": high_value_goods,
|
||||
},
|
||||
)
|
||||
|
||||
def _execute_query_exchange_records(
|
||||
self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict
|
||||
):
|
||||
|
||||
@@ -695,7 +695,7 @@ class OverviewMixin:
|
||||
|
||||
# ---- 精英宝典商城商品列表(高价值套装的**唯一**判据来源)----
|
||||
#
|
||||
# ⚠️ 必须用**精英宝典**的 manual_id(与 `refresh_goods` 任务一致),
|
||||
# ⚠️ 必须用**精英宝典**的 manual_id(与其他商城能力快照一致),
|
||||
# 而且**不能**寄生在上面的「电竞手册」分支里:那是两个无关的活动。
|
||||
# 之前商品列表被写在电竞手册的 else 分支内 —— 于是一旦没配
|
||||
# `esports_manual_id`,商城商品就永远拉不到,高价值三态就永远
|
||||
@@ -785,6 +785,14 @@ class OverviewMixin:
|
||||
errors["limited_goods"] = _err(exc)
|
||||
# 商品列表带有账号级 status;兑换记录只作历史展示,不参与准入判定。
|
||||
pending_goods = goods_items
|
||||
limited_goods = [
|
||||
item
|
||||
for item in goods_items
|
||||
if str(item.get("status") if item.get("status") is not None else "")
|
||||
== "1"
|
||||
]
|
||||
overview["limited_count"] = len(limited_goods)
|
||||
overview["limited_goods"] = limited_goods
|
||||
|
||||
# ---- 角色绑定状态(三个作用域都查)----
|
||||
#
|
||||
|
||||
@@ -7,14 +7,19 @@ import uuid
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from loguru import logger
|
||||
from sqlalchemy import and_, func, or_, select
|
||||
from sqlalchemy import and_, func, or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.douyu.cookie_utils import cookie_value
|
||||
|
||||
from ..models import Account, DouyuConfig, DouyuTask, LoginTask
|
||||
from ..models import Account, DouyuConfig, DouyuTask
|
||||
from . import account_query_filters as account_filters
|
||||
from .account_events_service import CATEGORY_ACTIVITY, EVENT_TASK_STARTED, emit_bulk
|
||||
|
||||
LOGIN_ATTEMPT_IN_FLIGHT_STATUSES = account_filters.LOGIN_ATTEMPT_IN_FLIGHT_STATUSES
|
||||
cookie_holding_filter = account_filters.cookie_holding_filter
|
||||
cookie_operable_filter = account_filters.cookie_operable_filter
|
||||
|
||||
SUPPORTED_DOUYU_TASK_TYPES = {
|
||||
"get_bind_qr": "获取绑定二维码",
|
||||
"confirm_bind": "确认绑定",
|
||||
@@ -38,11 +43,9 @@ SUPPORTED_DOUYU_TASK_TYPES = {
|
||||
"exchange_goods": "兑换商品",
|
||||
"query_game_name": "一键获取游戏名",
|
||||
"query_change_bind_time": "一键查询换绑时间",
|
||||
"query_limited_goods": "一键查询限兑商品",
|
||||
# 跨域聚合查询:鱼翅/积分/精英/电竞/小店/兑换记录/高价值套装是否已拥有
|
||||
"query_account_overview": "一键查询账号信息",
|
||||
"query_gold_balance": "一键查询鱼翅余额",
|
||||
"refresh_goods": "刷新商品列表",
|
||||
"query_exchange_records": "一键查询兑换记录",
|
||||
"prefetch_csrf_token": "一键获取兑换 CSRF Token",
|
||||
"get_xpd_bind_qr": "生成小店绑定二维码",
|
||||
@@ -73,9 +76,7 @@ DOUYU_HANDBOOK_TASK_TYPES = {
|
||||
"exchange_goods",
|
||||
"query_game_name",
|
||||
"query_change_bind_time",
|
||||
"query_limited_goods",
|
||||
"query_gold_balance",
|
||||
"refresh_goods",
|
||||
"query_exchange_records",
|
||||
"prefetch_csrf_token",
|
||||
"query_account_overview",
|
||||
@@ -89,7 +90,6 @@ DOUYU_HANDBOOK_TASK_TYPES = {
|
||||
"query_esports_points",
|
||||
"query_gold_balance",
|
||||
"query_change_bind_time",
|
||||
"query_limited_goods",
|
||||
"refresh_esports_goods",
|
||||
"exchange_esports_goods",
|
||||
"create_gold_qr",
|
||||
@@ -235,45 +235,6 @@ def ensure_douyu_config(db: Session) -> DouyuConfig:
|
||||
return config
|
||||
|
||||
|
||||
# 登录/重登尝试的在途状态:判断只看 attempt_*,与账号持有的 CK 无关。
|
||||
LOGIN_ATTEMPT_IN_FLIGHT_STATUSES = ("pending", "running")
|
||||
|
||||
|
||||
def cookie_holding_filter():
|
||||
"""判定**账号**当前是否持有 CK。
|
||||
|
||||
CK 是账号级凭证(2026-10-04 起从 `login_tasks.cookie` 迁到 `accounts.cookie`)。
|
||||
判定只看账号自己,不再看任何登录记录的状态 —— 这正是解耦的目的:
|
||||
删历史、批次排队、重登失败都不该让账号凭空「没有 CK」。
|
||||
"""
|
||||
return and_(
|
||||
Account.cookie.isnot(None),
|
||||
Account.cookie != "",
|
||||
)
|
||||
|
||||
|
||||
def cookie_operable_filter():
|
||||
"""判定**账号**能否基于现有 CK 再做检测或重登。
|
||||
|
||||
在途排除仍然要查 `login_tasks`:「正被批次处理」是**某次尝试**的属性,
|
||||
CK 才是账号的属性。所以这里是「账号有 CK」+「该账号没有在途尝试」。
|
||||
|
||||
走 `login_tasks(account_id, attempt_status)` 索引(迁移 0042 新增),
|
||||
否则每轮选行都要回表。
|
||||
"""
|
||||
in_flight = (
|
||||
select(LoginTask.id)
|
||||
.where(LoginTask.account_id == Account.id)
|
||||
.where(
|
||||
func.coalesce(LoginTask.attempt_status, "").in_(
|
||||
LOGIN_ATTEMPT_IN_FLIGHT_STATUSES
|
||||
)
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
return and_(cookie_holding_filter(), ~in_flight)
|
||||
|
||||
|
||||
def latest_cookie(db: Session, account_id: int) -> str:
|
||||
"""读取账号当前 CK。"""
|
||||
account = db.query(Account.cookie).filter(Account.id == account_id).first()
|
||||
@@ -394,7 +355,7 @@ def create_douyu_planned_tasks(
|
||||
_ensure_active_task_budget(db, len(accounts))
|
||||
_lock_payment_accounts(db, accounts, task_type)
|
||||
if (
|
||||
task_type in {"refresh_goods", "refresh_esports_goods", "refresh_xpd_goods"}
|
||||
task_type in {"refresh_esports_goods", "refresh_xpd_goods"}
|
||||
and accounts
|
||||
):
|
||||
# 商品快照是全局数据,一个可用 CK 足够;没有 CK 时前端无法选账号创建任务。
|
||||
|
||||
@@ -10,7 +10,6 @@ from threading import Lock
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException
|
||||
from loguru import logger
|
||||
from sqlalchemy import func, or_, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -74,11 +73,10 @@ from ..douyu_account_probe import (
|
||||
persist_probe_result,
|
||||
probe_account,
|
||||
)
|
||||
from ..douyu_account_state import DouyuLiveAccountState, refresh_douyu_account_state
|
||||
from ..douyu_account_state import DouyuLiveAccountState
|
||||
from ..douyu_batch_launcher import start_douyu_batch, stop_douyu_batches
|
||||
from ..douyu_service import (
|
||||
SUPPORTED_DOUYU_TASK_TYPES,
|
||||
douyu_config_value,
|
||||
ensure_douyu_config,
|
||||
)
|
||||
from ..external_order_source import (
|
||||
@@ -91,12 +89,9 @@ from . import classification
|
||||
from .account_selector import (
|
||||
AccountCandidate,
|
||||
CapabilityCandidate,
|
||||
capability_candidate_accounts,
|
||||
capability_candidate_rows,
|
||||
capability_refresh_candidate_accounts,
|
||||
has_recent_exchange,
|
||||
lease_account,
|
||||
live_candidate_accounts,
|
||||
release_lease,
|
||||
)
|
||||
from .order_site_notifier import enqueue_order_site_binding_state
|
||||
@@ -117,7 +112,7 @@ BINDING_QR_TTL_SECONDS = int(
|
||||
os.getenv("PLATFORM_BINDING_QR_TTL_SECONDS", "1260")
|
||||
)
|
||||
AUTO_SYNC_INTERVAL_SECONDS = 900.0
|
||||
DEFAULT_LIVE_CANDIDATE_LIMIT = 20
|
||||
DEFAULT_CAPABILITY_CANDIDATE_LIMIT = 20
|
||||
LOCAL_STOCK_RETRY_SECONDS = 5 * 60
|
||||
LOCAL_CACHE_MISS_RETRY_SECONDS = 2 * 60
|
||||
LOCAL_LEASE_CONFLICT_RETRY_SECONDS = 5
|
||||
@@ -138,11 +133,6 @@ def binding_failed_lease_ttl_minutes() -> int:
|
||||
return max(1, value)
|
||||
|
||||
|
||||
def _matching_mode() -> str:
|
||||
mode = os.getenv("PLATFORM_FULFILLMENT_MATCH_MODE", "hybrid").strip().lower()
|
||||
return mode if mode in {"legacy", "hybrid", "capability"} else "hybrid"
|
||||
|
||||
|
||||
def _exchange_gate(
|
||||
state: DouyuLiveAccountState,
|
||||
commodity_id: str,
|
||||
@@ -2600,7 +2590,6 @@ def _allocate_task_from_capability(
|
||||
*,
|
||||
task: PlatformFulfillmentTask,
|
||||
current: User,
|
||||
mode: str,
|
||||
) -> dict[str, Any]:
|
||||
"""只用本地能力索引选号;外部资格查询留给独立同步队列。"""
|
||||
moment = _now()
|
||||
@@ -2608,12 +2597,12 @@ def _allocate_task_from_capability(
|
||||
try:
|
||||
candidate_limit = int(
|
||||
os.getenv(
|
||||
"PLATFORM_FULFILLMENT_LIVE_CANDIDATE_LIMIT",
|
||||
str(DEFAULT_LIVE_CANDIDATE_LIMIT),
|
||||
"PLATFORM_FULFILLMENT_CAPABILITY_CANDIDATE_LIMIT",
|
||||
str(DEFAULT_CAPABILITY_CANDIDATE_LIMIT),
|
||||
)
|
||||
)
|
||||
except ValueError:
|
||||
candidate_limit = DEFAULT_LIVE_CANDIDATE_LIMIT
|
||||
candidate_limit = DEFAULT_CAPABILITY_CANDIDATE_LIMIT
|
||||
candidate_limit = max(1, min(candidate_limit, 100))
|
||||
is_presale = classification.normalize_sale_mode(task.sale_mode) == "presale"
|
||||
capability_candidates = capability_candidate_rows(
|
||||
@@ -2770,7 +2759,7 @@ def _allocate_task_from_capability(
|
||||
"account_allocation_failed",
|
||||
detail={
|
||||
"candidateCount": len(capability_candidates),
|
||||
"matchingMode": mode,
|
||||
"matchingMode": "capability",
|
||||
"strategy": "local_capability",
|
||||
"capabilityCandidates": len(capability_candidates),
|
||||
"availabilityCounts": availability_counts,
|
||||
@@ -2874,304 +2863,17 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
raise HTTPException(status_code=409, detail="已结束任务不能重新分配账号")
|
||||
if not task.commodity_id or not task.required_points:
|
||||
raise HTTPException(status_code=409, detail="请先完成商品映射")
|
||||
required_points = int(task.required_points)
|
||||
if task.account_id and task.lease_id:
|
||||
lease = db.query(AccountLease).filter(AccountLease.id == task.lease_id).first()
|
||||
if lease and lease.status == "active":
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
mode = _matching_mode()
|
||||
if mode in {"hybrid", "capability"}:
|
||||
# 2026-11-22:分配热路径只读本地能力索引。官网资格刷新已经由独立
|
||||
# account_sync_queue 执行;绑码和兑换前仍有各自的实时校验,不应让
|
||||
# 一次选号串行探测 20 个账号并阻塞其他订单。
|
||||
return _allocate_task_from_capability(
|
||||
db,
|
||||
task=task,
|
||||
current=current,
|
||||
mode=mode,
|
||||
)
|
||||
|
||||
config = ensure_douyu_config(db)
|
||||
lease_duration_minutes = _lease_duration_minutes(db)
|
||||
manual_id = str(douyu_config_value("manual_id", config.manual_id))
|
||||
rid = str(douyu_config_value("rid", config.rid))
|
||||
act_alias = str(douyu_config_value("bind_act_alias", config.bind_act_alias))
|
||||
try:
|
||||
candidate_limit = int(
|
||||
os.getenv(
|
||||
"PLATFORM_FULFILLMENT_LIVE_CANDIDATE_LIMIT",
|
||||
str(DEFAULT_LIVE_CANDIDATE_LIMIT),
|
||||
)
|
||||
)
|
||||
except ValueError:
|
||||
candidate_limit = DEFAULT_LIVE_CANDIDATE_LIMIT
|
||||
candidate_limit = max(1, min(candidate_limit, 100))
|
||||
is_presale = classification.normalize_sale_mode(task.sale_mode) == "presale"
|
||||
capability_candidates = (
|
||||
capability_candidate_accounts(
|
||||
db,
|
||||
limit=candidate_limit,
|
||||
required_points=required_points,
|
||||
commodity_id=task.commodity_id,
|
||||
scope=task.scope or SCOPE_ELITE,
|
||||
allow_not_open=is_presale,
|
||||
)
|
||||
if mode in {"hybrid", "capability"}
|
||||
else []
|
||||
)
|
||||
capability_first = bool(capability_candidates)
|
||||
candidates = capability_candidates
|
||||
if not candidates and mode != "capability":
|
||||
# hybrid 模式下能力索引为空或全部过期时,保留旧逻辑兜底。
|
||||
candidates = live_candidate_accounts(
|
||||
db,
|
||||
limit=candidate_limit,
|
||||
required_points=required_points,
|
||||
commodity_id=task.commodity_id,
|
||||
)
|
||||
diagnostics: list[dict[str, Any]] = []
|
||||
qualified: list[tuple[Account, DouyuLiveAccountState]] = []
|
||||
selected: AccountCandidate | None = None
|
||||
selected_state: DouyuLiveAccountState | None = None
|
||||
lease = None
|
||||
|
||||
def inspect_candidate(
|
||||
account: Account,
|
||||
*,
|
||||
source: str,
|
||||
) -> tuple[Account, DouyuLiveAccountState] | None:
|
||||
live_state = refresh_douyu_account_state(
|
||||
db,
|
||||
account,
|
||||
manual_id=manual_id,
|
||||
rid=rid,
|
||||
act_alias=act_alias,
|
||||
)
|
||||
limited = task.commodity_id in live_state.limited_ids
|
||||
recent_exchange = has_recent_exchange(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
commodity_id=task.commodity_id,
|
||||
)
|
||||
reasons = live_state.blocking_reasons(
|
||||
commodity_id=task.commodity_id,
|
||||
allow_not_open=is_presale,
|
||||
)
|
||||
if recent_exchange:
|
||||
reasons.append("365 天内已有该商品兑换事件")
|
||||
|
||||
if live_state.ck_verdict == "valid" and not live_state.error:
|
||||
try:
|
||||
availability = live_state.commodity_availability.get(
|
||||
task.commodity_id
|
||||
)
|
||||
blocking_reason = live_state.commodity_blocking_reasons.get(
|
||||
task.commodity_id,
|
||||
"",
|
||||
)
|
||||
if limited:
|
||||
availability = AVAILABILITY_LIMITED
|
||||
blocking_reason = "该账号已达此商品兑换上限"
|
||||
elif live_state.commodity_availability and availability is None:
|
||||
availability = AVAILABILITY_NOT_OFFERED
|
||||
blocking_reason = "官方商品列表本次未返回该商品,可能已下架"
|
||||
elif availability is None:
|
||||
availability = AVAILABILITY_AVAILABLE
|
||||
record_account_commodity_state(
|
||||
db,
|
||||
account,
|
||||
commodity_id=task.commodity_id,
|
||||
availability=availability,
|
||||
blocking_reason=blocking_reason,
|
||||
raw_status="1" if limited else "0",
|
||||
observed_at=live_state.checked_at,
|
||||
source="live_check",
|
||||
scope=task.scope or SCOPE_ELITE,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - 派生索引不能阻止本次选号
|
||||
logger.warning(
|
||||
"写入选号商品能力失败 account_id={} commodity_id={}: {}",
|
||||
account.id,
|
||||
task.commodity_id,
|
||||
exc,
|
||||
)
|
||||
if reasons:
|
||||
diagnostics.append(
|
||||
{
|
||||
"accountId": int(account.id),
|
||||
"accountName": live_state.account_name,
|
||||
"ck": live_state.ck_verdict,
|
||||
"eliteOpened": live_state.elite_opened,
|
||||
"points": live_state.points,
|
||||
"reasons": reasons,
|
||||
"source": source,
|
||||
}
|
||||
)
|
||||
return None
|
||||
return account, live_state
|
||||
|
||||
def inspect_batch(
|
||||
batch: list[Account],
|
||||
*,
|
||||
source: str,
|
||||
) -> None:
|
||||
# 能力索引已按成本排序,首个确认通过即可;旧兜底最多比较 3 个。
|
||||
for account in batch:
|
||||
inspected = inspect_candidate(account, source=source)
|
||||
if inspected is None:
|
||||
continue
|
||||
qualified.append(inspected)
|
||||
if source == "capability" or len(qualified) >= 3:
|
||||
return
|
||||
|
||||
if capability_candidates:
|
||||
inspect_batch(capability_candidates, source="capability")
|
||||
elif mode != "capability":
|
||||
inspect_batch(candidates, source="live")
|
||||
|
||||
if not qualified and capability_first and mode == "hybrid":
|
||||
checked_ids = {int(account.id) for account in capability_candidates}
|
||||
fallback_candidates = [
|
||||
account
|
||||
for account in live_candidate_accounts(
|
||||
db,
|
||||
limit=candidate_limit,
|
||||
required_points=required_points,
|
||||
commodity_id=task.commodity_id,
|
||||
)
|
||||
if int(account.id) not in checked_ids
|
||||
]
|
||||
candidates = [*candidates, *fallback_candidates]
|
||||
inspect_batch(fallback_candidates, source="live_fallback")
|
||||
|
||||
# 积分和宝典开通不淘汰账号,只影响后续处理成本。优先选已经可直接兑换的账号,
|
||||
# 其次是只需充值、只需开通、两者都需要的账号。
|
||||
qualified.sort(
|
||||
key=lambda item: (
|
||||
item[1].readiness_rank(required_points=required_points),
|
||||
int(item[1].points or 0),
|
||||
int(item[0].id),
|
||||
)
|
||||
)
|
||||
for account, live_state in qualified:
|
||||
candidate = AccountCandidate(
|
||||
account_id=int(account.id),
|
||||
username=str(account.username or ""),
|
||||
nickname=str(account.nickname or ""),
|
||||
points=int(live_state.points or 0),
|
||||
gold_balance=account.gold_balance,
|
||||
points_expire_at=account.points_expire_at,
|
||||
role_change_available_at=live_state.role_change_available_at,
|
||||
)
|
||||
lease = lease_account(
|
||||
db,
|
||||
candidate=candidate,
|
||||
holder_user_id=current.id,
|
||||
task_id=task.id,
|
||||
commodity_id=task.commodity_id,
|
||||
ttl_minutes=lease_duration_minutes,
|
||||
)
|
||||
if lease is not None:
|
||||
selected = candidate
|
||||
selected_state = live_state
|
||||
break
|
||||
diagnostics.append(
|
||||
{
|
||||
"accountId": int(account.id),
|
||||
"accountName": live_state.account_name,
|
||||
"ck": live_state.ck_verdict,
|
||||
"eliteOpened": live_state.elite_opened,
|
||||
"points": live_state.points,
|
||||
"reasons": ["账号刚被其他任务占用"],
|
||||
"source": "capability" if capability_first else "live",
|
||||
}
|
||||
)
|
||||
if selected is None or selected_state is None or lease is None:
|
||||
task.execution_status = "ready"
|
||||
task.next_allocate_at = _now() + timedelta(seconds=45)
|
||||
task.issue_code = "no_eligible_account"
|
||||
if not candidates:
|
||||
task.last_error = (
|
||||
"商品能力池暂无可用账号"
|
||||
if mode == "capability"
|
||||
else "没有带 CK、允许自动管理且未停用的斗鱼账号"
|
||||
)
|
||||
else:
|
||||
summary = ";".join(
|
||||
f"{item['accountName']}: {', '.join(item['reasons'][:3])}"
|
||||
for item in diagnostics[:5]
|
||||
)
|
||||
task.last_error = (
|
||||
f"已实时检测 {len(diagnostics)} 个账号,当前没有可用账号"
|
||||
+ (f"。{summary}" if summary else "")
|
||||
)[:500]
|
||||
task.updated_at = _now()
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"account_allocation_failed",
|
||||
detail={
|
||||
"candidateCount": len(candidates),
|
||||
"matchingMode": mode,
|
||||
"capabilityCandidates": len(capability_candidates),
|
||||
"diagnostics": diagnostics,
|
||||
},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
task.account_id = selected.account_id
|
||||
task.lease_id = lease.id
|
||||
task.operator_id = current.id
|
||||
task.execution_status = "allocated"
|
||||
task.next_allocate_at = None
|
||||
task.binding_status = "not_started"
|
||||
soft_reasons = selected_state.soft_requirement_reasons(
|
||||
required_points=required_points
|
||||
)
|
||||
gate_status, gate_issue, gate_message = _exchange_gate(
|
||||
selected_state,
|
||||
task.commodity_id,
|
||||
allow_not_open=is_presale,
|
||||
)
|
||||
if gate_status:
|
||||
task.exchange_status = gate_status
|
||||
task.issue_code = gate_issue
|
||||
task.last_error = gate_message
|
||||
elif selected_state.elite_opened is not True:
|
||||
task.exchange_status = "not_opened"
|
||||
task.issue_code = "elite_not_opened"
|
||||
task.last_error = "账号尚未开通精英宝典,可开通后继续"
|
||||
elif selected_state.points is None or selected_state.points < required_points:
|
||||
task.exchange_status = "points_short"
|
||||
task.issue_code = "points_short"
|
||||
task.last_error = "账号积分不足,可充值后继续"
|
||||
else:
|
||||
task.exchange_status = "eligible"
|
||||
task.issue_code = ""
|
||||
task.last_error = ""
|
||||
task.updated_at = _now()
|
||||
_apply_task_lease_policy(db, task, lease=lease)
|
||||
_append_event(
|
||||
# 阶段 5:生产选号只允许读取本地能力索引。官网查询由
|
||||
# account_sync_queue/商品能力补货完成,绑码与兑换前各自实时复核。
|
||||
return _allocate_task_from_capability(
|
||||
db,
|
||||
task.id,
|
||||
"account_allocated",
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
detail={
|
||||
"accountId": selected.account_id,
|
||||
"accountName": selected.display_name,
|
||||
"points": selected.points,
|
||||
"eliteOpened": selected_state.elite_opened,
|
||||
"softReasons": soft_reasons,
|
||||
"exchangeStatus": task.exchange_status,
|
||||
},
|
||||
task=task,
|
||||
current=current,
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
|
||||
def release_task_account(
|
||||
|
||||
@@ -414,10 +414,10 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
return map;
|
||||
}, [isEsportsHandbook, tasks]);
|
||||
|
||||
const latestLimitedGoodsTaskByAccount = useMemo(() => {
|
||||
const latestOverviewTaskByAccount = useMemo(() => {
|
||||
const map = new Map<number, DouyuTaskItem>();
|
||||
for (const task of tasks) {
|
||||
if (task.task_type !== 'query_limited_goods') continue;
|
||||
if (task.task_type !== 'query_account_overview') continue;
|
||||
if (!['success', 'failed', 'error'].includes(task.status)) continue;
|
||||
const previous = map.get(task.account_id);
|
||||
if (!previous || task.id > previous.id) map.set(task.account_id, task);
|
||||
@@ -1786,7 +1786,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
{
|
||||
title: '限制兑换', dataIndex: 'exchange_balance', width: 160, ellipsis: true,
|
||||
render: (_, record) => {
|
||||
const latestLimitedTask = latestLimitedGoodsTaskByAccount.get(record.id);
|
||||
const latestLimitedTask = latestOverviewTaskByAccount.get(record.id);
|
||||
const text = limitedGoodsTaskText(latestLimitedTask);
|
||||
if (!text) return <Text type="secondary">未查</Text>;
|
||||
if (latestLimitedTask?.status !== 'success') {
|
||||
@@ -2479,7 +2479,6 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
<div style={actionGridStyle}>
|
||||
{renderActionButton('refresh_goods')}
|
||||
{renderActionButton('lock_goods')}
|
||||
{renderActionButton('exchange_goods', 'primary')}
|
||||
</div>
|
||||
|
||||
@@ -45,7 +45,6 @@ export const ELITE_QUICK_ACTIONS = [
|
||||
{ key: 'query_points', icon: <SearchOutlined /> },
|
||||
{ key: 'query_game_name', icon: <SearchOutlined /> },
|
||||
{ key: 'query_change_bind_time', icon: <FieldTimeOutlined /> },
|
||||
{ key: 'query_limited_goods', icon: <SearchOutlined /> },
|
||||
{ key: 'query_gold_balance', icon: <SearchOutlined /> },
|
||||
{ key: 'lock_goods', icon: <ShoppingOutlined /> },
|
||||
{ key: 'exchange_goods', icon: <ShoppingOutlined /> },
|
||||
@@ -59,7 +58,6 @@ export const ESPORTS_QUICK_ACTIONS = [
|
||||
{ key: 'create_esports_qr', icon: <CreditCardOutlined /> },
|
||||
{ key: 'query_esports_points', icon: <SearchOutlined /> },
|
||||
{ key: 'query_change_bind_time', icon: <FieldTimeOutlined /> },
|
||||
{ key: 'query_limited_goods', icon: <SearchOutlined /> },
|
||||
{ key: 'query_gold_balance', icon: <SearchOutlined /> },
|
||||
{ key: 'refresh_esports_goods', icon: <ReloadOutlined /> },
|
||||
{ key: 'exchange_esports_goods', icon: <ShoppingOutlined /> },
|
||||
@@ -93,9 +91,7 @@ export const ELITE_TASK_TYPES = new Set([
|
||||
'exchange_goods',
|
||||
'query_game_name',
|
||||
'query_change_bind_time',
|
||||
'query_limited_goods',
|
||||
'query_gold_balance',
|
||||
'refresh_goods',
|
||||
'query_exchange_records',
|
||||
'prefetch_csrf_token',
|
||||
'query_account_overview',
|
||||
@@ -109,7 +105,6 @@ export const ESPORTS_TASK_TYPES = new Set([
|
||||
'query_esports_points',
|
||||
'query_gold_balance',
|
||||
'query_change_bind_time',
|
||||
'query_limited_goods',
|
||||
'refresh_esports_goods',
|
||||
'exchange_esports_goods',
|
||||
'create_gold_qr',
|
||||
|
||||
Reference in New Issue
Block a user