291 lines
8.2 KiB
TypeScript
291 lines
8.2 KiB
TypeScript
import { logDebug, logError, logInfo, logWarn } from '../../utils/logger.js'
|
|
import { createHttpError } from '../../utils/http.js'
|
|
import type { JsonObject } from '../../types/json.js'
|
|
import { getScheduledJobById, getScheduledJobsConfig } from './config-service.js'
|
|
import { runDepositUnfreezeJob } from './deposit-unfreeze-job.js'
|
|
import { runWorkOrderTimeoutJob } from './work-order-timeout-job.js'
|
|
import { runDataRetentionJob } from './data-retention-job.js'
|
|
import { runPddOrderSyncJob } from './pdd-order-sync-job.js'
|
|
import {
|
|
runCarrierDispatchJob,
|
|
runChannelAssignJob,
|
|
runCostSnapshotJob,
|
|
runLedgerPostJob,
|
|
runOrderServiceAutoPublishJobs,
|
|
runProductNormalizeJob,
|
|
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>()
|
|
|
|
export function startScheduledJobs() {
|
|
reloadScheduledJobs()
|
|
}
|
|
|
|
export function stopScheduledJobs() {
|
|
for (const timer of timers.values()) {
|
|
clearTimeout(timer)
|
|
}
|
|
timers.clear()
|
|
}
|
|
|
|
export function reloadScheduledJobs() {
|
|
stopScheduledJobs()
|
|
const config = getScheduledJobsConfig()
|
|
|
|
if (config.enabled === false) {
|
|
logInfo('[scheduler]', '定时任务系统已停用', {})
|
|
return
|
|
}
|
|
|
|
for (const job of config.jobs) {
|
|
if (job.enabled !== true) {
|
|
updateJobState(job.id, {
|
|
enabled: false,
|
|
running: false,
|
|
nextRunAt: '',
|
|
})
|
|
continue
|
|
}
|
|
|
|
scheduleJob(job, 1000)
|
|
}
|
|
}
|
|
|
|
export function getScheduledJobRuntimeStates() {
|
|
return Array.from(jobStates.values())
|
|
}
|
|
|
|
export async function runScheduledJobNow(jobId: unknown) {
|
|
const config = getScheduledJobsConfig()
|
|
const job = config.jobs.find(
|
|
(item) => String(item.id || '').trim() === String(jobId || '').trim(),
|
|
)
|
|
|
|
if (!job) {
|
|
throw createHttpError('定时任务不存在', {
|
|
statusCode: 404,
|
|
errorCode: 'scheduled_job_not_found',
|
|
})
|
|
}
|
|
|
|
const currentTimer = timers.get(job.id)
|
|
if (currentTimer) {
|
|
clearTimeout(currentTimer)
|
|
timers.delete(job.id)
|
|
}
|
|
|
|
const result = await runJob(job, { manual: true })
|
|
if (getScheduledJobsConfig().enabled !== false) {
|
|
rescheduleJob(job)
|
|
}
|
|
return result
|
|
}
|
|
|
|
function rescheduleJob(job: JsonObject) {
|
|
const jobId = String(job.id || '').trim()
|
|
if (!jobId) return
|
|
const latestJob = getScheduledJobById(jobId)
|
|
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) {
|
|
const jobId = String(job.id || '').trim()
|
|
if (!jobId) {
|
|
return
|
|
}
|
|
|
|
const nextRunAt = new Date(Date.now() + delayMs).toISOString()
|
|
updateJobState(jobId, {
|
|
enabled: true,
|
|
nextRunAt,
|
|
})
|
|
|
|
const timer = setTimeout(() => {
|
|
timers.delete(jobId)
|
|
void runJob(job).finally(() => {
|
|
rescheduleJob(job)
|
|
})
|
|
}, delayMs)
|
|
|
|
timers.set(jobId, timer)
|
|
}
|
|
|
|
async function runJob(job: JsonObject, { manual = false }: { manual?: boolean } = {}) {
|
|
const jobId = String(job.id || '').trim()
|
|
const startedAt = new Date().toISOString()
|
|
|
|
if (!jobId) {
|
|
return null
|
|
}
|
|
|
|
if (jobStates.get(jobId)?.running) {
|
|
logWarn('[scheduler]', '定时任务仍在运行,跳过本次执行', { jobId })
|
|
return {
|
|
skipped: true,
|
|
reason: 'running',
|
|
}
|
|
}
|
|
|
|
updateJobState(jobId, {
|
|
enabled: job.enabled === true,
|
|
running: true,
|
|
lastRunAt: startedAt,
|
|
lastStatus: 'running',
|
|
lastMessage: '执行中',
|
|
})
|
|
publishPipelineRealtimeChange(jobId, 'started')
|
|
|
|
try {
|
|
const result = await dispatchJob(job)
|
|
const summary = result && typeof result === 'object' ? (result as JsonObject) : {}
|
|
updateJobState(jobId, {
|
|
running: false,
|
|
lastFinishedAt: new Date().toISOString(),
|
|
lastStatus: String(summary.status || 'ok'),
|
|
lastMessage: String(summary.message || '执行完成'),
|
|
lastAsset: typeof summary.asset === 'number' ? summary.asset : null,
|
|
lastThreshold: typeof summary.threshold === 'number' ? summary.threshold : null,
|
|
lastAccounts: Array.isArray(summary.accounts) ? summary.accounts : [],
|
|
lastAccountCount: Number(summary.accountCount || 0),
|
|
lastCheckedCount: Number(summary.checkedCount || 0),
|
|
lastOkCount: Number(summary.okCount || 0),
|
|
lastLowAssetCount: Number(summary.lowAssetCount || 0),
|
|
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。
|
|
const isQuietSuccess =
|
|
!manual && status === 'ok' && failedCount <= 0 && job.type !== 'data_retention'
|
|
const doneLog = isQuietSuccess ? logDebug : logInfo
|
|
doneLog('[scheduler]', '定时任务执行完成', {
|
|
jobId,
|
|
type: job.type,
|
|
status,
|
|
...(job.type === 'data_retention'
|
|
? {
|
|
matchLogDeletedRows: Number(summary.matchLogDeletedRows || 0),
|
|
sourceOrderAmountBackfillRows: Number(summary.sourceOrderAmountBackfillRows || 0),
|
|
costExceptionArchivedRows: Number(summary.costExceptionArchivedRows || 0),
|
|
}
|
|
: {}),
|
|
})
|
|
return result
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : String(error || '定时任务执行失败')
|
|
updateJobState(jobId, {
|
|
running: false,
|
|
lastFinishedAt: new Date().toISOString(),
|
|
lastStatus: 'failed',
|
|
lastMessage: message,
|
|
lastManual: manual,
|
|
})
|
|
publishPipelineRealtimeChange(jobId, 'failed')
|
|
logError('[scheduler]', '定时任务执行失败', {
|
|
jobId,
|
|
type: job.type,
|
|
error,
|
|
})
|
|
return {
|
|
ok: false,
|
|
status: 'failed',
|
|
message,
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
if (job.type === 'deposit_unfreeze') {
|
|
return runDepositUnfreezeJob(job)
|
|
}
|
|
if (job.type === 'data_retention') {
|
|
return runDataRetentionJob()
|
|
}
|
|
if (job.type === 'pdd_order_sync') {
|
|
return runPddOrderSyncJob(job)
|
|
}
|
|
if (job.type === 'channel_assign') {
|
|
return runChannelAssignJob(job)
|
|
}
|
|
if (job.type === 'carrier_dispatch') {
|
|
return runCarrierDispatchJob(job)
|
|
}
|
|
if (job.type === 'product_normalize') {
|
|
return runProductNormalizeJob(job)
|
|
}
|
|
if (job.type === 'cost_snapshot') {
|
|
return runCostSnapshotJob(job)
|
|
}
|
|
if (job.type === 'sales_facts') {
|
|
return runSalesFactsJob(job)
|
|
}
|
|
if (job.type === 'ledger_post') {
|
|
return runLedgerPostJob(job)
|
|
}
|
|
if (job.type === 'order_service_auto_publish') {
|
|
return runOrderServiceAutoPublishJobs(job)
|
|
}
|
|
if (job.type === 'voucher_consume_dispatch') {
|
|
return runVoucherConsumeDispatchJob(
|
|
job.config && typeof job.config === 'object' ? (job.config as Record<string, unknown>) : {},
|
|
)
|
|
}
|
|
|
|
throw createHttpError(`不支持的定时任务类型:${job.type}`, {
|
|
statusCode: 400,
|
|
errorCode: 'unsupported_scheduled_job_type',
|
|
})
|
|
}
|
|
|
|
function updateJobState(jobId: string, patch: JsonObject) {
|
|
const current = jobStates.get(jobId) || {
|
|
id: jobId,
|
|
enabled: false,
|
|
running: false,
|
|
lastRunAt: '',
|
|
lastFinishedAt: '',
|
|
nextRunAt: '',
|
|
lastStatus: 'pending',
|
|
lastMessage: '',
|
|
lastAsset: null,
|
|
lastThreshold: null,
|
|
lastAccounts: [],
|
|
lastAccountCount: 0,
|
|
lastCheckedCount: 0,
|
|
lastOkCount: 0,
|
|
lastLowAssetCount: 0,
|
|
lastFailedCount: 0,
|
|
lastManual: false,
|
|
}
|
|
jobStates.set(jobId, {
|
|
...current,
|
|
...patch,
|
|
})
|
|
}
|