Files
live-hub-py/web/backend/services/douyu_batch_launcher.py
T

78 lines
2.2 KiB
Python

"""在后台线程启动斗鱼任务批次。
任务创建入口和执行入口分离,供旧工作台和直播账号履约共用同一套线程/注册表。
"""
from __future__ import annotations
import asyncio
import os
import threading
from web.backend.database import WorkerSessionLocal
from .douyu_concurrency import clamp_batch_task_concurrency
from .douyu_runner import DouyuBatchRunner, douyu_batch_registry
def _env_positive_int(name: str, default: int) -> int:
try:
value = int(os.getenv(name, str(default)))
except ValueError:
return default
return value if value > 0 else default
DOUYU_BATCH_CREATE_CONCURRENCY = _env_positive_int(
"DOUYU_BATCH_CREATE_CONCURRENCY",
2,
)
DOUYU_MAX_ACTIVE_BATCHES = _env_positive_int("DOUYU_MAX_ACTIVE_BATCHES", 10)
def start_douyu_batch(
*,
batch_id: str,
task_type: str,
payload: dict,
concurrency: int,
loop: asyncio.AbstractEventLoop | None = None,
log_queue: asyncio.Queue | None = None,
capture_event_loop: bool = True,
) -> None:
"""Create the batch coordinator; websocket logging is optional."""
if loop is None and capture_event_loop:
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None
if log_queue is None and loop is not None:
log_queue = asyncio.Queue()
thread_db = WorkerSessionLocal()
runner = DouyuBatchRunner(
db=thread_db,
batch_id=batch_id,
task_type=task_type,
payload=payload,
log_queue=log_queue,
loop=loop,
concurrency=clamp_batch_task_concurrency(concurrency),
)
douyu_batch_registry.register(batch_id, log_queue, loop, runner)
threading.Thread(target=runner.run, daemon=True).start()
def stop_douyu_batches(batch_ids: list[str]) -> int:
"""向仍在运行的批次发送停止信号,返回实际命中的批次数。"""
stopped = 0
for batch_id in dict.fromkeys(value for value in batch_ids if value):
batch = douyu_batch_registry.get(batch_id)
if not batch or batch.get("finished"):
continue
batch["runner"].stop()
stopped += 1
return stopped
__all__ = ["start_douyu_batch", "stop_douyu_batches"]