更新定时任务页面和前端构建

This commit is contained in:
2025-12-14 23:09:27 +08:00
parent 1ec0d80f6c
commit dab29347bd
18 changed files with 121 additions and 41 deletions

View File

@@ -6,7 +6,7 @@ import heapq
import os
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor, wait
from dataclasses import dataclass
import database
@@ -87,10 +87,25 @@ class TaskScheduler:
self._executor = ThreadPoolExecutor(max_workers=self._executor_max_workers, thread_name_prefix="TaskWorker")
self._old_executors = []
self._futures_lock = threading.Lock()
self._active_futures = set()
self._running = True
self._dispatcher_thread = threading.Thread(target=self._dispatch_loop, daemon=True, name="TaskDispatcher")
self._dispatcher_thread.start()
def _track_future(self, future) -> None:
with self._futures_lock:
self._active_futures.add(future)
try:
future.add_done_callback(self._untrack_future)
except Exception:
pass
def _untrack_future(self, future) -> None:
with self._futures_lock:
self._active_futures.discard(future)
def shutdown(self, timeout: float = 5.0):
"""停止调度器(用于进程退出清理)"""
with self._cond:
@@ -102,6 +117,21 @@ class TaskScheduler:
except Exception:
pass
# 等待已提交的任务收尾(最多等待 timeout 秒),避免遗留 active_task 干扰后续调度/测试
try:
deadline = time.time() + max(0.0, float(timeout or 0))
while True:
with self._futures_lock:
pending = [f for f in self._active_futures if not f.done()]
if not pending:
break
remaining = deadline - time.time()
if remaining <= 0:
break
wait(pending, timeout=remaining)
except Exception:
pass
try:
self._executor.shutdown(wait=False)
except Exception:
@@ -265,6 +295,7 @@ class TaskScheduler:
try:
future = self._executor.submit(self._run_task_wrapper, task)
self._track_future(future)
safe_set_task(task.account_id, future)
except Exception:
with self._cond:
@@ -835,4 +866,3 @@ def run_task(user_id, account_id, browse_type, enable_screenshot=True, source="m
finally:
pass