WMS_MAIN/app/schedule/sync_finished_wave_batch.js
liufumingren df641257cc fix(wave): 波次闭环判空二次确认,避免瞬时空表误终结波次
- wave_batch_closure.js 新增判空二次确认:同一波次需连续两轮(间隔不小于
  confirmMinIntervalMs,默认 30s)都被判定为空才允许终结,确认状态写 Redis
  (TTL 1h);状态缓存不可用或读取异常时本轮不终结任何波次(宁可漏关、不可误关)
- 规避批量解波/订单迁移进行中造成的瞬时空表误判(2026-09-12 生产事故)
- closed_count 改为 UPDATE 实际影响行数,新增 empty_count / confirmed_count /
  pending_confirm_count 等统计与日志(波次列表超长截断)
- sync_finished_wave_batch.js 调度日志同步输出 empty_count / pending_confirm_count
- systemMethods/order/wave.js 解波 UPDATE 增加 wave_status < 3 守卫,避免覆盖
  自动终结(5)及已进入拣货及后续环节的波次状态
2026-09-15 16:18:58 +08:00

122 lines
3.9 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,
empty_count: result ? result.empty_count : 0,
closed_count: result ? result.closed_count : 0,
pending_confirm_count: result ? result.pending_confirm_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}`
+ ` pendingConfirm=${result ? result.pending_confirm_count : 0} warehouses=${result ? result.warehouse_ids.length : 0}`
);
},
};