perf(douyu): 拆分任务槽并改造绑定扫码调度

This commit is contained in:
yml
2026-11-20 22:03:20 +08:00
parent 8869e427d2
commit 071d45ad1d
40 changed files with 1635 additions and 245 deletions
+34 -33
View File
@@ -5,7 +5,7 @@ import os
import threading
import time
import uuid
from concurrent.futures import ThreadPoolExecutor, as_completed
from concurrent.futures import as_completed
from datetime import UTC, datetime
from types import SimpleNamespace
from typing import cast
@@ -32,6 +32,7 @@ from .douyu_account_probe import persist_probe_result, probe_session
from .douyu_service import LOGIN_ATTEMPT_IN_FLIGHT_STATUSES
from .login_failure import classify_failure, failure_kind_label, is_account_fault
from .proxy_service import build_proxy_runtime
from .task_runtime import TaskLane, submit
CHECK_STATUS_MESSAGES = {
"account_cancelled": "账号已注销",
@@ -296,7 +297,7 @@ def _persist_login_cookie_verification(
class LoginBatchRunner:
"""批量登录执行器,在线程中运行,通过 ThreadPoolExecutor 并发登录多个账号。"""
"""批量登录执行器,通过进程级登录槽并发执行账号。"""
def __init__(
self,
@@ -357,7 +358,7 @@ class LoginBatchRunner:
message: str = "重新登录已停止,旧 Cookie 已保留",
) -> None:
"""把未开始或已中断的重登尝试收敛为终态,旧 CK 原封不动。"""
worker_db = SessionLocal()
worker_db = WorkerSessionLocal()
try:
task = worker_db.query(LoginTask).filter(LoginTask.id == task_id).first()
if task and _attempt_in_flight(task):
@@ -376,7 +377,7 @@ class LoginBatchRunner:
与 `_mark_relogin_stopped` 的区别只是入口:批次挂了但记录还没建出来时,
这里是唯一能把「该账号的在途尝试」收干净的路径。
"""
worker_db = SessionLocal()
worker_db = WorkerSessionLocal()
try:
tasks = (
worker_db.query(LoginTask)
@@ -432,7 +433,7 @@ class LoginBatchRunner:
self._mark_relogin_stopped(task_id, "任务已停止,旧 Cookie 已保留")
return
worker_db = SessionLocal()
worker_db = WorkerSessionLocal()
try:
task = worker_db.query(LoginTask).filter(LoginTask.id == task_id).first()
if not task:
@@ -774,33 +775,33 @@ class LoginBatchRunner:
self._push_log("result", "")
return
# 并发执行登录,每个账号独立获取代理
with ThreadPoolExecutor(max_workers=concurrency) as executor:
futures = []
for item_index, item in enumerate(task_infos):
if self._stop.is_set():
self._push_log("warning", "任务已停止,跳过剩余账号")
# 所有模式都要把没轮到的尝试收敛掉,否则会一直停在
# 「等待中」;已有 CK 的记录本身不受影响。
for pending_item in task_infos[item_index:]:
self._mark_relogin_stopped(
pending_item["task_id"], "任务已停止,旧 Cookie 已保留"
)
break
future = executor.submit(
self._execute_one,
item["task_id"],
item["acc_info"],
total,
)
futures.append(future)
# 全局登录槽负责并发;批次只提交任务并收集结果。
futures = []
for item_index, item in enumerate(task_infos):
if self._stop.is_set():
self._push_log("warning", "任务已停止,跳过剩余账号")
for pending_item in task_infos[item_index:]:
self._mark_relogin_stopped(
pending_item["task_id"], "任务已停止,旧 Cookie 已保留"
)
break
future = submit(
TaskLane.LOGIN,
self._execute_one,
item["task_id"],
item["acc_info"],
total,
stopped=self._stop.is_set,
)
if future is None:
break
futures.append(future)
# 等待所有任务完成
for future in as_completed(futures):
try:
future.result()
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
self._push_log("error", f"Worker 异常: {e}")
for future in as_completed(futures):
try:
future.result()
except Exception as e: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
self._push_log("error", f"Worker 异常: {e}")
self._push_log("info", f"批量{action_name}任务 {batch_id} 完成")
self._push_log("result", "")
@@ -902,5 +903,5 @@ def cleanup_orphan_login_tasks(
return _converge_interrupted_attempts(db, message)
# 在模块末尾导入 SessionLocal(避免循环导入)
from ..database import SessionLocal
# 在模块末尾导入 WorkerSessionLocal(避免循环导入)
from ..database import WorkerSessionLocal