WMS_MAIN/app/schedule/sync_finished_wave_batch.js
liufumingren 90d5701a53 feat(wave): item_detail_machine 改 TEXT 并新增波次自动终结
- wms_wave_batch.item_detail_machine VARCHAR(1024) -> TEXT, 兼容多商品长结构标识
- 新增 wave_batch_closure 服务: 扫描活动波次(0/1)中订单已全部离开待处理表的波次并收敛 wave_status=5
- 新增 sync_finished_wave_batch 定时任务与执行日志缓存
2026-08-21 02:20:17 +08:00

119 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// app/schedule/sync_finished_wave_batch.js
'use strict';
const EXECUTION_LOG_CACHE_KEY = 'schedule:sync_finished_wave_batch:last_execution';
const EXECUTION_LOG_CACHE_TTL = 300; // 5 分钟
const EXECUTION_LOG_HISTORY_CACHE_KEY = 'schedule:sync_finished_wave_batch:history';
const EXECUTION_LOG_HISTORY_MAX = 20; // 保留最近 20 次执行记录
const EXECUTION_LOG_HISTORY_TTL = 86400; // 1 天
/**
* 将本次执行日志写入缓存。
*
* @param {Object} ctx Egg 上下文。
* @param {Object} logEntry 日志条目。
* @return {Promise<void>}
*/
async function writeExecutionLogToCache(ctx, logEntry) {
try {
if (ctx.app && typeof ctx.app.setCacheAsync === 'function') {
await ctx.app.setCacheAsync(EXECUTION_LOG_CACHE_KEY, logEntry, EXECUTION_LOG_CACHE_TTL);
}
} catch (err) {
ctx.logger.warn(`[sync-finished-wave-batch] 写入执行日志缓存失败: ${err.message}`);
}
}
/**
* 将本次执行日志追加到历史记录缓存(保留最近 N 次)。
*
* @param {Object} ctx Egg 上下文。
* @param {Object} logEntry 日志条目。
* @return {Promise<void>}
*/
async function appendExecutionHistory(ctx, logEntry) {
try {
if (typeof ctx.app.getCacheAsync !== 'function' || typeof ctx.app.setCacheAsync !== 'function') {
return;
}
const rawHistory = await ctx.app.getCacheAsync(EXECUTION_LOG_HISTORY_CACHE_KEY);
const history = Array.isArray(rawHistory) ? rawHistory : [];
history.unshift(logEntry);
await ctx.app.setCacheAsync(
EXECUTION_LOG_HISTORY_CACHE_KEY,
history.slice(0, EXECUTION_LOG_HISTORY_MAX),
EXECUTION_LOG_HISTORY_TTL
);
} catch (err) {
ctx.logger.warn(`[sync-finished-wave-batch] 写入执行历史缓存失败: ${err.message}`);
}
}
/**
* 每 1 分钟在单 worker 中执行一次波次自动终结。
*
* 将"订单已全部离开待处理表"的活动波次(wave_status IN (0,1))收敛为
* wave_status=5(复核完成),修复遗留 0 单波次在 batchList 中展示的问题。
*/
module.exports = {
schedule: {
interval: '1m',
type: 'worker',
immediate: false,
disable: false,
},
/**
* 执行波次自动终结任务。
*
* @param {Object} ctx Egg 上下文。
* @return {Promise<void>} 无返回值。
*/
async task(ctx) {
if (ctx.app._syncFinishedWaveBatchRunning) {
return;
}
ctx.app._syncFinishedWaveBatchRunning = true;
const startedAt = Date.now();
let status = 'success';
let errorMessage = '';
let result = null;
try {
result = await ctx.service.systems.waveBatchClosure.closeFinishedWaves();
if (result && result.closed_count > 0) {
ctx.logger.info(
`[sync-finished-wave-batch] 自动终结 ${result.closed_count} 个波次: ${JSON.stringify(result.closed_waves)}`
);
}
} catch (error) {
status = 'error';
errorMessage = error.message;
ctx.logger.error(`[sync-finished-wave-batch] 执行失败: ${error.message}`);
} finally {
ctx.app._syncFinishedWaveBatchRunning = false;
}
const logEntry = {
executed_at: new Date().toISOString(),
duration_ms: Date.now() - startedAt,
status,
error_message: errorMessage,
candidate_count: result ? result.candidate_count : 0,
closed_count: result ? result.closed_count : 0,
warehouse_ids: result ? result.warehouse_ids : [],
closed_waves: result ? result.closed_waves : [],
};
await writeExecutionLogToCache(ctx, logEntry);
await appendExecutionHistory(ctx, logEntry);
ctx.logger.info(
`[sync-finished-wave-batch] 本轮完成 closed=${result ? result.closed_count : 0} candidate=${result ? result.candidate_count : 0} warehouses=${result ? result.warehouse_ids.length : 0}`
);
},
};