feat(automation): 持久执行队列与重复充值确认

This commit is contained in:
yml
2026-11-23 15:47:56 +08:00
parent 2549511598
commit aaacbea10a
29 changed files with 2147 additions and 178 deletions
+18 -5
View File
@@ -5,6 +5,7 @@ from __future__ import annotations
import asyncio
import threading
import time
from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from typing import TYPE_CHECKING, Any
@@ -106,6 +107,7 @@ class DouyuBatchRunnerCore:
loop: asyncio.AbstractEventLoop | None = None,
concurrency: int = 3,
close_db: bool = True,
log_sink: Callable[[dict[str, Any]], None] | None = None,
):
self.db = db
self.close_db = close_db
@@ -115,6 +117,7 @@ class DouyuBatchRunnerCore:
self.log_queue = log_queue
self.loop = loop
self.concurrency = clamp_douyu_task_concurrency(task_type, concurrency)
self.log_sink = log_sink
self._stop = threading.Event()
self._counter_lock = threading.Lock()
self._started = 0
@@ -179,6 +182,11 @@ class DouyuBatchRunnerCore:
self.log_queue.put({"level": level, "message": message}),
self.loop,
)
if self.log_sink is not None:
try:
self.log_sink({"level": level, "message": message})
except Exception: # noqa: BLE001 - 日志落库不能中断任务
logger.exception("写入斗鱼批次持久日志失败: {}", self.batch_id)
@staticmethod
def _account_name(account: Account) -> str:
@@ -209,8 +217,6 @@ class DouyuBatchRunnerCore:
def _push_task_event(self, task: DouyuTask) -> None:
"""向批次 WS 推送任务状态事件(level=task),前端即时更新不依赖轮询。"""
if not self.log_queue or not self.loop:
return
try:
payload = douyu_task_payload(task)
except Exception: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
@@ -221,7 +227,13 @@ class DouyuBatchRunnerCore:
"message": "",
"task": payload,
}
asyncio.run_coroutine_threadsafe(self.log_queue.put(event), self.loop)
if self.log_queue and self.loop:
asyncio.run_coroutine_threadsafe(self.log_queue.put(event), self.loop)
if self.log_sink is not None:
try:
self.log_sink(event)
except Exception: # noqa: BLE001 - 日志落库不能中断任务
logger.exception("写入斗鱼任务事件失败: {}", self.batch_id)
def _sync_platform_fulfillment(self, douyu_task_id: int, *, enabled: bool) -> None:
"""绑定/确认任务完成后立即刷新履约投影并写 Outbox。"""
@@ -358,8 +370,9 @@ class DouyuBatchRunnerCore:
task.next_run_at = now + timedelta(seconds=max(1, int(delay_seconds)))
task.last_run_at = now
if task.run_deadline_at is None:
task.run_deadline_at = now + timedelta(seconds=DOUYU_PAYMENT_POLL_SECONDS)
result["payment_deadline_at"] = task.run_deadline_at.isoformat()
deadline = now + timedelta(seconds=DOUYU_PAYMENT_POLL_SECONDS)
task.run_deadline_at = deadline
result["payment_deadline_at"] = deadline.isoformat()
self._update_task_progress(db, task, "running", message, result)
def _update_task_progress(