优化(直播履约): 改为订单快照事件驱动
This commit is contained in:
@@ -16,6 +16,7 @@ import {
|
||||
runSalesFactsJob,
|
||||
} from './finance-pipeline-jobs.js'
|
||||
import { runVoucherConsumeDispatchJob } from '../fulfillment/voucher-consume-queue-service.js'
|
||||
import { publishRealtimeEvent } from '../realtime/realtime-event-service.js'
|
||||
|
||||
const timers = new Map<string, NodeJS.Timeout>()
|
||||
const jobStates = new Map<string, JsonObject>()
|
||||
@@ -91,6 +92,7 @@ function rescheduleJob(job: JsonObject) {
|
||||
if (!latestJob || latestJob.enabled !== true) return
|
||||
const intervalMs = Math.max(1, Number(latestJob.intervalSeconds || 20)) * 1000
|
||||
scheduleJob(latestJob, intervalMs)
|
||||
publishPipelineRealtimeChange(latestJob.id, 'scheduled')
|
||||
}
|
||||
|
||||
function scheduleJob(job: JsonObject, delayMs: number) {
|
||||
@@ -138,6 +140,7 @@ async function runJob(job: JsonObject, { manual = false }: { manual?: boolean }
|
||||
lastStatus: 'running',
|
||||
lastMessage: '执行中',
|
||||
})
|
||||
publishPipelineRealtimeChange(jobId, 'started')
|
||||
|
||||
try {
|
||||
const result = await dispatchJob(job)
|
||||
@@ -157,6 +160,7 @@ async function runJob(job: JsonObject, { manual = false }: { manual?: boolean }
|
||||
lastFailedCount: Number(summary.failedCount || 0),
|
||||
lastManual: manual,
|
||||
})
|
||||
publishPipelineRealtimeChange(jobId, 'finished')
|
||||
const status = String(summary.status || 'ok')
|
||||
const failedCount = Number(summary.failedCount || 0)
|
||||
// 成功且无失败的周期空跑降到 debug(PDD 约 20s 一轮),手动触发与失败仍保留 info。
|
||||
@@ -185,6 +189,7 @@ async function runJob(job: JsonObject, { manual = false }: { manual?: boolean }
|
||||
lastMessage: message,
|
||||
lastManual: manual,
|
||||
})
|
||||
publishPipelineRealtimeChange(jobId, 'failed')
|
||||
logError('[scheduler]', '定时任务执行失败', {
|
||||
jobId,
|
||||
type: job.type,
|
||||
@@ -198,6 +203,20 @@ async function runJob(job: JsonObject, { manual = false }: { manual?: boolean }
|
||||
}
|
||||
}
|
||||
|
||||
function publishPipelineRealtimeChange(jobId: unknown, phase: string) {
|
||||
publishRealtimeEvent({
|
||||
type: 'fulfillment_pipeline.changed',
|
||||
entityId: 0,
|
||||
scopes: ['admin_fulfillment_pipelines'],
|
||||
operation: 'refresh',
|
||||
data: {
|
||||
jobId: String(jobId || '').trim(),
|
||||
phase: String(phase || '').trim(),
|
||||
},
|
||||
admin: true,
|
||||
})
|
||||
}
|
||||
|
||||
function dispatchJob(job: JsonObject) {
|
||||
if (job.type === 'work_order_timeout') {
|
||||
return runWorkOrderTimeoutJob(job)
|
||||
|
||||
Reference in New Issue
Block a user