feat(workflow): implement real-time workflow tail and watch commands
This commit is contained in:
1 parent
71f9879f68
commit
53201ba4e1
11 files changed
+1310
-7
No files matched your search
@@ -1683,4 +1683,141 @@ describe("CLI - workflow job 执行详情与异常精准透视", () => {
|
||||
)
|
||||
).rejects.toThrow("触发源节点");
|
||||
});
|
||||
|
||||
it("handles workflow tail and watch commands for real-time streaming and snapshots", async () => {
|
||||
// 1. Missing target -> usage
|
||||
const noArgRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "tail",
|
||||
args: [],
|
||||
options: {},
|
||||
raw: ["workflow", "tail"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(noArgRes.exitCode).toBe(1);
|
||||
expect(noArgRes.stderr).toContain("用法: hose-use workflow tail");
|
||||
|
||||
const mockJobDetail = {
|
||||
id: "job_tail_101",
|
||||
pipelineId: "pipe_tail_test",
|
||||
label: "实时监控流程",
|
||||
status: "COMPLETED",
|
||||
runType: "DEBUG",
|
||||
durationTime: 850,
|
||||
successTaskCount: 2,
|
||||
errorTaskCount: 0,
|
||||
hasError: false,
|
||||
};
|
||||
|
||||
const mockExecutions = [
|
||||
{
|
||||
id: "ex_node_1",
|
||||
name: "trigger_webhook",
|
||||
label: "发票接入",
|
||||
type: "webhook",
|
||||
status: "COMPLETED",
|
||||
executionTime: 15,
|
||||
},
|
||||
{
|
||||
id: "ex_node_2",
|
||||
name: "action_clean",
|
||||
label: "数据清洗",
|
||||
type: "code-js2",
|
||||
status: "COMPLETED",
|
||||
executionTime: 120,
|
||||
},
|
||||
];
|
||||
|
||||
const mockClient = {
|
||||
get: (url: string) => {
|
||||
if (url.includes("api/easo/pipelines/pipe_tail_test/log")) {
|
||||
return {
|
||||
json: async () => ({
|
||||
code: 200,
|
||||
data: {
|
||||
jobList: [{ jobId: "job_tail_101", status: "COMPLETED" }],
|
||||
},
|
||||
}),
|
||||
};
|
||||
}
|
||||
if (url.includes("api/easo/jobs/running")) {
|
||||
return { json: async () => ({ code: 200, data: [] }) };
|
||||
}
|
||||
if (url.includes("api/easo/jobs/job_tail_101/execution/level/failed")) {
|
||||
return { json: async () => ({ code: 200, data: { items: [] } }) };
|
||||
}
|
||||
if (url.includes("api/easo/jobs/job_tail_101/execution")) {
|
||||
return { json: async () => ({ code: 200, data: mockExecutions }) };
|
||||
}
|
||||
if (url.includes("api/easo/jobs/job_tail_101")) {
|
||||
return { json: async () => ({ code: 200, data: mockJobDetail }) };
|
||||
}
|
||||
if (url.includes("api/easo/pipelines/pipe_tail_test")) {
|
||||
return { json: async () => ({ code: 200, data: { id: "pipe_tail_test", label: "实时监控流程" } }) };
|
||||
}
|
||||
return { json: async () => ({ code: 200, data: null }) };
|
||||
},
|
||||
};
|
||||
|
||||
spies.push(
|
||||
spyOn(helpers, "createClientsForCorp").mockResolvedValue({
|
||||
ekbClient: mockClient as any,
|
||||
easoClient: mockClient as any,
|
||||
corp: mockCorp,
|
||||
} as any)
|
||||
);
|
||||
spies.push(
|
||||
spyOn(helpers, "createPiperClientForCorp").mockResolvedValue(mockClient as any)
|
||||
);
|
||||
|
||||
// 2. tail --once snapshot
|
||||
const onceRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "tail",
|
||||
args: ["pipe_tail_test"],
|
||||
options: { once: true },
|
||||
raw: ["workflow", "tail", "pipe_tail_test", "--once"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(onceRes.exitCode).toBe(0);
|
||||
expect(onceRes.stdout).toContain("trigger_webhook");
|
||||
expect(onceRes.stdout).toContain("action_clean");
|
||||
expect(onceRes.stdout).toContain("Job [job_tail_101] 执行结束");
|
||||
|
||||
// 3. tail --once --json
|
||||
const onceJsonRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "tail",
|
||||
args: ["pipe_tail_test"],
|
||||
options: { once: true },
|
||||
raw: ["workflow", "tail", "pipe_tail_test", "--once"],
|
||||
},
|
||||
{ ...mockRuntime, jsonMode: true }
|
||||
);
|
||||
expect(onceJsonRes.exitCode).toBe(0);
|
||||
const parsedReport = JSON.parse(onceJsonRes.stdout);
|
||||
expect(parsedReport.totalJobsWatched).toBe(1);
|
||||
expect(parsedReport.jobs[0].jobId).toBe("job_tail_101");
|
||||
expect(parsedReport.jobs[0].successCount).toBe(2);
|
||||
|
||||
// 4. watch alias targeting specific job
|
||||
const watchJobRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "watch",
|
||||
args: ["pipe_tail_test"],
|
||||
options: { job: "job_tail_101", interval: 1, once: true },
|
||||
raw: ["workflow", "watch", "pipe_tail_test", "--job=job_tail_101", "--once"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(watchJobRes.exitCode).toBe(0);
|
||||
expect(watchJobRes.stdout).toContain("Job [job_tail_101]");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,6 +12,7 @@ import { handleCopy } from "./copy.js";
|
||||
import { handleDelete } from "./delete.js";
|
||||
import { handleCreate } from "./create.js";
|
||||
import { handleNodeCommand } from "./node.js";
|
||||
import { handleWorkflowTailCommand } from "./tail.js";
|
||||
|
||||
export async function handleWorkflowCommand(
|
||||
parsed: CliParsedArgs,
|
||||
@@ -93,10 +94,14 @@ export async function handleWorkflowCommand(
|
||||
case "node":
|
||||
case "nodes":
|
||||
return handleNodeCommand(parsed, runtime);
|
||||
case "tail":
|
||||
case "watch":
|
||||
case "follow":
|
||||
return handleWorkflowTailCommand(parsed, runtime);
|
||||
default:
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `未知 workflow 子命令: ${action}。支持: list, get, find, analyze, toggle, publish, patch, save, history, logs, job, diff, diff-job, check, test, copy, create, delete, node\n`,
|
||||
stderr: `未知 workflow 子命令: ${action}。支持: list, get, find, analyze, toggle, publish, patch, save, history, logs, job, diff, diff-job, check, test, copy, create, delete, node, tail\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import {
|
||||
watchWorkflowPipeline,
|
||||
formatWatchEvent,
|
||||
type WorkflowWatchEvent,
|
||||
} from "../../../domains/platform/services.js";
|
||||
import { stringOption, numberOption, booleanOption } from "../../parser.js";
|
||||
import { printJson } from "../../printer.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
|
||||
import { createWorkflowDetailClient } from "./shared.js";
|
||||
|
||||
export async function handleWorkflowTailCommand(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const targetArg = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId", "workflowId", "job", "jobId"]);
|
||||
|
||||
if (!targetArg) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: [
|
||||
"用法: hose-use workflow tail <pipelineId> [选项]",
|
||||
"别名: hose-use workflow watch, hose-use workflow follow",
|
||||
"",
|
||||
"说明:",
|
||||
" 类似 tail -f 实时监听工作流在云端的任务派发与节点执行树跃迁,即时捕获错误与异常跳过 (SKIPPED)。",
|
||||
"",
|
||||
"选项:",
|
||||
" --job=<jobId> 定向实时监听指定 Job 直至其运行结束",
|
||||
" --interval=<秒> 轮询检查间隔秒数 (默认 2 秒,最小 1 秒)",
|
||||
" --timeout=<秒> 最长监听秒数 (超时后退出)",
|
||||
" --max-jobs=<数量> 最多持续跟踪的 Job 个数后退出",
|
||||
" --once 仅检视最新或当前运行中的 Job 进度快照,不进入持续监听循环",
|
||||
" --failed-only 仅打印报错或异常跳过的节点与失败摘要",
|
||||
" --follow, -f 持续监听 (即使单个 Job 完成后也继续等待后续新任务)",
|
||||
" --json 以 JSON 结构或 JSONL 事件流输出",
|
||||
"",
|
||||
"示例:",
|
||||
" hose-use workflow tail 898268773466365973",
|
||||
" hose-use workflow tail 898268773466365973 --job=898577624451395602",
|
||||
" hose-use workflow watch 898268773466365973 --failed-only",
|
||||
" hose-use workflow tail 898268773466365973 --once --json",
|
||||
].join("\n") + "\n",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
// 允许将特定 jobId 直接作为参数,或通过 --job 指定
|
||||
const explicitJob = stringOption(parsed.options, ["job", "jobId"]);
|
||||
let pipelineId = targetArg;
|
||||
let jobId = explicitJob;
|
||||
|
||||
const intervalSec = numberOption(parsed.options, ["interval", "i"]) ?? 2;
|
||||
const timeoutSec = numberOption(parsed.options, ["timeout", "t"]);
|
||||
const maxJobs = numberOption(parsed.options, ["max-jobs", "maxJobs", "count"]);
|
||||
const once = booleanOption(parsed.options, ["once", "snapshot"], false);
|
||||
const follow = booleanOption(parsed.options, ["follow", "f"], !once && !jobId);
|
||||
const failedOnly = booleanOption(parsed.options, ["failed-only", "failedOnly", "errors"], false);
|
||||
|
||||
const { client } = await createWorkflowDetailClient(parsed, runtime, pipelineId);
|
||||
|
||||
const outputChunks: string[] = [];
|
||||
const eventsCollected: WorkflowWatchEvent[] = [];
|
||||
|
||||
const emit = async (chunk: string) => {
|
||||
outputChunks.push(chunk);
|
||||
if (runtime.writeStdout) {
|
||||
await runtime.writeStdout(chunk);
|
||||
}
|
||||
};
|
||||
|
||||
const report = await watchWorkflowPipeline(client, pipelineId, {
|
||||
jobId,
|
||||
intervalMs: Math.max(intervalSec, 1) * 1000,
|
||||
timeoutMs: timeoutSec !== undefined ? timeoutSec * 1000 : undefined,
|
||||
maxJobs,
|
||||
once,
|
||||
follow,
|
||||
failedOnly,
|
||||
onEvent: async (ev) => {
|
||||
eventsCollected.push(ev);
|
||||
if (runtime.jsonMode) {
|
||||
if (!once) {
|
||||
await emit(JSON.stringify(ev) + "\n");
|
||||
}
|
||||
} else {
|
||||
const line = formatWatchEvent(ev);
|
||||
await emit(line + "\n");
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
if (once) {
|
||||
return {
|
||||
stdout: printJson(report, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: report.hasFailures ? 1 : 0,
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout: runtime.writeStdout ? "" : outputChunks.join(""),
|
||||
stderr: "",
|
||||
exitCode: report.hasFailures ? 1 : 0,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: runtime.writeStdout ? "" : outputChunks.join(""),
|
||||
stderr: "",
|
||||
exitCode: report.hasFailures ? 1 : 0,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user