feat(electron-shell): 1.8.0 执行队列、流程编排、流程定时与版本回滚
- 执行队列:多任务串行排队,可单独取消,超时自动放弃,异常退出后标记已中断 - 流程编排:画布多场景依赖图,内置确保登录/等待/通知/人工确认节点 - 流程定时:场景与流程统一配置定时(每天/工作日/每周/每月/每隔N小时) - 流程版本历史:自动留档 20 版,画布一键回滚且回滚可撤销 - 流程配置云备份:随定时任务备份到中心机,换机自动恢复 - 总览页/场景库布局调整,通知浮窗右下角堆叠显示
This commit is contained in:
@@ -16,11 +16,13 @@ const licensing = require("./licensing");
|
||||
const registry = require("./registry");
|
||||
const meta = require("./meta");
|
||||
const runner = require("./runner");
|
||||
const queue = require("./queue");
|
||||
const runHistory = require("./run-history");
|
||||
const lastFields = require("./last-fields");
|
||||
const pluginConfigs = require("./plugin-configs");
|
||||
const scheduler = require("./scheduler");
|
||||
const tasks = require("./tasks");
|
||||
const flows = require("./flows");
|
||||
const platformAuth = require("./platform-auth");
|
||||
const platformCreds = require("./platform-creds");
|
||||
const shellUpdater = require("./shell-updater");
|
||||
@@ -262,6 +264,8 @@ async function route(req, res, url) {
|
||||
const body = await readJsonBody(req);
|
||||
const account = String(body.account || "").trim();
|
||||
if (!account) throw Object.assign(new Error("账号不能为空"), { status: 400 });
|
||||
// 已登录态下换号:同样受"任务执行中不可切换"约束(未登录时首次登录不受影响)
|
||||
if (session.sessAccount(cookies)) assertIdleForSwitch();
|
||||
const r = await cp.request("POST", "/api/admin/auth", {
|
||||
headers: {
|
||||
"X-Admin-User": account,
|
||||
@@ -280,6 +284,9 @@ async function route(req, res, url) {
|
||||
}
|
||||
|
||||
if (p === "/api/admin/logout" && method === "POST") {
|
||||
// 执行/排队中不允许切账号:一次运行带着账号上下文(平台 cookie、凭据注入、上报归属),
|
||||
// 半路换号会串号。必须等它跑完,或用户先在「执行队列」手动停止。
|
||||
assertIdleForSwitch();
|
||||
const token = cookies[session.SESSION_COOKIE] || "";
|
||||
session.clearSession(token);
|
||||
clearSessionCookie(res);
|
||||
@@ -288,6 +295,7 @@ async function route(req, res, url) {
|
||||
|
||||
// 离线登录:不依赖网络,仅校验本地 7 天离线授权票据(验签 + 未过期),消费票据直接进入工作台
|
||||
if (p === "/api/admin/offline-login" && method === "POST") {
|
||||
if (session.sessAccount(cookies)) assertIdleForSwitch();
|
||||
const st = licensing.licenseStatus();
|
||||
if (st.status !== "ok" || !st.payload || !st.payload.userId) {
|
||||
const reason =
|
||||
@@ -405,6 +413,10 @@ async function route(req, res, url) {
|
||||
}
|
||||
const account = String(info.account || "").trim();
|
||||
if (!account || !info.exists) return sendHtml(res, 200, authDonePage("账号未开通或未解析到有效工号,请联系管理员。"));
|
||||
// 钉钉扫码回调也是"换号"入口:执行/排队中直接拦掉,避免任务串到新账号
|
||||
if (hasActiveRuns()) {
|
||||
return sendHtml(res, 200, authDonePage("有任务正在执行或排队,请先在客户端停止任务后再切换账号。"));
|
||||
}
|
||||
const token = session.establishSession(account, info.token || "");
|
||||
setSessionCookie(res, token);
|
||||
if (config.CONTROLPLANE_URL) {
|
||||
@@ -593,8 +605,19 @@ async function route(req, res, url) {
|
||||
}
|
||||
// 记住本次参数(不含 password 字段),供执行台「恢复上次参数」
|
||||
lastFields.remember(account, plugin.id, body.fields || {}, plugin.fields);
|
||||
const runId = runner.submitRun(account, plugin, body.fields || {}, { trigger: "manual" });
|
||||
return sendJson(res, 202, { run_id: runId });
|
||||
// 提交即入队:返回排队位置,前端据此提示"已加入队列,第 N 位"
|
||||
const r = runner.submitRun(account, plugin, body.fields || {}, { trigger: "manual" });
|
||||
return sendJson(res, 202, {
|
||||
run_id: r.run_id,
|
||||
queue_position: r.queue_position,
|
||||
queued: r.queue_position > 1,
|
||||
});
|
||||
}
|
||||
|
||||
// 执行队列快照(当前执行 + 等待中的任务,带位置与等待时长)
|
||||
if (p === "/api/queue" && method === "GET") {
|
||||
const account = session.requireLogin(cookies);
|
||||
return sendJson(res, 200, queue.snapshot(account));
|
||||
}
|
||||
|
||||
// 运行历史列表(当前账号,新的在前):右轨「最近运行」与执行台「已完成」态的数据源
|
||||
@@ -618,11 +641,15 @@ async function route(req, res, url) {
|
||||
const account = session.requireLogin(cookies);
|
||||
const run = state.RUNS.get(runMatch[1]);
|
||||
if (!run || run.account !== account) throw Object.assign(new Error("运行不存在"), { status: 404 });
|
||||
if (run.status !== "running") throw Object.assign(new Error("运行已结束,无需停止"), { status: 409 });
|
||||
if (!runner.terminateRun(runMatch[1])) {
|
||||
throw Object.assign(new Error("任务尚未真正启动(排队中或刚结束),请稍候再试"), { status: 409 });
|
||||
// 排队中也能停:直接从队列摘除并落「已取消」,不必等轮到它再失败
|
||||
if (run.status !== "running" && run.status !== "queued") {
|
||||
throw Object.assign(new Error("运行已结束,无需停止"), { status: 409 });
|
||||
}
|
||||
return sendJson(res, 200, { ok: true });
|
||||
const wasQueued = run.status === "queued";
|
||||
if (!runner.terminateRun(runMatch[1])) {
|
||||
throw Object.assign(new Error("任务刚刚结束,请刷新后再试"), { status: 409 });
|
||||
}
|
||||
return sendJson(res, 200, { ok: true, was_queued: wasQueued });
|
||||
}
|
||||
|
||||
// ---------- 定时任务 ----------
|
||||
@@ -688,6 +715,104 @@ async function route(req, res, url) {
|
||||
return sendJson(res, 200, rec);
|
||||
}
|
||||
|
||||
// ---------- 流程编排(DAG) ----------
|
||||
if (p === "/api/flows" && method === "GET") {
|
||||
const account = session.requireLogin(cookies);
|
||||
return sendJson(res, 200, { flows: flows.list(account) });
|
||||
}
|
||||
if (p === "/api/flows" && method === "POST") {
|
||||
const account = session.requireLogin(cookies);
|
||||
const body = await readJsonBody(req);
|
||||
let rec;
|
||||
try {
|
||||
rec = flows.create(account, body);
|
||||
} catch (e) {
|
||||
throw Object.assign(new Error(e.message), { status: 400 });
|
||||
}
|
||||
tasks.pushBackup(account, session.cpHeaders(cookies)); // 流程配置随定时备份一起上云
|
||||
return sendJson(res, 201, rec);
|
||||
}
|
||||
// 运行态快照(含每个节点的状态),供流程页轮询/首屏兜底
|
||||
if (p === "/api/flows/runs" && method === "GET") {
|
||||
const account = session.requireLogin(cookies);
|
||||
return sendJson(res, 200, { runs: flows.activeRuns(account) });
|
||||
}
|
||||
const flowRunGet = p.match(/^\/api\/flows\/runs\/([^/]+)$/);
|
||||
if (flowRunGet && method === "GET") {
|
||||
const account = session.requireLogin(cookies);
|
||||
const r = flows.getRun(flowRunGet[1]);
|
||||
// 同样的越权保护:别人的流程运行不对外暴露
|
||||
if (!r || r.account !== account) throw Object.assign(new Error("流程运行不存在"), { status: 404 });
|
||||
return sendJson(res, 200, r);
|
||||
}
|
||||
// 人工确认节点:流程跑到 confirm 节点会挂起,这里点「继续 / 终止」
|
||||
const flowConfirm = p.match(/^\/api\/flows\/runs\/([^/]+)\/confirm$/);
|
||||
if (flowConfirm && method === "POST") {
|
||||
const account = session.requireLogin(cookies);
|
||||
const run = flows.getRun(flowConfirm[1]);
|
||||
if (!run || run.account !== account) throw Object.assign(new Error("流程运行不存在"), { status: 404 });
|
||||
const body = await readJsonBody(req);
|
||||
const done = flows.confirmNode(flowConfirm[1], String(body.node_id || ""), !!body.ok);
|
||||
if (!done) throw Object.assign(new Error("该确认节点已超时或已被处理"), { status: 409 });
|
||||
return sendJson(res, 200, { ok: true });
|
||||
}
|
||||
|
||||
// 流程版本历史:只给摘要(时间/名称/节点数/类型分布),节点明细走回滚接口
|
||||
const flowVer = p.match(/^\/api\/flows\/([^/]+)\/versions$/);
|
||||
if (flowVer && method === "GET") {
|
||||
const account = session.requireLogin(cookies);
|
||||
try {
|
||||
return sendJson(res, 200, { versions: flows.listVersions(flowVer[1], account) });
|
||||
} catch (e) {
|
||||
throw Object.assign(new Error(e.message), { status: e.status || 400 });
|
||||
}
|
||||
}
|
||||
const flowRollback = p.match(/^\/api\/flows\/([^/]+)\/rollback$/);
|
||||
if (flowRollback && method === "POST") {
|
||||
const account = session.requireLogin(cookies);
|
||||
const body = await readJsonBody(req);
|
||||
try {
|
||||
const rec = flows.rollback(flowRollback[1], account, String(body.vid || ""));
|
||||
tasks.pushBackup(account, session.cpHeaders(cookies));
|
||||
return sendJson(res, 200, rec);
|
||||
} catch (e) {
|
||||
throw Object.assign(new Error(e.message), { status: e.status || 400 });
|
||||
}
|
||||
}
|
||||
|
||||
const flowMatch = p.match(/^\/api\/flows\/([^/]+)(\/run)?$/);
|
||||
if (flowMatch && flowMatch[2] && method === "POST") {
|
||||
const account = session.requireLogin(cookies);
|
||||
try {
|
||||
return sendJson(res, 202, flows.run(account, flowMatch[1]));
|
||||
} catch (e) {
|
||||
throw Object.assign(new Error(e.message), { status: e.status || 400 });
|
||||
}
|
||||
}
|
||||
if (flowMatch && !flowMatch[2] && method === "PUT") {
|
||||
const account = session.requireLogin(cookies);
|
||||
const body = await readJsonBody(req);
|
||||
let rec;
|
||||
try {
|
||||
rec = flows.update(flowMatch[1], account, body);
|
||||
} catch (e) {
|
||||
throw Object.assign(new Error(e.message), { status: e.status || 400 });
|
||||
}
|
||||
if (!rec) throw Object.assign(new Error("流程不存在"), { status: 404 });
|
||||
tasks.pushBackup(account, session.cpHeaders(cookies));
|
||||
return sendJson(res, 200, rec);
|
||||
}
|
||||
if (flowMatch && !flowMatch[2] && method === "DELETE") {
|
||||
const account = session.requireLogin(cookies);
|
||||
try {
|
||||
if (!flows.remove(flowMatch[1], account)) throw Object.assign(new Error("流程不存在"), { status: 404 });
|
||||
} catch (e) {
|
||||
throw Object.assign(new Error(e.message), { status: e.status || 400 });
|
||||
}
|
||||
tasks.pushBackup(account, session.cpHeaders(cookies));
|
||||
return sendJson(res, 200, { ok: true });
|
||||
}
|
||||
|
||||
// ---------- 插件配置 / UI ----------
|
||||
const cfgMatch = p.match(/^\/api\/plugin-config\/([^/]+)$/);
|
||||
if (cfgMatch && method === "GET") {
|
||||
@@ -755,6 +880,25 @@ async function route(req, res, url) {
|
||||
sendJson(res, 404, { detail: "Not Found" });
|
||||
}
|
||||
|
||||
// 是否有"带账号上下文"的任务在跑或在排队(切换账号的唯一阻断条件)
|
||||
function hasActiveRuns() {
|
||||
if (queue.busy() || queue.size() > 0) return true;
|
||||
for (const [, r] of state.RUNS) {
|
||||
if (r && (r.status === "running" || r.status === "queued")) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function assertIdleForSwitch() {
|
||||
if (!hasActiveRuns()) return;
|
||||
const running = queue.runningItem();
|
||||
const tip = running && running.name ? `「${running.name}」正在执行` : "有任务正在执行或排队";
|
||||
throw Object.assign(
|
||||
new Error(`${tip},请先在「执行队列」停止任务后再切换账号`),
|
||||
{ status: 409 }
|
||||
);
|
||||
}
|
||||
|
||||
function findOwned(sid, account) {
|
||||
const s = tasks.getSchedule(sid);
|
||||
if (!s) throw Object.assign(new Error("任务不存在"), { status: 404 });
|
||||
|
||||
Reference in New Issue
Block a user