- 数据库迁移与本地缓存:在 `core/db/migrations.ts` 新增 version 5 迁移 (`005_workflow_job_cache`),创建 `workflow_job_index` 缓存表及高效复合索引;实现 `core/db/workflow-job-cache.ts`,支持按企业+工作流+单号快速检索与幂等增量缓存; - 流式早停探查与业务编排:在 `domains/platform/workflow/execution/search-by-flow.ts` 实现 `searchWorkflowJobByFlowCode`,采用「本地 SQLite 缓存 0ms 直达 ➔ 远端流式探查早停熔断并自动建立本地索引」机制,彻底解决云端 Piper API 无单号索引导致的排查低效问题; - CLI 命令层升级: - `workflow logs <pipelineId> --flow=<单号>`:直接秒级定位单据关联 Job 实例; - `platform records <业务对象>`:支持单参数业务对象名称,自动在企业全量自建应用中全局反查与智能寻址,若跨应用重名则抛出精准候选; - 命令行元数据对齐:在 `command-metadata.ts` 为 `workflow` 补齐 `--max-pages`、`--maxPages` 与 `--page-limit` 选项注册,满足严格参数词汇表静态检查; - 测试与文档:新增并更新 `workflow-job-cache.test.ts`、`search-by-flow.test.ts`、`platform.test.ts` 与 `db.test.ts`,更新核心与域层文档,全量 1126 个测试与类型检查全部通过。
169 lines
4.8 KiB
TypeScript
169 lines
4.8 KiB
TypeScript
import type { KyInstance } from "ky";
|
|
import { getCachedWorkflowJobByCode, upsertWorkflowJobIndex, type WorkflowJobCacheRecord } from "../../../../core/db/index.js";
|
|
import { getJob } from "./queries.js";
|
|
import { listWorkflowLogs } from "../transport/pipeline/api.js";
|
|
import type { WorkflowJobDetail } from "../types.js";
|
|
|
|
export interface SearchJobByFlowOptions {
|
|
maxScanPages?: number;
|
|
pageSize?: number;
|
|
}
|
|
|
|
export interface SearchJobByFlowResult {
|
|
found: boolean;
|
|
source?: "cache" | "scan";
|
|
job?: WorkflowJobDetail;
|
|
jobId?: string;
|
|
flowCode?: string;
|
|
scannedPages?: number;
|
|
totalScanned?: number;
|
|
}
|
|
|
|
/**
|
|
* 提取工作流 Job 内部携带的触发单号
|
|
*/
|
|
export function extractJobFlowCode(job: any): string | undefined {
|
|
if (!job) return undefined;
|
|
const inputs = job.inputs || {};
|
|
if (inputs.code && typeof inputs.code === "string") return inputs.code.trim();
|
|
if (inputs.flowCode && typeof inputs.flowCode === "string") return inputs.flowCode.trim();
|
|
if (job.code && typeof job.code === "string") return job.code.trim();
|
|
return undefined;
|
|
}
|
|
|
|
/**
|
|
* 根据单据编号 (flowCode) 高效查找对应的工作流运行实例 (Job)
|
|
* 采用【本地 SQLite 缓存命中 ➔ 未命中流式翻页探查并写入缓存】的双层机制
|
|
*/
|
|
export async function searchWorkflowJobByFlowCode(
|
|
client: KyInstance,
|
|
corpId: string,
|
|
pipelineId: string,
|
|
flowCode: string,
|
|
options: SearchJobByFlowOptions = {}
|
|
): Promise<SearchJobByFlowResult> {
|
|
const targetCode = flowCode.trim();
|
|
if (!targetCode) {
|
|
return { found: false, flowCode: targetCode };
|
|
}
|
|
|
|
// 1. 本地 SQLite 缓存优先探查 (0ms 命中)
|
|
try {
|
|
const cached = getCachedWorkflowJobByCode(corpId, pipelineId, targetCode);
|
|
if (cached?.jobId) {
|
|
try {
|
|
const fullJob = await getJob(client, cached.jobId);
|
|
if (fullJob) {
|
|
return {
|
|
found: true,
|
|
source: "cache",
|
|
job: fullJob,
|
|
jobId: cached.jobId,
|
|
flowCode: targetCode,
|
|
};
|
|
}
|
|
} catch {
|
|
// 若远程已清理或失效,平滑回退至实时扫描
|
|
}
|
|
}
|
|
} catch {}
|
|
|
|
// 2. 本地未命中,启动流式嗅探拉取(带本地边拉边存与早停熔断)
|
|
const maxPages = options.maxScanPages ?? 10;
|
|
const pageSize = options.pageSize ?? 20;
|
|
let totalScanned = 0;
|
|
|
|
for (let page = 1; page <= maxPages; page++) {
|
|
const logPage = await listWorkflowLogs(client, pipelineId, {
|
|
page,
|
|
pageSize,
|
|
});
|
|
|
|
const jobs = logPage.jobList || [];
|
|
if (!jobs.length) {
|
|
break;
|
|
}
|
|
|
|
// 并发提取当前页所有 Job 详情以解析其单号
|
|
const batchDetails = await Promise.allSettled(
|
|
jobs.map(async (j) => {
|
|
try {
|
|
const detail = await getJob(client, j.jobId);
|
|
return { summary: j, detail };
|
|
} catch {
|
|
return { summary: j, detail: null };
|
|
}
|
|
})
|
|
);
|
|
|
|
const cacheBatch: WorkflowJobCacheRecord[] = [];
|
|
let matchedJob: WorkflowJobDetail | null = null;
|
|
|
|
for (const item of batchDetails) {
|
|
if (item.status !== "fulfilled") continue;
|
|
const { summary, detail } = item.value;
|
|
totalScanned++;
|
|
|
|
const parsedCode = detail ? extractJobFlowCode(detail) : undefined;
|
|
const parsedFlowId = detail?.inputs?.flowId || undefined;
|
|
const userName = detail?.inputs?.userInfo?.name || undefined;
|
|
|
|
let recordTime: number | undefined;
|
|
if (summary.executeTime) {
|
|
const t = typeof summary.executeTime === "number" ? summary.executeTime : new Date(summary.executeTime).getTime();
|
|
recordTime = Number.isNaN(t) ? undefined : t;
|
|
} else if (detail?.startTime) {
|
|
const t = typeof detail.startTime === "number" ? detail.startTime : new Date(detail.startTime).getTime();
|
|
recordTime = Number.isNaN(t) ? undefined : t;
|
|
}
|
|
|
|
cacheBatch.push({
|
|
corpId,
|
|
pipelineId,
|
|
jobId: summary.jobId,
|
|
flowCode: parsedCode,
|
|
flowId: parsedFlowId,
|
|
executeTime: recordTime,
|
|
status: summary.status || detail?.status,
|
|
userName,
|
|
});
|
|
|
|
if (
|
|
parsedCode &&
|
|
(parsedCode === targetCode || parsedCode.toUpperCase() === targetCode.toUpperCase())
|
|
) {
|
|
matchedJob = detail;
|
|
}
|
|
}
|
|
|
|
// 将拉取到的索引批量落盘至本地 SQLite 缓存
|
|
try {
|
|
upsertWorkflowJobIndex(cacheBatch);
|
|
} catch {}
|
|
|
|
// 一旦命中立即短路熔断,返回目标单据的完整 Job 记录
|
|
if (matchedJob) {
|
|
return {
|
|
found: true,
|
|
source: "scan",
|
|
job: matchedJob,
|
|
jobId: matchedJob.id,
|
|
flowCode: targetCode,
|
|
scannedPages: page,
|
|
totalScanned,
|
|
};
|
|
}
|
|
|
|
if (page >= (logPage.totalPages || 0)) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return {
|
|
found: false,
|
|
flowCode: targetCode,
|
|
scannedPages: maxPages,
|
|
totalScanned,
|
|
};
|
|
}
|