fix: 默认心跳先等够再报,新登录账号可以立刻开始

This commit is contained in:
2026-11-16 02:44:54 +08:00
parent ec41af9489
commit e27b3002a4
5 changed files with 62 additions and 22 deletions
+31 -12
View File
@@ -472,10 +472,11 @@ def _count_total(sent: int, progress: float, origin: float = 0.0) -> int:
def _jitter(seconds: int) -> float:
"""只往晚了偏,避免心跳比页面节奏更早发出。间隔为 0 时至少等 1 秒。"""
if seconds <= 0:
return 0.0
spread = min(3.0, max(0.0, float(seconds) * 0.2))
return max(0.0, float(seconds) + random.uniform(-spread, spread))
return 1.0
spread = min(3.0, max(0.3, float(seconds) * 0.1))
return float(seconds) + random.uniform(0.3, spread)
def _parse_minutes(value: str) -> int:
@@ -647,15 +648,21 @@ class StudySession:
refreshed = resp.headers.get("token") or resp.headers.get("Token")
if refreshed:
self._token = refreshed
if resp.status_code in (401, 403):
for domain in ("euniv.zte.com.cn", "api-mall.yxt.com"):
self._client.cookies.set("ELEARNING_00024", refreshed, domain=domain, path="/")
if resp.status_code == 401:
raise SessionExpired("学习平台登录已失效")
if resp.status_code == 403:
raise PermissionError("当前课程没有权限")
if resp.headers.get("content-type", "").startswith("application/json"):
try:
payload = resp.json()
if isinstance(payload, dict) and str(payload.get("code")) in {"401", "403"}:
raise SessionExpired("学习平台登录已失效")
except (json.JSONDecodeError, ValueError):
pass
payload = None
if isinstance(payload, dict) and str(payload.get("code")) == "401":
raise SessionExpired("学习平台登录已失效")
if isinstance(payload, dict) and str(payload.get("code")) == "403":
raise PermissionError("当前课程没有权限")
return resp
async def update_progress(self, kid: str, pkg_id: str = "") -> dict:
@@ -903,6 +910,11 @@ async def _study_video(session: StudySession, kid: str, pkg_id: str,
started = True
view = int(progress.get("viewSchedule") or view)
view += step
page_rhythm = interval > 0 and interval == step
if page_rhythm and await _heartbeat_wait(worker, interval, stop, hours, schedule):
return
if _halted(stop, hours):
return
try:
result = await session.submit_video_heartbeat(kid, pkg_id, step, view)
except Exception:
@@ -915,8 +927,9 @@ async def _study_video(session: StudySession, kid: str, pkg_id: str,
if schedule >= 100:
await hours.refresh()
return
if await _heartbeat_wait(worker, interval, stop, hours, schedule):
return
if not page_rhythm:
if await _heartbeat_wait(worker, 1 if interval <= 0 else interval, stop, hours, schedule):
return
async def _study_document(session: StudySession, kid: str, pkg_id: str,
@@ -941,6 +954,11 @@ async def _study_document(session: StudySession, kid: str, pkg_id: str,
return
started = True
view = int(progress.get("viewSchedule") or 1)
page_rhythm = interval > 0 and interval == step
if page_rhythm and await _heartbeat_wait(worker, interval, stop, hours, schedule):
return
if _halted(stop, hours):
return
try:
await session.submit_document_heartbeat(kid, pkg_id, page_size, step, view)
progress = await session.update_progress(kid, pkg_id)
@@ -952,8 +970,9 @@ async def _study_document(session: StudySession, kid: str, pkg_id: str,
if _completed(progress):
await hours.refresh()
return
if await _heartbeat_wait(worker, interval, stop, hours, schedule):
return
if not page_rhythm:
if await _heartbeat_wait(worker, 1 if interval <= 0 else interval, stop, hours, schedule):
return
async def _study_scorm(session: StudySession, kid: str, pkg_id: str,
@@ -1408,7 +1427,7 @@ class AutoStudyWorker:
except SessionExpired:
hours.user_stop.set()
_touch(worker, ok=False)
_set_user_status(employee_id, state="会话过期")
_set_user_status(employee_id, state="会话失效", message="请重新登录")
log.info("用户 %s 会话过期", employee_id)
except Exception as exc:
_touch(worker, ok=False)