Files
hose-use/src/cli/commands/workflow/tail.ts
T

113 lines
4.2 KiB
TypeScript

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 || report.timedOut || report.cancelled ? 1 : 0,
};
}
return {
stdout: runtime.writeStdout ? "" : outputChunks.join(""),
stderr: "",
exitCode: report.hasFailures || report.timedOut || report.cancelled ? 1 : 0,
};
}
return {
stdout: runtime.writeStdout ? "" : outputChunks.join(""),
stderr: "",
exitCode: report.hasFailures || report.timedOut || report.cancelled ? 1 : 0,
};
}