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

110 lines
3.1 KiB
Python

"""在后台线程启动斗鱼任务批次。
任务创建入口和执行入口分离,供旧工作台和直播账号履约共用同一套线程/注册表。
"""
from __future__ import annotations
import asyncio
import os
from web.backend.database import WorkerSessionLocal
from .douyu_concurrency import clamp_douyu_task_concurrency
from .douyu_runner import DouyuBatchRunner, douyu_batch_registry
from .task_runtime import TaskLane, submit
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 _run_batch(
*,
batch_id: str,
task_type: str,
payload: dict,
concurrency: int,
loop: asyncio.AbstractEventLoop | None,
log_queue: asyncio.Queue | None,
) -> None:
"""Run one coordinator with a short-lived session owned by the worker."""
thread_db = WorkerSessionLocal()
try:
runner = DouyuBatchRunner(
db=thread_db,
batch_id=batch_id,
task_type=task_type,
payload=payload,
log_queue=log_queue,
loop=loop,
concurrency=clamp_douyu_task_concurrency(task_type, concurrency),
)
douyu_batch_registry.register(batch_id, log_queue, loop, runner)
runner.run()
finally:
thread_db.close()
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,
) -> bool:
"""Queue a batch coordinator without creating a thread per order.
The coordinator uses the dedicated bounded lane and opens its database
session only after a worker slot is available. Callers may therefore
safely invoke this from a request thread without holding a database
connection while waiting in the queue.
"""
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()
future = submit(
TaskLane.BATCH_COORDINATOR,
_run_batch,
batch_id=batch_id,
task_type=task_type,
payload=payload,
concurrency=concurrency,
loop=loop,
log_queue=log_queue,
)
return future is not None
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"]