perf(fulfillment): 优化本地能力选号查询与索引

This commit is contained in:
yml
2026-11-23 18:53:06 +08:00
parent d5422b0ecd
commit 7e0d27927c
23 changed files with 1624 additions and 95 deletions
+60 -1
View File
@@ -43,6 +43,16 @@ if TYPE_CHECKING:
DOUYU_PAYMENT_POLL_SECONDS = 600
DOUYU_PAYMENT_POLL_INTERVAL = 5
AUTO_EXCHANGE_ELIGIBILITY_TASK_TYPES = frozenset(
{
"create_gold_qr",
"donate_elite_gift",
"create_elite_qr",
"confirm_bind",
"query_game_name",
}
)
# 走代理的任务类型:写操作/消耗类(兑换、锁单、支付、开通、充值、送礼)。
# 查询/刷新/绑定/扫码等读操作始终直连——它们不怕 IP 频控,走代理只会浪费代理额度。
DOUYU_PROXY_TASK_TYPES = {
@@ -255,6 +265,40 @@ class DouyuBatchRunnerCore:
"[douyu] 同步直播账号履约状态失败: task_id={}", douyu_task_id
)
def _refresh_platform_exchange_eligibility(
self,
douyu_task_id: int,
*,
enabled: bool,
) -> None:
"""账号积分/宝典变化后,自动刷新当前履约任务的兑换投影。"""
if not enabled:
return
try:
from ..database import WorkerSessionLocal
from .platform_fulfillment.service import (
refresh_active_exchange_eligibility_for_account,
)
refresh_db = WorkerSessionLocal()
try:
task = refresh_db.get(DouyuTask, int(douyu_task_id))
if task is None or task.status != "success":
return
refresh_active_exchange_eligibility_for_account(
refresh_db,
account_id=int(task.account_id),
scope=str(task.handbook_scope or "elite"),
actor_id=int(task.created_by),
)
finally:
refresh_db.close()
except Exception: # noqa: BLE001 - 自动刷新不能覆盖任务执行结果
logger.exception(
"[douyu] 自动刷新直播履约兑换资格失败: task_id={}",
douyu_task_id,
)
def _sync_platform_fulfillment_operation(
self,
db: Session,
@@ -285,8 +329,13 @@ class DouyuBatchRunnerCore:
message: str,
result: dict | None = None,
) -> None:
from .platform_fulfillment.service import requires_account_binding_sync
task_id = int(task.id)
has_platform_task = task.platform_task_id is not None
sync_platform_fulfillment = has_platform_task or requires_account_binding_sync(
task.task_type
)
task.status = status
task.message = message[:512]
if status in {"success", "failed", "error", "stopped"}:
@@ -352,7 +401,17 @@ class DouyuBatchRunnerCore:
logger.exception(
"[douyu] 写入账号任务结果事件失败: task_id={}", task.id
)
self._sync_platform_fulfillment(task_id, enabled=has_platform_task)
self._sync_platform_fulfillment(
task_id,
enabled=sync_platform_fulfillment,
)
self._refresh_platform_exchange_eligibility(
task_id,
enabled=(
status == "success"
and task.task_type in AUTO_EXCHANGE_ELIGIBILITY_TASK_TYPES
),
)
def _schedule_payment_poll(
self,