Files
37duAutomation/desktop/electron-shell/electron/plugin-health.js
T
puddy 9db8c74b7f feat(shell): 插件密码加密、失败熔断与客户端自动更新(v1.11.3)
- 插件配置中 format=password 字段改为 AES-256-GCM 密文落盘(本机密钥,历史明文自动迁移)
- 同一插件连续失败 3 次自动禁用(仅统计进程启动后失败),升级自动解除
- 检测到新版本后台下载,空闲窗口提前提示并自动重启升级,设置页可配安全窗口
2026-11-26 09:28:18 +08:00

125 lines
3.7 KiB
JavaScript
Raw 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.
// 插件健康熔断:同一插件连续失败达到阈值自动禁用,避免定时任务每天白跑、失败却无人知晓。
// 只统计"进程真正跑起来后失败"(runner.finishRun 中 status=error 且已有 started),
// 入队失败、SDK/环境未就绪、平台登录失败等前置问题不计入——那些不是插件本身的问题。
// 恢复:插件升级到新版本自动清零;或前端手动「恢复」。
"use strict";
const fs = require("fs");
const path = require("path");
const config = require("./config");
const { makeLogger } = require("./logger");
const logger = makeLogger("plugin-health");
const FILE = path.join(config.APP_DATA, "plugin_health.json");
const FAIL_THRESHOLD = 3;
let _cache = null;
function load() {
if (_cache) return _cache;
let data = {};
try {
if (fs.existsSync(FILE)) {
const d = JSON.parse(fs.readFileSync(FILE, "utf-8"));
if (d && typeof d === "object" && !Array.isArray(d)) data = d;
}
} catch (e) {
logger.warning(`插件健康记录损坏,已忽略:${FILE}`);
}
_cache = data;
return _cache;
}
function persist() {
try {
fs.mkdirSync(config.APP_DATA, { recursive: true });
const tmp = FILE + ".tmp";
fs.writeFileSync(tmp, JSON.stringify(_cache || {}, null, 2), "utf-8");
fs.renameSync(tmp, FILE); // 原子替换
} catch (e) {
logger.warning(`插件健康记录落盘失败:${e.message}`);
}
}
function entry(pluginId) {
const all = load();
return all[pluginId] || null;
}
/**
* 记录一次运行结果。
* @returns {{ tripped: boolean, fails: number }} tripped 表示本次刚好触达熔断阈值
*/
function record(pluginId, ok, error) {
if (!pluginId) return { tripped: false, fails: 0 };
const all = load();
const cur = all[pluginId] || { fails: 0, tripped: false };
if (ok) {
if (cur.fails || cur.tripped) {
all[pluginId] = { fails: 0, tripped: false, last_ok_at: new Date().toLocaleString("zh-CN", { hour12: false }) };
persist();
logger.info(`插件 ${pluginId} 执行成功,失败计数清零 / 熔断解除`);
}
return { tripped: false, fails: 0 };
}
const fails = (parseInt(cur.fails, 10) || 0) + 1;
const tripped = cur.tripped || fails >= FAIL_THRESHOLD;
all[pluginId] = {
fails,
tripped,
error: String(error || "").slice(0, 300),
last_fail_at: new Date().toLocaleString("zh-CN", { hour12: false }),
};
persist();
if (tripped && !cur.tripped) {
logger.warning(`插件 ${pluginId} 连续失败 ${fails} 次,已自动熔断(需人工恢复或升级插件)`);
return { tripped: true, fails };
}
return { tripped: false, fails };
}
function isTripped(pluginId) {
const e = entry(pluginId);
return !!(e && e.tripped);
}
function status(pluginId) {
const e = entry(pluginId);
return {
tripped: !!(e && e.tripped),
fails: (e && parseInt(e.fails, 10)) || 0,
threshold: FAIL_THRESHOLD,
error: (e && e.error) || "",
last_fail_at: (e && e.last_fail_at) || "",
};
}
function all() {
const out = {};
for (const [pid, e] of Object.entries(load())) {
if (!e) continue;
out[pid] = {
tripped: !!e.tripped,
fails: parseInt(e.fails, 10) || 0,
threshold: FAIL_THRESHOLD,
error: e.error || "",
last_fail_at: e.last_fail_at || "",
};
}
return out;
}
// 手动恢复 / 插件升级后自动调用
function reset(pluginId) {
if (!pluginId) return false;
const all = load();
if (!all[pluginId]) return false;
delete all[pluginId];
persist();
logger.info(`插件 ${pluginId} 熔断已解除(手动恢复或版本更新)`);
return true;
}
module.exports = { FAIL_THRESHOLD, record, isTripped, status, all, reset };