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
+36 -27
View File
@@ -4,7 +4,7 @@ from __future__ import annotations
import threading
import uuid
from concurrent.futures import ThreadPoolExecutor, as_completed
from concurrent.futures import as_completed
from dataclasses import dataclass, field
from datetime import UTC, datetime
@@ -13,7 +13,7 @@ from core.huya.auto_register import HuyaAutoRegisterResult, register_huya_with_s
from core.huya.cookie_utils import normalize_huya_cookie
from core.sms_provider import SmsLine
from ..database import SessionLocal
from ..database import WorkerSessionLocal
from ..models import (
HuyaRegisterBatch as HuyaRegisterBatchModel,
)
@@ -28,6 +28,7 @@ from ..models import (
)
from .huya_service import upsert_huya_cookie
from .proxy_service import build_proxy_runtime
from .task_runtime import TaskLane, submit
# 运行中状态:服务中断后视为未完成,可继续
RUNNING_ITEM_STATUSES = frozenset({"sending", "waiting", "changing", "logging"})
@@ -209,7 +210,7 @@ def load_batch_snapshot(
batch_id: str, *, recover_interrupted: bool = True
) -> dict | None:
"""从数据库加载批次详情;若服务中断则标记为 interrupted。"""
db = SessionLocal()
db = WorkerSessionLocal()
try:
batch_row = (
db.query(HuyaRegisterBatchModel)
@@ -258,7 +259,7 @@ def list_batch_summaries(
) -> list[dict]:
"""列出最近的注册批次摘要。live_batch_ids 中的 running 保持运行中。"""
live = live_batch_ids or set()
db = SessionLocal()
db = WorkerSessionLocal()
try:
rows = (
db.query(HuyaRegisterBatchModel)
@@ -335,7 +336,7 @@ def export_success_logs_text(
limit: int = 5000,
) -> str:
"""从成功流水表导出 txt。"""
db = SessionLocal()
db = WorkerSessionLocal()
try:
query = db.query(HuyaRegisterSuccessLog).order_by(
HuyaRegisterSuccessLog.id.asc()
@@ -368,7 +369,7 @@ def list_success_logs(
limit: int = 200,
) -> list[dict]:
"""列出成功流水(含密码,供管理端展示/导出)。"""
db = SessionLocal()
db = WorkerSessionLocal()
try:
query = db.query(HuyaRegisterSuccessLog).order_by(
HuyaRegisterSuccessLog.id.desc()
@@ -450,7 +451,7 @@ class HuyaRegisterRunner:
"""把批次汇总写回数据库。"""
if not self.batch.db_id:
return
db = SessionLocal()
db = WorkerSessionLocal()
try:
row = (
db.query(HuyaRegisterBatchModel)
@@ -493,7 +494,7 @@ class HuyaRegisterRunner:
item = self.batch.items[index]
if not item.db_id:
return
db = SessionLocal()
db = WorkerSessionLocal()
try:
row = (
db.query(HuyaRegisterItemModel)
@@ -536,7 +537,7 @@ class HuyaRegisterRunner:
self, index: int, result: HuyaAutoRegisterResult
) -> tuple[int | None, str, str]:
"""成功时:写账号 + 成功流水(成功一个写一条,立即可导出)。"""
db = SessionLocal()
db = WorkerSessionLocal()
try:
# upsert_huya_cookie 内部会 commit 一次
account = upsert_huya_cookie(
@@ -737,22 +738,30 @@ class HuyaRegisterRunner:
self.batch.message = f"代理白名单预热失败: {msg}"
self._persist_batch_meta()
with ThreadPoolExecutor(max_workers=self.batch.concurrency) as executor:
futures = []
for index in indices:
if self._stop.is_set():
self._set_item(
index,
status="stopped",
message="已停止",
finished_at=_now(),
)
continue
item = self.sms_lines[index]
futures.append(executor.submit(self._run_one, index, item))
futures = []
for index in indices:
if self._stop.is_set():
self._set_item(
index,
status="stopped",
message="已停止",
finished_at=_now(),
)
continue
item = self.sms_lines[index]
future = submit(
TaskLane.HUYA_REGISTER,
self._run_one,
index,
item,
stopped=self._stop.is_set,
)
if future is None:
break
futures.append(future)
for future in as_completed(futures):
future.result()
for future in as_completed(futures):
future.result()
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
with self._lock:
self.batch.status = "error"
@@ -811,7 +820,7 @@ class HuyaRegisterRegistry:
for index, item in enumerate(sms_lines)
]
db = SessionLocal()
db = WorkerSessionLocal()
try:
batch_row = HuyaRegisterBatchModel(
batch_id=batch_id,
@@ -871,7 +880,7 @@ class HuyaRegisterRegistry:
started_at=_now(),
)
# DB 同步为 running,避免返回 pending 导致前端误判
db = SessionLocal()
db = WorkerSessionLocal()
try:
row = (
db.query(HuyaRegisterBatchModel)
@@ -936,7 +945,7 @@ class HuyaRegisterRegistry:
empty_msg = "没有可继续的未完成/失败条目"
run_label = "续跑未完成"
db = SessionLocal()
db = WorkerSessionLocal()
try:
batch_row = (
db.query(HuyaRegisterBatchModel)