build(desktop): 升级 electron-shell 版本至 1.6.3 并调整插件执行超时配置

- 插件执行超时默认延长至 6 小时,支持插件单独配置超时时间
- 新增插件根目录迁移逻辑,解决旧版权限问题
- 新增多个服务端实体、DTO、配置模块等基础代码
- 新增插件 UI 模板及相关静态资源
This commit is contained in:
2026-10-15 11:38:45 +08:00
parent 3dcedf95fd
commit f27639777f
717 changed files with 88647 additions and 17543 deletions
+3 -2
View File
@@ -13,8 +13,9 @@ CONTROLPLANE_TOKEN=
# 环境包目录的显式覆盖(默认:打包态 exe 同级 python-env/;dev electron-shell/python-env/)
# SHELL_PYTHON_ENV_ROOT=D:\work\code\37duAutomation\desktop\electron-shell\python-env
# 插件执行超时(秒,浏览器类默认 30min)
RUN_TIMEOUT=1800
# 插件执行超时(秒,默认 21600=6 小时)。长流程(耗时数小时)不再被默认 30 分钟强杀;
# 个别插件可在 plugin.json 声明 runTimeoutSeconds 覆盖(0=不限时),RUN_TIMEOUT=0 则全局不限时
RUN_TIMEOUT=21600
# 插件进程作业级内存上限(MB,环境包模式经 job_guard/Job Object 生效,默认 2048)
# RUN_MAX_MEM_MB=2048
+1 -1
View File
@@ -76,7 +76,7 @@ npm run electron
| `CONTROLPLANE_TOKEN` | 内部凭证(与 controlplane/.env 一致;非空时发 X-API-Token) |
| `PYTHON_EXE` | 插件执行子进程的 Python 解释器。**解析优先级**:显式 `PYTHON_EXE` > 环境包 `python-env/current.json` 指向的 `python.exe` > 系统 `python`(不设即可用环境包) |
| `SHELL_PYTHON_ENV_ROOT` | 环境包目录的显式覆盖(默认:打包态 exe 同级 `python-env/`,dev `electron-shell/python-env/`) |
| `RUN_TIMEOUT` | 插件执行超时秒数(默认 1800) |
| `RUN_TIMEOUT` | 插件执行超时秒数(默认 21600=6 小时)。可用插件级 `plugin.json.runTimeoutSeconds` 按插件覆盖(`0`=不限时);`RUN_TIMEOUT=0` 则全局不限时 |
| `RUN_MAX_MEM_MB` | 插件进程作业级内存上限(默认 2048,环境包模式经 job_guard 生效) |
| `DEBUG=1` | 开启完整 JSON 调试日志 |
+3
View File
@@ -75,6 +75,9 @@ async function init(options = {}) {
}
state.setBootStep("加载配置与本地服务");
// 插件根迁移(一次性):旧版打包态插件根在安装目录(装到 Program Files 时普通权限 EPERM),
// 现已改用 APP_DATA/plugins —— 必须在权限清单/插件扫描/同步之前执行
pluginSync.migrateLegacyPluginRoot();
scheduler.initActive();
// 账号:优先 .env,其次最近活跃本机会话
+23 -5
View File
@@ -56,12 +56,12 @@ const APP_DATA = path.join(localAppData(), "37duAutomation");
// 插件本地配置目录:per-plugin JSON 持久化
const PLUGIN_CONFIG_DIR = path.join(APP_DATA, "plugin_configs");
// 插件源码/安装根:dev = 仓库根 plugins/;打包 = exe 同级 plugins/
// 插件源码/安装根:dev = 仓库根 plugins/;打包 = %LOCALAPPDATA%\37duAutomation\plugins。
// 不能放 exe 同级:装到 C:\Program Files 时普通权限进程连 mkdir 都被拒(EPERM,插件全部装不上),
// 且 NSIS 重装会清空安装目录、插件陪葬(与环境包同因,见 envRootDir 注释)。
function pluginRoot() {
if (process.env.SHELL_PLUGIN_ROOT) return process.env.SHELL_PLUGIN_ROOT;
if (IS_FROZEN) {
return path.join(path.dirname(process.execPath), "plugins");
}
if (IS_FROZEN) return path.join(APP_DATA, "plugins");
return path.join(REPO_ROOT, "plugins");
}
@@ -91,9 +91,26 @@ const CONTROLPLANE_TOKEN = process.env.CONTROLPLANE_TOKEN || "";
// ==================== 插件执行子进程配置 ====================
// Python 解释器:优先 env,默认 python(PATH 中的 venv/系统 Python)
const PYTHON_EXE = process.env.PYTHON_EXE || "python";
const RUN_TIMEOUT = parseInt(process.env.RUN_TIMEOUT || "1800", 10);
// 插件执行超时(秒,默认 21600=6 小时):长流程(耗时数小时)不再被 1800s 默认强杀。
// 可整体覆盖(.env / 环境变量 RUN_TIMEOUT,0=全局不限时);单个插件可用 plugin.json 的
// runTimeoutSeconds 覆盖(见 runTimeoutFor)。两处都按 0=不限时、缺省回退壳默认处理。
const RUN_TIMEOUT = parseInt(process.env.RUN_TIMEOUT || "21600", 10);
const RUN_MAX_MEM_MB = parseInt(process.env.RUN_MAX_MEM_MB || "2048", 10);
// 插件执行超时(秒):插件 plugin.json 声明 runTimeoutSeconds 优先(0=不限时),
// 否则回退壳默认 RUN_TIMEOUT。传入 registry.find() 产出的插件对象。
function runTimeoutFor(plugin) {
if (plugin && typeof plugin === "object") {
const raw = plugin.timeout_seconds;
// 注意不能用 Number(raw) 直接判:null/undefined 会被转成 0(=不限时),必须显式排除
if (raw !== undefined && raw !== null) {
const v = Number(raw);
if (Number.isFinite(v) && v >= 0) return v;
}
}
return RUN_TIMEOUT;
}
// ==================== 便携 Python 环境包(python-env) ====================
// 版本化目录(python-env/<version>/)+ current.json 指向当前版本,保留上一版供回滚。
// 目录位置:打包态 = %LOCALAPPDATA%\37duAutomation\python-env——NSIS 重装/升级会清空安装目录,
@@ -183,6 +200,7 @@ module.exports = {
PYTHON_EXE,
RUN_TIMEOUT,
RUN_MAX_MEM_MB,
runTimeoutFor,
IS_FROZEN,
pluginRoot,
runnerScript,
@@ -22,6 +22,23 @@ const MAX_PKG_BYTES = 200 * 1024 * 1024;
// 放 APP_DATA 而非插件根,避免污染插件目录(pruneStale 遍历/版本目录混入)与 dev 仓库。
const ROLLBACK_DIR = path.join(config.APP_DATA, "plugin_rollback");
// 旧版兼容(一次性):1.6.1 及以前打包态插件根 = 安装目录(exe 同级 plugins/)。装到
// C:\Program Files 时普通权限进程 mkdir 直接 EPERM(插件全部装不上),且 NSIS 重装会清空安装
// 目录让插件陪葬。现改用 APP_DATA/plugins,启动最早期把旧目录整体迁过去(同盘 rename 瞬时完成)。
function migrateLegacyPluginRoot() {
if (!config.IS_FROZEN) return;
const target = pluginRoot();
const legacy = path.join(path.dirname(process.execPath), "plugins");
if (target === legacy || fs.existsSync(target) || !fs.existsSync(legacy)) return;
try {
fs.mkdirSync(path.dirname(target), { recursive: true });
fs.renameSync(legacy, target);
logger.info(`插件目录已从安装目录迁移到 ${target}`);
} catch (e) {
logger.warning(`插件旧目录迁移失败(将按权限重新下载插件):${e.message}`);
}
}
// 升级前备份当前版本(仅已装插件),返回 "id@version";失败返回 null(不阻断升级)
function backupForRollback(pluginId) {
const target = path.join(pluginRoot(), pluginId);
@@ -380,6 +397,7 @@ module.exports = {
localVersion,
resolveAllowed,
pluginRoot,
migrateLegacyPluginRoot,
backupForRollback,
rollbackAvailable,
rollbackIndex,
@@ -70,6 +70,15 @@ function fieldsFromSchema(schema) {
return out;
}
// 插件级执行超时(秒):plugin.json.runTimeoutSeconds;0=不限时,缺省/非法返回 null(回退壳默认)
function parseRunTimeout(manifest) {
if (manifest && manifest.runTimeoutSeconds !== undefined) {
const v = Number(manifest.runTimeoutSeconds);
if (Number.isFinite(v) && v >= 0) return v;
}
return null;
}
// 组装插件元数据(manifest 驱动,与 Python meta._plugin_schema 一致)
function pluginSchema(pluginId) {
const manifest = readManifest(pluginId);
@@ -98,6 +107,7 @@ function pluginSchema(pluginId) {
? manifest.platforms // 插件声明的全部平台依赖(运行前登录态预检按此逐个检查)
: manifest.platform ? [manifest.platform] : [], // 缺省回退单 platform 字段
min_runtime_version: manifest.minRuntimeVersion || "", // 版本兼容校验声明(安装时强校验,此处透传前端展示)
timeout_seconds: parseRunTimeout(manifest), // 插件级执行超时(秒,0=不限时,null=回退壳默认)
capabilities: manifest.capabilities || [],
permissions: manifest.permissions || {},
fields: fields || [],
+10 -6
View File
@@ -259,7 +259,8 @@ async function runQueued(plugin, fields, runId, account) {
// 执行结束(close)后异步落终态 + 上报 + 回空闲。
function startChild(plugin, fields, runId, account) {
return new Promise((resolve, reject) => {
const timeout = config.RUN_TIMEOUT;
// 插件级超时优先(plugin.json runTimeoutSeconds,0=不限时),缺省回退壳默认 RUN_TIMEOUT
const timeout = config.runTimeoutFor(plugin);
// 执行环境解析:便携环境包(python-env)优先 → 经 job_guard(Job Object 内存上限
// + 整树托管)拉起 plugin_runner;无环境包(dev 直接 .venv)保持原直拉链路。
// 两种模式协议逐字节一致(plugin_runner stdout JSON 事件行经 stdio 原样透传)。
@@ -396,11 +397,14 @@ function startChild(plugin, fields, runId, account) {
child.once("spawn", () => {
resolved = true;
timer = setTimeout(() => {
timedOut = true;
logger.warning(`插件执行超时(${timeout}s)已强制终止:${plugin.id}`);
killFn();
}, timeout * 1000);
// 超时整树强杀:timeout>0 起定时器;timeout=0(插件声明不限时)不设定时器、进程永不自动杀
if (timeout > 0) {
timer = setTimeout(() => {
timedOut = true;
logger.warning(`插件执行超时(${timeout}s)已强制终止:${plugin.id}`);
killFn();
}, timeout * 1000);
}
resolve(runId);
});
+12 -2
View File
@@ -417,8 +417,18 @@ function watchRun(scheduleId, runId) {
persist();
};
HUB.on("event", onEvent);
// 兜底:进程异常或事件丢失时不让监听器长期驻留(执行超时上限 + 2 分钟宽限)
timer = setTimeout(() => HUB.removeListener("event", onEvent), (config.RUN_TIMEOUT + 120) * 1000);
// 兜底:进程异常或事件丢失时不让监听器长期驻留(按该任务插件的执行超时上限 + 2 分钟宽限;
// 插件声明不限时时给 7 天硬上限,避免 setTimeout 溢出提前移除监听)
let boundSecs = config.RUN_TIMEOUT;
const sched = _schedules.find((x) => x.id === scheduleId);
if (sched && sched.plugin_id) {
try {
const plugin = require("./registry").find(sched.plugin_id);
const t = config.runTimeoutFor(plugin);
boundSecs = t > 0 ? t : 7 * 24 * 3600;
} catch (e) { /* 插件缺失/解析失败:沿用壳默认 */ }
}
timer = setTimeout(() => HUB.removeListener("event", onEvent), (boundSecs + 120) * 1000);
if (timer.unref) timer.unref();
}
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "37du-electron-shell",
"productName": "37du 自动化工作台",
"version": "1.6.1",
"version": "1.6.3",
"description": "37du 执行机 Node/Electron 客户端(替代 Python webhost 底座,插件保持 Python 子进程执行)",
"main": "electron/main.js",
"private": true,