WMS_MAIN/app/schedule/intercept_auto_confirm.js
liufumingren 6939d9600a feat: 拦截链路完善与车绑定修复
- 订单表新增 intercept_time 拦截时间字段(迁移覆盖基表与全部分表),全拦截链路写入拦截时间、解除拦截清空

- 新增自动库内拦截确定定时任务(1h 间隔,扫描 requested+未打印订单,超时自动库内拦截确定)

- 复核链路自动移除波次内未打印拦截订单回池,拦截解除后可重新抓取发货

- 打印解绑车判定排除拦截订单,修复波次含拦截订单时车无法解锁

- 同步 methods 文档与变更记录;含 wave_scope 回填、workbench token 过期等前序迁移
2026-09-07 03:48:43 +08:00

117 lines
3.8 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/intercept_auto_confirm.js
'use strict';
const EXECUTION_LOG_CACHE_KEY = 'schedule:intercept_auto_confirm:last_execution';
const EXECUTION_LOG_CACHE_TTL = 900; // 15 分钟
const EXECUTION_LOG_HISTORY_CACHE_KEY = 'schedule:intercept_auto_confirm: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(`[intercept-auto-confirm] 写入执行日志缓存失败: ${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(`[intercept-auto-confirm] 写入执行历史缓存失败: ${err.message}`);
}
}
/**
* 每 1 小时在单 worker 中执行一次自动库内拦截确定。
*
* 扫描待处理表(current 分表)中「拦截状态为请求拦截(intercept_status='requested')
* 且面单未打印(print_status='none')」的订单,满足拦截时间超 12 小时或
* 创建时间超 36 小时任一条件时,自动执行库内拦截确定。
*/
module.exports = {
schedule: {
interval: '1h',
type: 'worker',
immediate: false,
disable: false,
},
/**
* 执行自动库内拦截确定任务。
*
* @param {Object} ctx Egg 上下文。
* @return {Promise<void>} 无返回值。
*/
async task(ctx) {
if (ctx.app._interceptAutoConfirmRunning) {
return;
}
ctx.app._interceptAutoConfirmRunning = true;
const startedAt = Date.now();
let status = 'success';
let errorMessage = '';
let result = null;
try {
result = await ctx.service.systems.interceptAutoConfirm.runAutoConfirm();
} catch (error) {
status = 'error';
errorMessage = error.message;
ctx.logger.error(`[intercept-auto-confirm] 执行失败: ${error.message}`);
} finally {
ctx.app._interceptAutoConfirmRunning = false;
}
const logEntry = {
executed_at: new Date().toISOString(),
duration_ms: Date.now() - startedAt,
status,
error_message: errorMessage,
warehouse_count: result ? result.warehouse_count : 0,
candidate_count: result ? result.candidate_count : 0,
confirmed_count: result ? result.confirmed_count : 0,
failed_count: result ? result.failed_count : 0,
warehouse_ids: result ? result.warehouse_ids : [],
};
await writeExecutionLogToCache(ctx, logEntry);
await appendExecutionHistory(ctx, logEntry);
ctx.logger.info(
`[intercept-auto-confirm] 本轮完成 warehouses=${logEntry.warehouse_count}`
+ ` candidate=${logEntry.candidate_count} confirmed=${logEntry.confirmed_count}`
+ ` failed=${logEntry.failed_count} duration=${logEntry.duration_ms}ms`
);
},
};