feat(electron-shell): 启动链路修复与执行/配置收口插件侧
- 启动链路 P1-P7 修复:预算对齐、完成事件推送、端口占用提示、前端阶段文案接管 - 静态缓存按 IS_FROZEN 区分开发/打包态,bootPhaseOwned 竞态修复 - 一键执行与参数配置统一收口到插件界面,定时任务引用 config_id - 环境包迁移到 %LOCALAPPDATA%\37duAutomation\python-env,重装零解压 - shell-updater:缓存包 sha256 校验、静默升级后自动拉起新版、自动清理旧安装包 - after-pack 钩子生成 shell.version 修复打包态版本识别 - 插件 xq0031/xq0045 迁移至 plugins/XQ0001-0100/ 号段分桶中文目录
This commit is contained in:
@@ -379,6 +379,11 @@ async function route(req, res, url) {
|
||||
}
|
||||
|
||||
// ---------- 元数据 / 插件同步 ----------
|
||||
// 启动状态(启动页展示"当前在干嘛",无需登录)
|
||||
if (p === "/api/boot/status" && method === "GET") {
|
||||
return sendJson(res, 200, state.bootStatus());
|
||||
}
|
||||
|
||||
if (p === "/api/meta" && method === "GET") {
|
||||
const account = session.sessAccount(cookies);
|
||||
const data = await meta.buildMeta(account);
|
||||
@@ -398,6 +403,10 @@ async function route(req, res, url) {
|
||||
} catch (e) {
|
||||
logger.warning(`同步后刷新授权票据失败:${e.message}`);
|
||||
}
|
||||
// 同步完成 → 推事件,前端据此刷新插件清单(避免用户看到"空场景/请授权"旧值)
|
||||
try {
|
||||
HUB.publish({ t: "plugins_synced", failed: result.failed || [] });
|
||||
} catch (e) { /* 忽略:推送失败不影响返回 */ }
|
||||
return sendJson(res, 200, result);
|
||||
}
|
||||
|
||||
@@ -465,8 +474,8 @@ async function route(req, res, url) {
|
||||
}
|
||||
if (p === "/api/env/sync" && method === "POST") {
|
||||
session.requireLogin(cookies);
|
||||
const r = await envSync.checkAndSync(true);
|
||||
return sendJson(res, 200, { ok: r.ok, reason: r.reason || "", version: r.version || "" });
|
||||
void envSync.checkAndSync(true); // 后台下载+切换,进度经 /api/env/status 轮询展示
|
||||
return sendJson(res, 200, { ok: true });
|
||||
}
|
||||
if (p === "/api/env/rollback" && method === "POST") {
|
||||
session.requireLogin(cookies);
|
||||
@@ -768,6 +777,12 @@ function servePluginUi(res, pluginId, rel) {
|
||||
res.end(body);
|
||||
}
|
||||
|
||||
// 静态资源内存缓存:首屏 index.html/js/css 每次 readFileSync 是同步 IO,会阻塞事件循环,
|
||||
// 与 boot status 轮询、WS 握手抢时间(空机器 + 杀软扫描场景被放大)→ 命中缓存零 IO。
|
||||
// 仅打包态启用:开发态 dist 随时重建,内存缓存会吐旧文件,必须每次读盘。
|
||||
const _STATIC_CACHE = new Map();
|
||||
const STATIC_CACHE_MAX = 200;
|
||||
|
||||
function serveStatic(res, url) {
|
||||
const dist = config.frontendDist();
|
||||
let rel = url.pathname === "/" ? "index.html" : url.pathname.slice(1);
|
||||
@@ -782,7 +797,19 @@ function serveStatic(res, url) {
|
||||
if (!fs.existsSync(target)) return sendJson(res, 404, { detail: "Not Found" });
|
||||
}
|
||||
const mediaType = MIME[path.extname(target).toLowerCase()] || "application/octet-stream";
|
||||
if (config.IS_FROZEN) {
|
||||
const cached = _STATIC_CACHE.get(target);
|
||||
if (cached) {
|
||||
res.writeHead(200, { "Content-Type": mediaType, "Content-Length": cached.length });
|
||||
res.end(cached);
|
||||
return;
|
||||
}
|
||||
}
|
||||
const body = fs.readFileSync(target);
|
||||
if (config.IS_FROZEN) {
|
||||
if (_STATIC_CACHE.size >= STATIC_CACHE_MAX) _STATIC_CACHE.clear(); // 简单防无限增长
|
||||
_STATIC_CACHE.set(target, body);
|
||||
}
|
||||
res.writeHead(200, { "Content-Type": mediaType, "Content-Length": body.length });
|
||||
res.end(body);
|
||||
}
|
||||
@@ -915,6 +942,14 @@ function listen(cb) {
|
||||
} else {
|
||||
logger.error(`本地服务启动失败:${e.message}`);
|
||||
}
|
||||
// 端口占用时窗口还没建,直接 exit 用户只看到"闪一下就没了" → 弹窗说明后再退出
|
||||
try {
|
||||
const { dialog } = require("electron");
|
||||
dialog.showErrorBox(
|
||||
"37du 自动化工作台",
|
||||
`本地服务端口 ${config.PORT} 被占用,无法启动。\n请先关闭旧客户端/底座进程后重试。\n(${e.code || e.message})`
|
||||
);
|
||||
} catch (_e) { /* 非 Electron 环境忽略 */ }
|
||||
process.exit(1);
|
||||
});
|
||||
return server;
|
||||
|
||||
Reference in New Issue
Block a user