- 按活动 alias 与 cjm 双角色快照建立绑定 baseline,避免旧角色被误判为本次扫码 - 确认绑定任务固化目标角色,禁止回退确认旧角色 - 绑定失败状态优先于旧待确认标记,并支持客服重置客户角色资料 - 取消任务改为终态幂等操作,停止关联批次并默认释放账号和租约 - 补充双角色选择、重置绑定和取消释放账号测试
53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
"""在后台线程启动斗鱼任务批次。
|
|
|
|
任务创建入口和执行入口分离,供旧工作台和直播账号履约共用同一套线程/注册表。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import threading
|
|
|
|
from web.backend.database import SessionLocal
|
|
|
|
from .douyu_runner import DouyuBatchRunner, douyu_batch_registry
|
|
|
|
|
|
def start_douyu_batch(
|
|
*,
|
|
batch_id: str,
|
|
task_type: str,
|
|
payload: dict,
|
|
concurrency: int,
|
|
) -> None:
|
|
"""必须在运行中的 asyncio 事件循环里调用。"""
|
|
log_queue = asyncio.Queue()
|
|
loop = asyncio.get_running_loop()
|
|
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=max(1, min(int(concurrency), 10)),
|
|
)
|
|
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"]
|