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,
|
||||
};
|
||||
}
|
||||
+1
-1
@@ -39,7 +39,7 @@ export const HELP_REGISTRY: Record<string, HelpTopic> = {
|
||||
},
|
||||
},
|
||||
config: { summary: "全局运行模式与路径配置", usage: "hose-use config <子命令>", commands: { paths: "诊断配置目录链", get: "查看全局配置", mode: "切换 stateful/stateless", set: "更新指定配置项" } },
|
||||
workflow: { summary: "工作流查询、草稿、运行排障与生命周期管理", usage: "hose-use workflow <子命令> [参数] [选项]", commands: { list: "列出工作流", get: "查看工作流详情", find: "按节点类型搜索", analyze: "静态分析工作流(含禁用/SKIP/联动覆盖检查)", job: "查看单次运行实例树、报错节点与MVEL日志", diff: "对比两个工作流快照的结构与脚本差异", "diff-job": "对比两次执行的节点分支与报错发散点", test: "端到端一键调试单据(替代前端手工调测)", patch: "生成草稿字段补丁", save: "保存草稿到远端", toggle: "切换启停状态", publish: "发布并启用工作流", history: "查看版本历史", logs: "查看运行日志", check: "检查错误管道" }, options: ["--flow=<单号> 指定测试单据", "--file=<workflow.json> 指定工作流快照", "--failed 只看失败节点", "--parent=<executionId> 展开循环/分支子节点", "--logs=<executionId> 查看节点执行日志", "--dry-run 只预览写操作", "--yes/--confirm 确认写操作"] },
|
||||
workflow: { summary: "工作流查询、草稿、运行排障与生命周期管理", usage: "hose-use workflow <子命令> [参数] [选项]", commands: { list: "列出工作流", get: "查看工作流详情", find: "按节点类型搜索", analyze: "静态分析工作流(含禁用/SKIP/联动覆盖检查)", job: "查看单次运行实例树、报错节点与MVEL日志", diff: "对比两个工作流快照的结构与脚本差异", "diff-job": "对比两次执行的节点分支与报错发散点", test: "端到端一键调试单据(替代前端手工调测)", node: "声明式节点编排与增删改 (list|get|add|edit|rm)", tail: "实时监听任务派发与节点执行日志流 (watch|follow)", create: "从零新建工作流草稿", copy: "复制克隆工作流", delete: "删除工作流", patch: "生成草稿字段补丁", save: "保存草稿到远端", toggle: "切换启停状态", publish: "发布并启用工作流", history: "查看版本历史", logs: "查看运行日志", check: "检查错误管道" }, options: ["--flow=<单号> 指定测试单据", "--file=<workflow.json> 指定工作流快照", "--failed 只看失败节点", "--parent=<executionId> 展开循环/分支子节点", "--logs=<executionId> 查看节点执行日志", "--dry-run 只预览写操作", "--yes/--confirm 确认写操作"] },
|
||||
bill: { summary: "单据检索、表单规格诊断与审批动作", usage: "hose-use bill <子命令> [单号|ID] [选项]", commands: { list: "查询单据", get: "查看单据详情", spec: "查看单据模板字段配置与联动覆盖规则", flow: "查看审批流程", approve: "同意审批", reject: "驳回审批", transfer: "转交审批", "add-node": "加签审批", comment: "添加评论" }, options: ["--dry-run 只预览审批动作", "--yes/--confirm 确认执行审批动作", "--comment=<原因> 驳回或评论内容"] },
|
||||
platform: { summary: "自建应用、业务对象、全量记录安全更新与台账体检", usage: "hose-use platform <子命令> [参数]", commands: { list: "列出自建应用", entities: "列出业务对象", schema: "查看字段定义", records: "查询记录(支持 --all / --ndjson 流式 / --stats 画像)", record: "查看或安全修改单条/批量记录 (get/create/edit/batch-edit)", audit: "业务对象健康度巡检(必填缺失/禁用引用/跨表同步差异)", permissions: "查看权限", imports: "查看导入任务" }, options: ["--all 自动翻页拉取全量记录", "--ndjson 流式逐行下发 JSON Lines ($O(1)内存)", "--flat 剥离底层实体动态字段前缀并提取引用", "--stats 输出总数/启停/重名/编码规则聚类画像", "--dry-run 只预览记录写操作", "--yes/--confirm 确认记录写操作", "--raw 跳过安全合并直接发送原始补丁"] },
|
||||
feetype: { summary: "费用类型全生命周期管理与模板规格查询", usage: "hose-use feetype <子命令> [参数]", commands: { list: "查看费用类型树", get: "查看详情与字段", spec: "查看单据/申请模板字段与联动规则", create: "新建费用类型", update: "修改属性", disable: "停用", enable: "启用", move: "移动层级", fields: "查看字段配置" }, options: ["--dry-run 只预览写操作", "--yes/--confirm 确认写操作"] },
|
||||
|
||||
+1
-1
@@ -179,7 +179,7 @@ export function printHelp(target: string[] = []): string {
|
||||
corps 多企业租户管理 (list, search <关键词>, switch, current, add-link)
|
||||
aipro 合思 AI PRO 确定性审批仲裁网关入口 (status, scopes, bind, checklist, review, decide, logs)
|
||||
config 全局运行模式与路径诊断 (show, paths, mode <stateful|stateless>, set)
|
||||
workflow 工作流管理 (list, get, find, analyze, job, toggle, publish, patch, save, history, logs, check)
|
||||
workflow 工作流管理 (list, get, find, analyze, node, tail, job, create, copy, delete, toggle, publish, patch, save, history, logs, check)
|
||||
bill 单据检索、模板规格与审批动作 (list, get <单号|ID> [--risk], spec <模板ID>, approve|reject|transfer|add-node|comment <单号|ID> [选项], refs, relations, attachments, download, flow, risk, budget, schema, link)
|
||||
contact 企业通讯录与人员检索 (list, search <关键词>, get <staffId>)
|
||||
task 待办中心与审批任务 (todo, approved, records, submitted, cc)
|
||||
|
||||
Reference in new issue
Block a user