perf(douyu): 拆分支付轮询并扩展查询并发
This commit is contained in:
@@ -5,7 +5,7 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import threading
|
||||
import time
|
||||
from datetime import UTC, datetime
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from loguru import logger
|
||||
@@ -24,7 +24,7 @@ from ..models import (
|
||||
ProxyConfig as ProxyConfigModel,
|
||||
)
|
||||
from .douyu_account_probe import activity_client
|
||||
from .douyu_concurrency import clamp_batch_task_concurrency
|
||||
from .douyu_concurrency import clamp_douyu_task_concurrency
|
||||
from .douyu_goods_service import upsert_goods_snapshot
|
||||
from .douyu_service import (
|
||||
DOUYU_CONFIG_FIELDS,
|
||||
@@ -114,7 +114,7 @@ class DouyuBatchRunnerCore:
|
||||
self.payload = payload or {}
|
||||
self.log_queue = log_queue
|
||||
self.loop = loop
|
||||
self.concurrency = clamp_batch_task_concurrency(concurrency)
|
||||
self.concurrency = clamp_douyu_task_concurrency(task_type, concurrency)
|
||||
self._stop = threading.Event()
|
||||
self._counter_lock = threading.Lock()
|
||||
self._started = 0
|
||||
@@ -277,8 +277,18 @@ class DouyuBatchRunnerCore:
|
||||
has_platform_task = task.platform_task_id is not None
|
||||
task.status = status
|
||||
task.message = message[:512]
|
||||
if status in {"success", "failed", "error", "stopped"}:
|
||||
task.queue_lane = ""
|
||||
task.next_run_at = None
|
||||
task.run_deadline_at = None
|
||||
if result is not None:
|
||||
task.result = slim_douyu_task_result_for_storage(result)
|
||||
persisted = dict(result)
|
||||
if (
|
||||
status in {"success", "failed", "error", "stopped"}
|
||||
and "payment_polling" in persisted
|
||||
):
|
||||
persisted["payment_polling"] = False
|
||||
task.result = slim_douyu_task_result_for_storage(persisted)
|
||||
task.finished_at = datetime.now(UTC)
|
||||
account = (
|
||||
db.query(Account).filter(Account.id == task.account_id).first()
|
||||
@@ -332,6 +342,26 @@ class DouyuBatchRunnerCore:
|
||||
)
|
||||
self._sync_platform_fulfillment(task_id, enabled=has_platform_task)
|
||||
|
||||
def _schedule_payment_poll(
|
||||
self,
|
||||
db: Session,
|
||||
task: DouyuTask,
|
||||
result: dict,
|
||||
message: str,
|
||||
*,
|
||||
delay_seconds: int = DOUYU_PAYMENT_POLL_INTERVAL,
|
||||
) -> None:
|
||||
"""二维码已生成后释放 worker,由数据库调度器继续查询到账。"""
|
||||
now = datetime.now(UTC)
|
||||
result["payment_polling"] = True
|
||||
task.queue_lane = "payment_poll"
|
||||
task.next_run_at = now + timedelta(seconds=max(1, int(delay_seconds)))
|
||||
task.last_run_at = now
|
||||
if task.run_deadline_at is None:
|
||||
task.run_deadline_at = now + timedelta(seconds=DOUYU_PAYMENT_POLL_SECONDS)
|
||||
result["payment_deadline_at"] = task.run_deadline_at.isoformat()
|
||||
self._update_task_progress(db, task, "running", message, result)
|
||||
|
||||
def _update_task_progress(
|
||||
self,
|
||||
db: Session,
|
||||
@@ -428,13 +458,18 @@ class DouyuBatchRunnerCore:
|
||||
payload = payload_raw if isinstance(payload_raw, dict) else {}
|
||||
return {**payload, **self.payload}
|
||||
|
||||
def _client(self, cookie: str) -> DouyuActivityClient:
|
||||
def _client(
|
||||
self,
|
||||
cookie: str,
|
||||
*,
|
||||
direct: bool = False,
|
||||
) -> DouyuActivityClient:
|
||||
"""任务 HTTP 客户端:写操作任务注入会话级代理(兑换/锁单/支付/充值/送礼),
|
||||
读操作任务直连;见 DOUYU_PROXY_TASK_TYPES。"""
|
||||
读操作任务直连;持久支付轮询显式 ``direct=True``,不能每轮取新代理。"""
|
||||
return activity_client(
|
||||
cookie,
|
||||
logger_callback=lambda msg: self._push_log("debug", msg),
|
||||
proxies=self._proxies_for_task(),
|
||||
proxies=None if direct else self._proxies_for_task(),
|
||||
)
|
||||
|
||||
def _sleep_interruptible(self, seconds: float) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user