feat: 持久化运行记录并补充分发验证
This commit is contained in:
1 parent
c03380168d
commit
aff2d5cd95
8 files changed
+344
-7
No files matched your search
+36
-1
@@ -16,6 +16,36 @@ function fileOf(id: string): string {
|
||||
return path.join(dir(), `${id}.json`);
|
||||
}
|
||||
|
||||
interface PersistedRunSummary {
|
||||
flowId: string;
|
||||
status: "succeeded" | "failed" | "cancelled";
|
||||
finishedAt?: string;
|
||||
startedAt?: string;
|
||||
durationMs?: number;
|
||||
error?: { reason?: string } | string;
|
||||
}
|
||||
|
||||
function latestRuns(): Map<string, PersistedRunSummary> {
|
||||
const result = new Map<string, PersistedRunSummary>();
|
||||
const runsDir = path.join(app.getPath("userData"), "runs");
|
||||
if (!fs.existsSync(runsDir)) return result;
|
||||
for (const entry of fs.readdirSync(runsDir)) {
|
||||
const file = path.join(runsDir, entry, "run.json");
|
||||
if (!fs.existsSync(file)) continue;
|
||||
try {
|
||||
const run = JSON.parse(fs.readFileSync(file, "utf8")) as PersistedRunSummary;
|
||||
if (!run.flowId || !run.status) continue;
|
||||
const current = result.get(run.flowId);
|
||||
const time = run.finishedAt || run.startedAt || "";
|
||||
const currentTime = current?.finishedAt || current?.startedAt || "";
|
||||
if (!current || time > currentTime) result.set(run.flowId, run);
|
||||
} catch {
|
||||
// 损坏的单次运行记录不应阻塞任务列表
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function createFlow(name = "未命名流程"): FlowDefinition {
|
||||
const now = new Date().toISOString();
|
||||
const id = `flow_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 7)}`;
|
||||
@@ -44,11 +74,13 @@ export function createFlow(name = "未命名流程"): FlowDefinition {
|
||||
export function listFlows(): FlowSummary[] {
|
||||
const base = dir();
|
||||
const rows: FlowSummary[] = [];
|
||||
const runs = latestRuns();
|
||||
for (const entry of fs.readdirSync(base)) {
|
||||
if (!entry.endsWith(".json")) continue;
|
||||
try {
|
||||
const flow = JSON.parse(fs.readFileSync(path.join(base, entry), "utf8")) as FlowDefinition;
|
||||
if (!flow?.id || !Array.isArray(flow.nodes)) continue;
|
||||
const lastRun = runs.get(flow.id);
|
||||
rows.push({
|
||||
id: flow.id,
|
||||
name: flow.name || "未命名流程",
|
||||
@@ -56,7 +88,10 @@ export function listFlows(): FlowSummary[] {
|
||||
nodeCount: flow.nodes.length,
|
||||
enabled: flow.enabled !== false,
|
||||
triggerLabel: flow.trigger?.label || "手动运行",
|
||||
lastRunStatus: "never"
|
||||
lastRunAt: lastRun?.finishedAt || lastRun?.startedAt,
|
||||
lastRunStatus: lastRun?.status || "never",
|
||||
lastRunDurationMs: lastRun?.durationMs,
|
||||
lastRunError: typeof lastRun?.error === "string" ? lastRun.error : lastRun?.error?.reason
|
||||
});
|
||||
} catch {
|
||||
// 忽略损坏文件,避免一个坏流程阻塞列表
|
||||
|
||||
Reference in new issue
Block a user