feat(electron-shell): 1.11.0 新增设置中心与执行超时/内存分层,组件运行状态与运行历史峰值内存
This commit is contained in:
@@ -24,6 +24,13 @@ const { makeLogger } = require("./logger");
|
||||
|
||||
const logger = makeLogger("runner");
|
||||
|
||||
// 内存 MB → 展示文案(日志/历史共用口径)
|
||||
function fmtMemText(mb) {
|
||||
const n = Number(mb) || 0;
|
||||
if (n <= 0) return "—";
|
||||
return n >= 1024 ? `${(n / 1024).toFixed(n >= 10240 ? 0 : 1)} GB` : `${n} MB`;
|
||||
}
|
||||
|
||||
// 定时任务的最长排队等待:到点时前面还有任务在跑,超过这个时长就不跑了(避免堆到下班后)。
|
||||
// 手动执行不设限(用户自己点的,愿意等)。
|
||||
const SCHEDULE_MAX_WAIT_SEC = 30 * 60;
|
||||
@@ -219,6 +226,7 @@ function finishRun(runId, plugin, status, error, summary) {
|
||||
finished,
|
||||
duration_sec: durationSec,
|
||||
note: run.note || "",
|
||||
peak_mem_mb: run.peak_mem_mb || 0,
|
||||
logs: Array.isArray(run.logs) ? run.logs : [],
|
||||
});
|
||||
// 日志集中回传:整次运行日志上送中心机(离线自动暂存补报;失败不阻断运行收尾)
|
||||
@@ -239,6 +247,7 @@ function finishRun(runId, plugin, status, error, summary) {
|
||||
finished,
|
||||
duration_sec: durationSec,
|
||||
note: run.note || "",
|
||||
peak_mem_mb: run.peak_mem_mb || 0,
|
||||
logs: Array.isArray(run.logs) ? run.logs : [],
|
||||
});
|
||||
} catch (e) {
|
||||
@@ -459,6 +468,15 @@ async function executeQueued(item) {
|
||||
// 执行结束(close)后异步落终态 + 上报 + 回空闲。
|
||||
function startChild(plugin, fields, runId, account) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 磁盘预检(不阻断):数据盘/插件盘余量过低时先记一条 WARN,
|
||||
// 导出写到一半失败时能直接对上原因;是否清理由用户决定。
|
||||
try {
|
||||
for (const d of require("./sysinfo").lowDisks(1)) {
|
||||
emitLog(runId, plugin.id, "WARN",
|
||||
`磁盘剩余不足:${d.label} ${d.root} 仅剩 ${d.free_gb}GB,导出/写表可能失败,请先清理`);
|
||||
}
|
||||
} catch (e) { /* 统计失败不影响执行 */ }
|
||||
|
||||
// 插件级超时优先(plugin.json runTimeoutSeconds,0=不限时),缺省回退壳默认 RUN_TIMEOUT
|
||||
const timeout = config.runTimeoutFor(plugin);
|
||||
// 执行环境解析:便携环境包(python-env)优先 → 经 job_guard(Job Object 内存上限
|
||||
@@ -494,7 +512,7 @@ function startChild(plugin, fields, runId, account) {
|
||||
// 环境包模式:python -u job_guard.py --mem <MB> --timeout <s> -- python -u plugin_runner.py ...
|
||||
cmd = [
|
||||
python, "-u", guard,
|
||||
"--mem", String(config.RUN_MAX_MEM_MB),
|
||||
"--mem", String(config.runMaxMemMb()),
|
||||
"--timeout", String(timeout),
|
||||
"--", python, ...baseArgs,
|
||||
];
|
||||
@@ -556,9 +574,22 @@ function startChild(plugin, fields, runId, account) {
|
||||
});
|
||||
|
||||
const rlErr = readline.createInterface({ input: child.stderr });
|
||||
// 内存超限的表现是"分配失败 → MemoryError → 进程退出",Python 侧若来不及回 result,
|
||||
// 这里用 stderr 尾部兜底识别,避免前端只显示一个没有原因的"执行失败"。
|
||||
const stderrTail = [];
|
||||
let peakMemMb = 0;
|
||||
rlErr.on("line", (line) => {
|
||||
const s = String(line).trim();
|
||||
if (s) {
|
||||
// job_guard 收尾上报的峰值内存:解析后落运行历史,不再当作错误行刷进日志面板
|
||||
const peak = s.match(/^\[job_guard\]\s+peak_job_mb=(\d+)/);
|
||||
if (peak) {
|
||||
peakMemMb = parseInt(peak[1], 10) || 0;
|
||||
emitLog(runId, plugin.id, "INFO", `内存峰值:${fmtMemText(peakMemMb)}`);
|
||||
return;
|
||||
}
|
||||
stderrTail.push(s);
|
||||
if (stderrTail.length > 20) stderrTail.shift();
|
||||
emitLog(runId, plugin.id, "ERROR", s);
|
||||
}
|
||||
});
|
||||
@@ -581,11 +612,16 @@ function startChild(plugin, fields, runId, account) {
|
||||
if (timer) clearTimeout(timer);
|
||||
_ACTIVE.delete(runId);
|
||||
const hasResult = result && Object.keys(result).length > 0;
|
||||
// 内存上限是 Job Object 的 commit 硬顶:超限时 Python 抛 MemoryError(str 为空),
|
||||
// 旧版 runner 会回 ok=false + error="",前端只有"失败"看不出原因。
|
||||
const memHit = stderrTail.some((l) => /MemoryError|Cannot allocate memory|内存不足/i.test(l));
|
||||
const MEM_MSG = "内存不足:执行进程达到内存上限(可在客户端「设置 → 内存上限」调大,"
|
||||
+ "或让插件分批/流式处理大表)";
|
||||
let status;
|
||||
let error;
|
||||
if (hasResult) {
|
||||
status = result.ok ? "done" : "error";
|
||||
error = result.error || "";
|
||||
error = result.error || (memHit ? MEM_MSG : "");
|
||||
} else if (spawnErr) {
|
||||
status = "error";
|
||||
error = spawnErr;
|
||||
@@ -596,11 +632,16 @@ function startChild(plugin, fields, runId, account) {
|
||||
} else if (timedOut) {
|
||||
status = "error";
|
||||
error = `插件执行超时(${timeout}s)已强制终止`;
|
||||
} else if (memHit) {
|
||||
status = "error";
|
||||
error = MEM_MSG;
|
||||
} else {
|
||||
status = "error";
|
||||
error = `执行进程异常退出(exit=${code})且未返回结果`;
|
||||
}
|
||||
state.setRun(runId, { result: hasResult ? result.result : null });
|
||||
// 峰值内存先写进运行态,finishRun 会带进运行历史(0 = 旧环境包/直拉模式没有数据)
|
||||
if (peakMemMb > 0) state.setRun(runId, { peak_mem_mb: peakMemMb });
|
||||
finishRun(
|
||||
runId,
|
||||
plugin,
|
||||
|
||||
Reference in New Issue
Block a user