fix: 停止后立刻再开始不会被收尾任务吞掉
This commit is contained in:
+36
-12
@@ -55,7 +55,9 @@ _lock = threading.Lock()
|
||||
_status: dict = {"running": False, "users": {}, "message": "", "updated_at": ""}
|
||||
_halt_lock = threading.Lock()
|
||||
_user_halts: dict[str, asyncio.Event] = {}
|
||||
_wanted: set[str] = set()
|
||||
_wanted: dict[str, int] = {}
|
||||
_wanted_seq = 0
|
||||
_running: set[str] = set()
|
||||
_worker = None
|
||||
|
||||
|
||||
@@ -244,15 +246,26 @@ def _current_intervals() -> tuple[dict, bool]:
|
||||
}, cfg["mode"] == "auto"
|
||||
|
||||
|
||||
def _signal_stop(event: asyncio.Event | None) -> None:
|
||||
if event is None:
|
||||
return
|
||||
worker = _worker
|
||||
loop = getattr(worker, "_loop", None) if worker is not None else None
|
||||
if loop is not None and loop.is_running():
|
||||
loop.call_soon_threadsafe(event.set)
|
||||
return
|
||||
event.set()
|
||||
|
||||
|
||||
def halt_work(employee_id: str) -> None:
|
||||
key = (employee_id or "").strip()
|
||||
if not key:
|
||||
return
|
||||
drop_work(key)
|
||||
with _halt_lock:
|
||||
_wanted.pop(key, None)
|
||||
_running.discard(key)
|
||||
event = _user_halts.get(key)
|
||||
if event is not None:
|
||||
event.set()
|
||||
_signal_stop(event)
|
||||
_notify(key, urgent=True)
|
||||
|
||||
|
||||
@@ -288,8 +301,11 @@ def want_work(employee_id: str) -> None:
|
||||
key = (employee_id or "").strip()
|
||||
if not key:
|
||||
return
|
||||
global _wanted_seq
|
||||
with _halt_lock:
|
||||
_wanted.add(key)
|
||||
if key not in _wanted:
|
||||
_wanted_seq += 1
|
||||
_wanted[key] = _wanted_seq
|
||||
_notify(key, urgent=True)
|
||||
|
||||
|
||||
@@ -298,7 +314,7 @@ def drop_work(employee_id: str) -> None:
|
||||
if not key:
|
||||
return
|
||||
with _halt_lock:
|
||||
_wanted.discard(key)
|
||||
_wanted.pop(key, None)
|
||||
_notify(key, urgent=True)
|
||||
|
||||
|
||||
@@ -402,8 +418,7 @@ def _job_from_account(row) -> tuple | None:
|
||||
def is_studying(employee_id: str) -> bool:
|
||||
key = (employee_id or "").strip()
|
||||
with _halt_lock:
|
||||
event = _user_halts.get(key)
|
||||
return event is not None and not event.is_set()
|
||||
return key in _running
|
||||
|
||||
|
||||
# ── 课程路径解析 ──
|
||||
@@ -1269,12 +1284,14 @@ class AutoStudyWorker:
|
||||
drop_work(eid)
|
||||
return False
|
||||
want_work(eid)
|
||||
with _halt_lock:
|
||||
generation = _wanted.get(eid, 0)
|
||||
assert self._loop is not None
|
||||
self._tasks[eid] = self._loop.create_task(self._run_account(job, list(course_list)))
|
||||
self._tasks[eid] = self._loop.create_task(self._run_account(job, list(course_list), generation))
|
||||
log.info("启动账号 %s 的学习任务", eid)
|
||||
return True
|
||||
|
||||
async def _run_account(self, job: tuple, course_list: list[tuple[str, str, str]]) -> None:
|
||||
async def _run_account(self, job: tuple, course_list: list[tuple[str, str, str]], generation: int) -> None:
|
||||
emp_id, target, pw, cred, proxy, workers, use_pool = job
|
||||
try:
|
||||
from admin.settings import hours_source_of
|
||||
@@ -1295,7 +1312,12 @@ class AutoStudyWorker:
|
||||
else:
|
||||
_finish_user(emp_id)
|
||||
finally:
|
||||
drop_work(emp_id)
|
||||
with _halt_lock:
|
||||
current = _wanted.get(emp_id)
|
||||
if current == generation:
|
||||
drop_work(emp_id)
|
||||
elif current is not None and self._kick is not None:
|
||||
self._kick.set()
|
||||
self._tasks.pop(emp_id, None)
|
||||
|
||||
async def _study_user(self, employee_id: str, target: int,
|
||||
@@ -1306,9 +1328,10 @@ class AutoStudyWorker:
|
||||
user_stop = asyncio.Event()
|
||||
with _halt_lock:
|
||||
current = _user_halts.get(employee_id)
|
||||
if current is not None and not current.is_set():
|
||||
if current is not None and employee_id in _running:
|
||||
return
|
||||
_user_halts[employee_id] = user_stop
|
||||
_running.add(employee_id)
|
||||
try:
|
||||
async with sem:
|
||||
if user_stop.is_set():
|
||||
@@ -1348,6 +1371,7 @@ class AutoStudyWorker:
|
||||
with _halt_lock:
|
||||
if _user_halts.get(employee_id) is user_stop:
|
||||
_user_halts.pop(employee_id, None)
|
||||
_running.discard(employee_id)
|
||||
if use_pool:
|
||||
from euniv.proxy_pool import release
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ def fresh_database():
|
||||
from euniv import autostudy as autostudy_mod
|
||||
|
||||
autostudy_mod._wanted.clear()
|
||||
autostudy_mod._running.clear()
|
||||
autostudy_mod._user_halts.clear()
|
||||
users = autostudy_mod._status.get("users")
|
||||
if isinstance(users, dict):
|
||||
|
||||
Reference in New Issue
Block a user