72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
"""在后台线程启动斗鱼任务批次。
|
|
|
|
任务创建入口和执行入口分离,供旧工作台和直播账号履约共用同一套线程/注册表。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import os
|
|
import threading
|
|
|
|
from web.backend.database import SessionLocal
|
|
|
|
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,
|
|
) -> None:
|
|
"""创建执行线程;从工作线程调用时必须显式传入主事件循环和日志队列。"""
|
|
loop = loop or asyncio.get_running_loop()
|
|
log_queue = log_queue or asyncio.Queue()
|
|
thread_db = SessionLocal()
|
|
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"]
|