refactor(workflow): 收敛 Job 状态语义与监听事件过滤
同一份逻辑在两个地方各写一遍,必然在某次修改后漂移: - 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,说明状态语义的两个入口必须一起考虑。
This commit is contained in:
1 parent
a0058c1c66
commit
71aa9b54a7
7 files changed
+102
-48
No files matched your search
@@ -3,6 +3,7 @@ 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>) {
|
||||
@@ -74,8 +75,6 @@ export async function runWorkflowDebugTest(
|
||||
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) });
|
||||
const terminal = new Set(["COMPLETED", "FAILED", "STOP", "STOPPED", "CANCELLED", "CANCELED"]);
|
||||
const jobState = (job?: WorkflowJobDetail) => String(job?.status || job?.state || "UNKNOWN").toUpperCase();
|
||||
let finalJob: WorkflowJobDetail | undefined;
|
||||
let pollingFailed = false;
|
||||
|
||||
@@ -83,7 +82,7 @@ export async function runWorkflowDebugTest(
|
||||
try {
|
||||
const job = await api.getJob(client, jobId);
|
||||
finalJob = job;
|
||||
if (terminal.has(jobState(job))) break;
|
||||
if (isJobTerminal(normalizeJobStatus(job))) break;
|
||||
} catch (error) {
|
||||
// 任务刚创建可能存在短延时,继续轮询
|
||||
const status = (error as { status?: number; response?: { status?: number } } | null)?.status
|
||||
@@ -101,7 +100,7 @@ export async function runWorkflowDebugTest(
|
||||
try { finalJob = await api.getJob(client, jobId); }
|
||||
catch (error) { recordError("job", error); pollingFailed = true; }
|
||||
}
|
||||
const timedOut = !pollingFailed && !terminal.has(jobState(finalJob));
|
||||
const timedOut = !pollingFailed && !isJobTerminal(normalizeJobStatus(finalJob));
|
||||
const [treeResult, failuresResult] = await Promise.allSettled([
|
||||
api.getJobExecutionTree(client, jobId),
|
||||
api.getJobFailedExecutions(client, jobId),
|
||||
@@ -113,11 +112,11 @@ export async function runWorkflowDebugTest(
|
||||
|
||||
const rawFailed = extractFailureExecutions(tree, failedItems);
|
||||
const diagnosticsComplete = diagnosticsErrors.length === 0;
|
||||
const status = jobState(finalJob);
|
||||
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 (terminal.has(status) && (status !== "COMPLETED" || hasExecutionError)) outcome = "failed";
|
||||
else if (isJobTerminal(status) && (status !== "COMPLETED" || hasExecutionError)) outcome = "failed";
|
||||
else if (status === "COMPLETED" && diagnosticsComplete) outcome = "completed";
|
||||
const conditionDiagnoses = explainConditionBranches(tree);
|
||||
|
||||
|
||||
Reference in new issue
Block a user