同一份逻辑在两个地方各写一遍,必然在某次修改后漂移: - 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,说明状态语义的两个入口必须一起考虑。
37 lines
1.4 KiB
TypeScript
37 lines
1.4 KiB
TypeScript
import type { WorkflowJobDetail } from "../types.js";
|
||
|
||
/**
|
||
* Job 状态归一化。Job 可能只返回字符串 `status`,也可能只返回数字 `state`,
|
||
* 两个入口(watch 监听与 debug 轮询)必须给出一致的解释,否则同一个 Job
|
||
* 在一处被视为已完成、在另一处被视为仍在运行。
|
||
*
|
||
* 数字 state 的语义:0 完成、1 运行中、2 失败(与 watch 侧既有约定一致)。
|
||
*/
|
||
export const TERMINAL_JOB_STATES = new Set([
|
||
"COMPLETED",
|
||
"FAILED",
|
||
"STOP",
|
||
"STOPPED",
|
||
"CANCELLED",
|
||
"CANCELED",
|
||
]);
|
||
|
||
export function normalizeJobStatus(job?: Partial<WorkflowJobDetail> | Record<string, unknown> | null): string {
|
||
if (!job) return "UNKNOWN";
|
||
const rawStatus = (job as Record<string, unknown>).status;
|
||
const rawState = (job as Record<string, unknown>).state;
|
||
if (typeof rawStatus === "string" && rawStatus.trim()) {
|
||
const normalized = rawStatus.trim().toUpperCase();
|
||
return normalized === "STARTED" ? "RUNNING" : normalized;
|
||
}
|
||
if (rawState === 1 || rawState === "1") return "RUNNING";
|
||
if (rawState === 2 || rawState === "2") return "FAILED";
|
||
if (rawState === 0 || rawState === "0") return "COMPLETED";
|
||
if (rawState !== undefined && rawState !== null) return String(rawState).toUpperCase();
|
||
return "UNKNOWN";
|
||
}
|
||
|
||
export function isJobTerminal(status: string): boolean {
|
||
return TERMINAL_JOB_STATES.has(status.toUpperCase());
|
||
}
|