同一份逻辑在两个地方各写一遍,必然在某次修改后漂移: - Job 状态判定:watch/helpers.ts 与 execution/debug.ts 各有一份终态集合, 且对数字 state 的解释相反——watch 把 state:0 视为 COMPLETED, debug 把它当成未知继续轮询到超时,同一个 Job 在两条路径上结论相反。 统一到 execution/job-status.ts(watch 已依赖 execution,方向单向), helpers 保留转发导出,debug 删除本地实现;新增两条用例固定该语义。 - --failed-only 事件过滤:pipeline-watcher.ts 与 single-job.ts 逐字重复, 抽为 watch/helpers.ts 的 shouldEmitWatchEvent(),两处调用点同步瘦身。 同步更新 execution 模块 README,说明状态语义的两个入口必须一起考虑。
151 lines
5.7 KiB
TypeScript
151 lines
5.7 KiB
TypeScript
import * as api from "../api.js";
|
|
import { ValidationError } from "../../../../core/errors.js";
|
|
import { explainConditionBranches } from "../branch/index.js";
|
|
import { WorkflowJobDetail } from "../types.js";
|
|
import { extractFailureExecutions } from "./tree.js";
|
|
import { normalizeJobStatus, isJobTerminal } from "./job-status.js";
|
|
|
|
|
|
export function getWebhookInfo(...args: Parameters<typeof api.getWebhookInfo>) {
|
|
return api.getWebhookInfo(...args);
|
|
}
|
|
|
|
|
|
export function sampleWorkflowData(...args: Parameters<typeof api.sampleWorkflowData>) {
|
|
return api.sampleWorkflowData(...args);
|
|
}
|
|
|
|
|
|
export function getWorkflowDebugSource(...args: Parameters<typeof api.getWorkflowDebugSource>) {
|
|
return api.getWorkflowDebugSource(...args);
|
|
}
|
|
|
|
|
|
export function triggerWorkflowDebug(...args: Parameters<typeof api.triggerWorkflowDebug>) {
|
|
return api.triggerWorkflowDebug(...args);
|
|
}
|
|
|
|
|
|
export async function runWorkflowDebugTest(
|
|
client: import("ky").KyInstance,
|
|
pipelineId: string,
|
|
options: {
|
|
flowCodeOrId?: string;
|
|
data?: Record<string, any>;
|
|
triggerCategory?: import("../types.js").WorkflowTriggerCategory;
|
|
triggerType?: string;
|
|
triggerLabel?: string;
|
|
autoPublish?: boolean;
|
|
timeoutMs?: number;
|
|
pollIntervalMs?: number;
|
|
} = {}
|
|
): Promise<import("../types.js").WorkflowTestReport> {
|
|
if (!Number.isFinite(options.timeoutMs ?? 15000) || (options.timeoutMs ?? 15000) < 0 || !Number.isFinite(options.pollIntervalMs ?? 500) || (options.pollIntervalMs ?? 500) <= 0) {
|
|
throw new ValidationError("timeoutMs 必须为非负数,pollIntervalMs 必须为正数");
|
|
}
|
|
let targetEntityId = options.flowCodeOrId ?? "";
|
|
const code = options.flowCodeOrId;
|
|
const isDirectCode = !code || code.includes("-") || code.startsWith("ID01");
|
|
if (code && !isDirectCode) {
|
|
try {
|
|
const source = await api.getWorkflowDebugSource(client, pipelineId, {
|
|
page: 1,
|
|
pageSize: 10,
|
|
keywords: code,
|
|
});
|
|
const matched = source.items.find(
|
|
(item) => item.id === code || (item as any).code === code || item.title?.includes(code) || (item as any).name?.includes(code)
|
|
);
|
|
if (matched) {
|
|
targetEntityId = matched.id;
|
|
}
|
|
} catch {
|
|
// 关键字搜索超时或失败时直接回退使用传入值
|
|
}
|
|
}
|
|
|
|
const jobId = await api.triggerWorkflowDebug(client, pipelineId, {
|
|
entityId: targetEntityId || undefined,
|
|
data: options.data,
|
|
autoPublish: options.autoPublish ?? false,
|
|
});
|
|
|
|
const timeoutMs = options.timeoutMs ?? 15000;
|
|
const pollIntervalMs = options.pollIntervalMs ?? 500;
|
|
const start = Date.now();
|
|
const diagnosticsErrors: Array<{ source: string; message: string }> = [];
|
|
const recordError = (source: string, error: unknown) => diagnosticsErrors.push({ source, message: error instanceof Error ? error.message : String(error) });
|
|
let finalJob: WorkflowJobDetail | undefined;
|
|
let pollingFailed = false;
|
|
|
|
while (Date.now() - start < timeoutMs) {
|
|
try {
|
|
const job = await api.getJob(client, jobId);
|
|
finalJob = job;
|
|
if (isJobTerminal(normalizeJobStatus(job))) break;
|
|
} catch (error) {
|
|
// 任务刚创建可能存在短延时,继续轮询
|
|
const status = (error as { status?: number; response?: { status?: number } } | null)?.status
|
|
?? (error as { response?: { status?: number } } | null)?.response?.status;
|
|
if (status !== 404) {
|
|
recordError("job", error);
|
|
pollingFailed = true;
|
|
break;
|
|
}
|
|
}
|
|
await new Promise((resolve) => setTimeout(resolve, Math.min(pollIntervalMs, Math.max(0, timeoutMs - (Date.now() - start)))));
|
|
}
|
|
|
|
if (!finalJob && !pollingFailed) {
|
|
try { finalJob = await api.getJob(client, jobId); }
|
|
catch (error) { recordError("job", error); pollingFailed = true; }
|
|
}
|
|
const timedOut = !pollingFailed && !isJobTerminal(normalizeJobStatus(finalJob));
|
|
const [treeResult, failuresResult] = await Promise.allSettled([
|
|
api.getJobExecutionTree(client, jobId),
|
|
api.getJobFailedExecutions(client, jobId),
|
|
]);
|
|
const tree = treeResult.status === "fulfilled" ? treeResult.value : [];
|
|
const failedItems = failuresResult.status === "fulfilled" ? failuresResult.value.items : [];
|
|
if (treeResult.status === "rejected") recordError("executionTree", treeResult.reason);
|
|
if (failuresResult.status === "rejected") recordError("failedExecutions", failuresResult.reason);
|
|
|
|
const rawFailed = extractFailureExecutions(tree, failedItems);
|
|
const diagnosticsComplete = diagnosticsErrors.length === 0;
|
|
const status = normalizeJobStatus(finalJob);
|
|
const hasExecutionError = rawFailed.length > 0 || finalJob?.hasError === true || Number(finalJob?.errorTaskCount ?? 0) > 0;
|
|
let outcome: import("../types.js").WorkflowTestReport["outcome"] = "incomplete";
|
|
if (timedOut) outcome = "timed_out";
|
|
else if (isJobTerminal(status) && (status !== "COMPLETED" || hasExecutionError)) outcome = "failed";
|
|
else if (status === "COMPLETED" && diagnosticsComplete) outcome = "completed";
|
|
const conditionDiagnoses = explainConditionBranches(tree);
|
|
|
|
return {
|
|
jobId,
|
|
pipelineId,
|
|
status,
|
|
outcome,
|
|
timedOut,
|
|
diagnosticsComplete,
|
|
diagnosticsErrors,
|
|
runType: finalJob?.runType || "DEBUG",
|
|
durationTime: typeof finalJob?.durationTime === "number" ? finalJob.durationTime : undefined,
|
|
targetEntityId,
|
|
triggerCategory: options.triggerCategory,
|
|
triggerType: options.triggerType,
|
|
triggerLabel: options.triggerLabel,
|
|
executionTree: tree,
|
|
failedExecutions: rawFailed,
|
|
conditionDiagnoses,
|
|
};
|
|
}
|
|
|
|
|
|
export async function getWorkflowSampleData(
|
|
client: import("ky").KyInstance,
|
|
pipelineId: string,
|
|
type = "webhook"
|
|
): Promise<import("../types.js").WorkflowSampleDataResult> {
|
|
return api.getWorkflowSampleData(client, pipelineId, type);
|
|
}
|