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

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

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

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

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

70 lines
2.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.

'use strict';
/** @type {number} 扫描间隔兜底默认值(毫秒):1 小时,配置缺失时使用 */
const DEFAULT_SCAN_INTERVAL_MS = 60 * 60 * 1000;
/** @type {number} token 有效期兜底默认值(毫秒):3 个月(按 30 天/月折算),配置缺失时使用 */
const DEFAULT_MAX_AGE_MS = 3 * 30 * 24 * 60 * 60 * 1000;
/** @type {number} 批处理兜底默认值(条) */
const DEFAULT_BATCH_SIZE = 50;
/**
* 工作台 token 超期强制下线定时任务。
* 由 config.workbenchTokenExpire 控制:
* - enable=false 时不注册本任务;
* - scanIntervalMs 控制扫描间隔(毫秒),开发环境可覆盖为短循环便于测试;
* - maxAgeMs 控制 token 有效期阈值,token_updated_at 距今超过该时长即强制下线;
* - batchSize 控制单批处理条数。
* 实际清理逻辑收敛在 service.systems.workbench.clearExpiredWorkbenchTokens。
* @param {Object} app Egg 应用实例
* @return {Object} 定时任务定义(含 schedule 与 task)
*/
module.exports = app => {
const workbenchTokenExpire = (app.config && app.config.workbenchTokenExpire) || {};
const scanIntervalMs = Number(workbenchTokenExpire.scanIntervalMs) > 0
? Number(workbenchTokenExpire.scanIntervalMs)
: DEFAULT_SCAN_INTERVAL_MS;
return {
schedule: {
interval: scanIntervalMs,
type: 'worker',
immediate: false,
disable: workbenchTokenExpire.enable === false,
},
/**
* 扫描并清理 token 超期未更新的工作台,使其强制下线。
* 采用单 worker 防重入:上一轮未结束时跳过本轮。
* @param {Object} ctx Egg 上下文
* @return {Promise<void>} 无返回值
*/
async task(ctx) {
const scheduleName = 'workbench_token_expire';
const config = ctx.app.config.workbenchTokenExpire || {};
if (ctx.app._workbenchTokenExpireRunning) {
ctx.logger.info(`[${scheduleName}] 本轮任务跳过,上一轮尚未结束`);
return;
}
ctx.app._workbenchTokenExpireRunning = true;
try {
const maxAgeMs = Number(config.maxAgeMs) > 0 ? Number(config.maxAgeMs) : DEFAULT_MAX_AGE_MS;
const batchSize = Number(config.batchSize) > 0 ? Math.floor(Number(config.batchSize)) : DEFAULT_BATCH_SIZE;
const expiredBefore = new Date(Date.now() - maxAgeMs);
const summary = await ctx.service.systems.workbench.clearExpiredWorkbenchTokens({
expiredBefore,
batchSize,
});
ctx.logger.info(`[${scheduleName}] 本轮扫描完成:maxAgeMs=${maxAgeMs} expiredBefore=${expiredBefore.toISOString()} scanned=${summary.total_scanned} cleared=${summary.total_cleared} failed=${summary.total_failed}`);
} catch (error) {
ctx.logger.error(`[${scheduleName}] 执行失败: ${error.message}`);
} finally {
ctx.app._workbenchTokenExpireRunning = false;
}
},
};
};