Files
void ae117222df feat: v2.8.0 无头运行器、考试自动交卷、改为退避重试不跳课
新增无头运行能力,解决 Linux 无图形化服务器上无法常驻的问题;同时改掉
"遇到考试课/无进展课就跳过"的策略,改为在同门课上退避重试。

- runner/:自己起无头 Chromium,用 CDP 把构建产物注入页面主世界(等价
  @grant none),不装插件、不需要桌面环境或 Xvfb;含浏览器探测与启动参数、
  持久化 profile、陈旧 SingletonLock 清理、优雅退出、日志轮转、启动预检
- runner.config.mjs / start-headless.cmd:通用配置 + Windows 一键入口;
  凭据只走 YUN_XUE_TANG_USR / YUN_XUE_TANG_PWD 环境变量
- src/exam.js:含考试章节的课程自动进考试 → 开始/继续 → 空卷提交 → 确定,
  交卷后靠平台记账推进课程;maxExamTries 限制同门课交卷次数,防不记账时死循环
- 去掉跳过与黑名单:没学完就退避重试(30s 起翻倍,最长 5 分钟),
  同门课超过 maxCourseRetries 次才停整轮并提示,替代原 maxConsecutiveFailures
- 每完成一门都重新拉列表接口取平台实时状态再选下一门,避免用陈旧队列
- 学习进度改存 sessionStorage(yun-xue-tang-run-state-v3),整页跳转/刷新可续;
  运行开关不跨会话,浏览器重启后必须显式开跑
- dev/:新增 real-cdp / real-run / real-test / real-e2e 实跑脚本与 check.mjs
  统一静态检查(JS 22 + 批处理 2),替代逐文件 node --check
- docs/evidence/headless-linux-evidence.txt + 实跑截图,README 补无头部署章节
2026-10-25 23:04:40 +08:00

98 lines
4.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 启动前预检:构建产物、Node 版本、凭据、浏览器、调试端口。
//
// 目标是把「失败」变成一段人能照着做的说明,而不是一个栈。所有检查失败都抛
// PreflightError(exitCode=2),由 headless.mjs 统一打印并退出。
import fs from 'node:fs';
import { httpJson } from './cdp.mjs';
import { resolveBrowser, listCandidatePaths } from './browser.mjs';
import {
isWindows, platformName, portInUseHint, browserInstallHint, killProfileOrphans,
} from './platform.mjs';
export class PreflightError extends Error {
constructor(lines, exitCode = 2) {
super(Array.isArray(lines) ? lines.join('\n') : String(lines));
this.name = 'PreflightError';
this.lines = Array.isArray(lines) ? lines : [String(lines)];
this.exitCode = exitCode;
}
}
/**
* 逐项检查,全部通过后返回 { browser, scriptSrc }。
* @param {object} o
* @param {boolean} o.checkOnly 自检模式不校验凭据
* @param {string} o.scriptFile 构建产物路径
* @param {string} o.usr
* @param {string} o.pwd
* @param {number} o.port
* @param {string} o.profileDir 端口被占时用来找出「自己的」残留浏览器
* @param {string} [o.chromeBin] 配置文件里指定的浏览器路径(空 = 自动探测)
* @param {Function} [o.log]
*/
export async function runPreflight(o) {
const fail = (lines) => { throw new PreflightError(lines); };
// 1) 构建产物:没有它就没东西可注入
if (!fs.existsSync(o.scriptFile)) {
fail([
'缺少构建产物 ' + o.scriptFile + ',先运行 npm run build',
'(仓库里只提交源码 src/,产物由 esbuild 生成)',
]);
}
// 2) Node 运行时能力:cdp.mjs 用全局 WebSocket / fetch,Node 22 起才稳定
if (typeof WebSocket !== 'function' || typeof fetch !== 'function') {
fail([
'需要 Node 22+(依赖全局 WebSocket / fetch),当前 ' + process.version,
isWindows
? 'Windows 上升级:winget install OpenJS.NodeJS.LTS 或到 nodejs.org 下载 LTS 安装包'
: 'Linux 上可用 nvm / nodesource 升级:curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -',
]);
}
// 3) 凭据:自检模式不需要(只读检查不登录)
if (!o.checkOnly && (!o.usr || !o.pwd)) {
fail([
'缺少环境变量 YUN_XUE_TANG_USR / YUN_XUE_TANG_PWD。',
'(即使 profile 里有有效 cookie,也建议填上:cookie 失效时才能自动重登)',
]);
}
// 4) 浏览器:找不到就把所有尝试过的路径列出来,省得用户猜
const browser = o.chromeBin || resolveBrowser();
if (!browser || !fs.existsSync(browser)) {
fail(['找不到 Chrome/Chromium(' + platformName + '),可用 YUN_XUE_TANG_CHROME_BIN=/path/to/chrome '
+ '或配置文件里的 chrome 字段指定。已尝试:']
.concat(listCandidatePaths().map((p) => ' ' + p))
.concat(browserInstallHint()));
}
// 5) 调试端口:被占用意味着上一次的浏览器还在跑,Chrome 会直接拒绝启动。
// 无人值守场景下「自己上次没退干净」是最常见的原因,先按 profile 自愈一次:
// 只杀命令行里带本 profile 目录的进程,不碰用户自己的浏览器。
const probe = async () => {
try { return await httpJson('http://127.0.0.1:' + o.port + '/json/version'); } catch (e) { return null; }
};
let live = await probe();
if (live && live.webSocketDebuggerUrl) {
const killed = await killProfileOrphans(o.profileDir, { log: o.log });
if (killed) {
o.log('[预检] 已清理 ' + killed + ' 个占用本 profile 的残留进程,重新检测端口…');
await new Promise((r) => setTimeout(r, 1200));
live = await probe();
}
}
if (live && live.webSocketDebuggerUrl) {
const hint = portInUseHint(o.port);
fail([
'CDP 端口 ' + o.port + ' 已被占用(' + (live.Browser || '未知浏览器') + ')。',
'先结束占用该端口的进程:' + hint[0],
hint[1],
'或换端口重跑:--port ' + (o.port + 1) + ' / YUN_XUE_TANG_CDP_PORT=' + (o.port + 1),
]);
}
return { browser, scriptSrc: fs.readFileSync(o.scriptFile, 'utf8') };
}