fix: 进度连续五分钟不动就放弃这门课

This commit is contained in:
2026-11-16 02:51:52 +08:00
parent 7782bba8d8
commit 1da76931f8
2 changed files with 36 additions and 0 deletions
+23
View File
@@ -210,6 +210,23 @@ def _progress_of(payload: dict) -> float:
return 0.0 return 0.0
class _Stall:
"""累计申报满 5 分钟仍无进度时放弃,避免短间隔把一门课打成死循环。"""
def __init__(self) -> None:
self.declared = 0
self.baseline: float | None = None
def stuck(self, progress: float, sent: int) -> bool:
if self.baseline is None:
self.baseline = progress
elif progress > self.baseline + 0.01:
self.baseline = progress
self.declared = 0
self.declared += max(0, sent)
return self.declared >= 300 and progress <= self.baseline + 0.01
def _completed(payload: dict) -> bool: def _completed(payload: dict) -> bool:
if not isinstance(payload, dict): if not isinstance(payload, dict):
return False return False
@@ -912,6 +929,7 @@ async def _study_video(session: StudySession, kid: str, pkg_id: str,
step = 30 step = 30
view = 0 view = 0
started = False started = False
stall = _Stall()
while not _halted(stop, hours): while not _halted(stop, hours):
try: try:
progress = await session.update_progress(kid, pkg_id) progress = await session.update_progress(kid, pkg_id)
@@ -944,6 +962,8 @@ async def _study_video(session: StudySession, kid: str, pkg_id: str,
if schedule >= 100: if schedule >= 100:
await hours.refresh() await hours.refresh()
return return
if stall.stuck(schedule, step):
raise RuntimeError("进度长时间没有变化")
if not page_rhythm: if not page_rhythm:
if await _heartbeat_wait(worker, 1 if interval <= 0 else interval, stop, hours, schedule): if await _heartbeat_wait(worker, 1 if interval <= 0 else interval, stop, hours, schedule):
return return
@@ -957,6 +977,7 @@ async def _study_document(session: StudySession, kid: str, pkg_id: str,
if step <= 0: if step <= 0:
step = 120 step = 120
started = False started = False
stall = _Stall()
while not _halted(stop, hours): while not _halted(stop, hours):
try: try:
progress = await session.update_progress(kid, pkg_id) progress = await session.update_progress(kid, pkg_id)
@@ -987,6 +1008,8 @@ async def _study_document(session: StudySession, kid: str, pkg_id: str,
if _completed(progress): if _completed(progress):
await hours.refresh() await hours.refresh()
return return
if stall.stuck(schedule, step):
raise RuntimeError("进度长时间没有变化")
if not page_rhythm: if not page_rhythm:
if await _heartbeat_wait(worker, 1 if interval <= 0 else interval, stop, hours, schedule): if await _heartbeat_wait(worker, 1 if interval <= 0 else interval, stop, hours, schedule):
return return
+13
View File
@@ -23,6 +23,7 @@ from euniv.autostudy import (
_play_path, _play_path,
_set_user_status, _set_user_status,
_set_worker_status, _set_worker_status,
_Stall,
_study_document, _study_document,
_study_video, _study_video,
autostudy_status, autostudy_status,
@@ -121,6 +122,18 @@ def test_wait_stop_timeout_and_event() -> None:
asyncio.run(main()) asyncio.run(main())
def test_stall_gives_up_after_five_minutes_without_progress() -> None:
stall = _Stall()
assert stall.stuck(10, 120) is False
assert stall.stuck(10, 120) is False
assert stall.stuck(10, 120) is True
moved = _Stall()
assert moved.stuck(10, 120) is False
assert moved.stuck(40, 200) is False
assert moved.stuck(40, 99) is False
assert moved.stuck(40, 1) is True
def test_jitter_never_sends_early() -> None: def test_jitter_never_sends_early() -> None:
assert _jitter(0) == 1 assert _jitter(0) == 1
assert _jitter(-1) == 1 assert _jitter(-1) == 1