WMS_MAIN/app/schedule/sync_finished_wave_batch.js
liufumingren 05bef9210a fix(wave): 波次自动终结加固,防建波中间态误关(B2026100900660 事故)
- 年龄门槛 5->30 分钟,覆盖建波后订单绑定延迟的分钟级空窗
- 判空新增波次索引表交叉校验:订单表与索引表双空判为未绑定中间态不终结;索引有行时抽样核对识别读偏
- 候选条件与终结 UPDATE 改用 UTC_TIMESTAMP,与 create_time UTC 存储口径显式一致
- 调度执行日志补 unbound/misread/probeFailed 统计
- 测试 9->15 用例(双空/读偏/正常离表/查询失败/抽样失败/时区口径)
2026-10-09 20:05:36 +08:00

128 lines
4.3 KiB
JavaScript
Raw Permalink 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,
unbound_count: result ? result.unbound_count : 0,
misread_count: result ? result.misread_count : 0,
probe_failed_count: result ? result.probe_failed_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}`
+ ` unbound=${result ? result.unbound_count : 0} misread=${result ? result.misread_count : 0}`
+ ` probeFailed=${result ? result.probe_failed_count : 0}`
+ ` warehouses=${result ? result.warehouse_ids.length : 0}`
);
},
};