213 lines
6.0 KiB
Python
213 lines
6.0 KiB
Python
"""Database-backed scheduler for scan-wait binding tasks."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import threading
|
|
from concurrent.futures import Future
|
|
from datetime import UTC, datetime, timedelta
|
|
|
|
from loguru import logger
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
from ..database import WorkerSessionLocal
|
|
from ..models import DouyuTask
|
|
from .douyu_runner import DouyuBatchRunner
|
|
from .task_runtime import TaskLane, submit
|
|
|
|
DEFAULT_INTERVAL_SECONDS = 1
|
|
DEFAULT_BATCH_SIZE = 20
|
|
CLAIM_SECONDS = 60
|
|
|
|
_stop_event = threading.Event()
|
|
_thread: threading.Thread | None = None
|
|
_lock = threading.Lock()
|
|
_in_flight: set[int] = set()
|
|
_in_flight_lock = threading.Lock()
|
|
|
|
|
|
def _env_int(name: str, default: int, minimum: int, maximum: int) -> int:
|
|
try:
|
|
value = int(os.getenv(name, str(default)))
|
|
except ValueError:
|
|
return default
|
|
return max(minimum, min(maximum, value))
|
|
|
|
|
|
def _claim_due_binding_tasks(limit: int) -> list[int]:
|
|
db = WorkerSessionLocal()
|
|
now = datetime.now(UTC)
|
|
claimed: list[int] = []
|
|
try:
|
|
rows = (
|
|
db.query(DouyuTask)
|
|
.filter(
|
|
DouyuTask.task_type == "get_bind_qr",
|
|
DouyuTask.status == "running",
|
|
DouyuTask.queue_lane == "binding_poll",
|
|
DouyuTask.next_run_at.isnot(None),
|
|
DouyuTask.next_run_at <= now,
|
|
)
|
|
.order_by(DouyuTask.next_run_at.asc(), DouyuTask.id.asc())
|
|
.with_for_update(skip_locked=True)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
for row in rows:
|
|
task_id = int(row.id)
|
|
with _in_flight_lock:
|
|
if task_id in _in_flight:
|
|
continue
|
|
_in_flight.add(task_id)
|
|
row.next_run_at = now + timedelta(seconds=CLAIM_SECONDS)
|
|
claimed.append(task_id)
|
|
db.commit()
|
|
return claimed
|
|
except Exception:
|
|
db.rollback()
|
|
with _in_flight_lock:
|
|
for task_id in claimed:
|
|
_in_flight.discard(task_id)
|
|
raise
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def poll_binding_task_once(task_id: int) -> None:
|
|
db = WorkerSessionLocal()
|
|
try:
|
|
task = (
|
|
db.query(DouyuTask)
|
|
.options(joinedload(DouyuTask.account))
|
|
.filter(DouyuTask.id == int(task_id))
|
|
.first()
|
|
)
|
|
if (
|
|
task is None
|
|
or task.status != "running"
|
|
or task.task_type != "get_bind_qr"
|
|
or task.queue_lane != "binding_poll"
|
|
):
|
|
return
|
|
account = task.account
|
|
if account is None or not (account.cookie or ""):
|
|
task.queue_lane = ""
|
|
task.next_run_at = None
|
|
runner = DouyuBatchRunner(
|
|
db,
|
|
task.batch_id,
|
|
"get_bind_qr",
|
|
close_db=False,
|
|
)
|
|
runner._mark_task(db, task, "failed", "账号 Cookie 已失效,无法继续检测绑定")
|
|
return
|
|
runner = DouyuBatchRunner(
|
|
db,
|
|
task.batch_id,
|
|
"get_bind_qr",
|
|
close_db=False,
|
|
)
|
|
config = runner._config_info(db)
|
|
runner.poll_bind_role_once(db, task, account, config)
|
|
except Exception as exc: # noqa: BLE001 - the claimed row is retried later
|
|
db.rollback()
|
|
logger.warning("绑定扫码定时检测失败 task_id={}: {}", task_id, exc)
|
|
finally:
|
|
db.close()
|
|
with _in_flight_lock:
|
|
_in_flight.discard(int(task_id))
|
|
|
|
|
|
def _release_callback(_future: Future) -> None:
|
|
try:
|
|
_future.result()
|
|
except Exception as exc: # noqa: BLE001 - worker already logs; keep scheduler alive
|
|
logger.warning("绑定扫码任务异常: {}", exc)
|
|
|
|
|
|
def run_binding_poll_dispatch_once(limit: int = DEFAULT_BATCH_SIZE) -> int:
|
|
claimed = _claim_due_binding_tasks(max(1, limit))
|
|
submitted = 0
|
|
for task_id in claimed:
|
|
future = submit(
|
|
TaskLane.BINDING_POLL,
|
|
poll_binding_task_once,
|
|
task_id,
|
|
stopped=_stop_event.is_set,
|
|
)
|
|
if future is None:
|
|
with _in_flight_lock:
|
|
_in_flight.discard(task_id)
|
|
break
|
|
future.add_done_callback(_release_callback)
|
|
submitted += 1
|
|
return submitted
|
|
|
|
|
|
def _run() -> None:
|
|
interval = _env_int(
|
|
"BINDING_POLL_DISPATCH_SECONDS",
|
|
DEFAULT_INTERVAL_SECONDS,
|
|
1,
|
|
30,
|
|
)
|
|
while not _stop_event.is_set():
|
|
try:
|
|
batch_size = _env_int(
|
|
"BINDING_POLL_DISPATCH_BATCH",
|
|
DEFAULT_BATCH_SIZE,
|
|
1,
|
|
200,
|
|
)
|
|
run_binding_poll_dispatch_once(batch_size)
|
|
except Exception as exc: # noqa: BLE001 - scheduler must survive one bad tick
|
|
logger.warning("绑定扫码调度轮次失败: {}", exc)
|
|
_stop_event.wait(interval)
|
|
|
|
|
|
def start_binding_poll_scheduler() -> bool:
|
|
global _thread
|
|
with _lock:
|
|
if _thread is not None and _thread.is_alive():
|
|
return False
|
|
_stop_event.clear()
|
|
_thread = threading.Thread(
|
|
target=_run,
|
|
name="binding-poll-scheduler",
|
|
daemon=True,
|
|
)
|
|
_thread.start()
|
|
logger.info("绑定扫码定时调度器已启动")
|
|
return True
|
|
|
|
|
|
def stop_binding_poll_scheduler() -> None:
|
|
global _thread
|
|
_stop_event.set()
|
|
with _lock:
|
|
thread = _thread
|
|
_thread = None
|
|
if thread is not None and thread.is_alive():
|
|
thread.join(timeout=3)
|
|
|
|
|
|
def scheduler_status() -> dict[str, object]:
|
|
with _lock:
|
|
running = _thread is not None and _thread.is_alive()
|
|
with _in_flight_lock:
|
|
in_flight = len(_in_flight)
|
|
return {
|
|
"running": running,
|
|
"in_flight": in_flight,
|
|
"claim_seconds": CLAIM_SECONDS,
|
|
}
|
|
|
|
|
|
__all__ = [
|
|
"poll_binding_task_once",
|
|
"run_binding_poll_dispatch_once",
|
|
"scheduler_status",
|
|
"start_binding_poll_scheduler",
|
|
"stop_binding_poll_scheduler",
|
|
]
|