Files
hose-use/src/core/db/workflow-job-cache.ts
T
gitea 0c9eb27f85 feat(platform): 支持工作流单号本地缓存秒级定位与自建应用业务对象智能反查
- 数据库迁移与本地缓存:在 `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 个测试与类型检查全部通过。
2026-11-13 13:17:14 +08:00

81 lines
2.4 KiB
TypeScript

import { getDatabase } from "./connection.js";
export interface WorkflowJobCacheRecord {
corpId: string;
pipelineId: string;
jobId: string;
flowCode?: string;
flowId?: string;
executeTime?: number;
status?: string;
userName?: string;
updatedAt?: number;
}
/**
* 按企业 + 工作流 + 单号快速从本地 SQLite 缓存中检索匹配的 Job 记录
*/
export function getCachedWorkflowJobByCode(
corpId: string,
pipelineId: string,
flowCode: string,
dbPath?: string
): WorkflowJobCacheRecord | null {
const db = getDatabase(dbPath);
const normalizedCode = flowCode.trim();
const row = db
.query(
`SELECT corp_id AS corpId, pipeline_id AS pipelineId, job_id AS jobId,
flow_code AS flowCode, flow_id AS flowId, execute_time AS executeTime,
status, user_name AS userName, updated_at AS updatedAt
FROM workflow_job_index
WHERE corp_id = ? AND pipeline_id = ? AND (flow_code = ? OR UPPER(flow_code) = UPPER(?))
ORDER BY execute_time DESC, updated_at DESC
LIMIT 1`
)
.get(corpId, pipelineId, normalizedCode, normalizedCode) as WorkflowJobCacheRecord | null;
return row ?? null;
}
/**
* 批量插入或更新 Job 缓存索引
*/
export function upsertWorkflowJobIndex(
records: WorkflowJobCacheRecord[],
dbPath?: string
): void {
if (!records.length) return;
const db = getDatabase(dbPath);
const now = Date.now();
const insertStmt = db.prepare(`
INSERT INTO workflow_job_index (
corp_id, pipeline_id, job_id, flow_code, flow_id, execute_time, status, user_name, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(corp_id, pipeline_id, job_id) DO UPDATE SET
flow_code = COALESCE(excluded.flow_code, workflow_job_index.flow_code),
flow_id = COALESCE(excluded.flow_id, workflow_job_index.flow_id),
execute_time = COALESCE(excluded.execute_time, workflow_job_index.execute_time),
status = COALESCE(excluded.status, workflow_job_index.status),
user_name = COALESCE(excluded.user_name, workflow_job_index.user_name),
updated_at = excluded.updated_at
`);
db.transaction(() => {
for (const r of records) {
insertStmt.run(
r.corpId,
r.pipelineId,
r.jobId,
r.flowCode || null,
r.flowId || null,
r.executeTime || null,
r.status || null,
r.userName || null,
now
);
}
})();
}