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 { 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, }; }