WMS_MAIN/app/schedule/auto_finish_pack_task.js
liufumingren e5196dc1ea feat(pack): 打包任务最大时长约束与自动结束
- 仓库设置新增 single_order_pack_max_minutes(默认3)/group_order_pack_max_minutes(默认40) 配置
- 新增每小时定时任务扫描未结束打包任务,超时自动结束(end=start+上限)
- 结束时间统一截断为开始时间+上限时长(finishPack/新任务顶替/setEndTime 接口)
2026-08-15 23:12:51 +08:00

49 lines
1.5 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';
/**
* 打包任务自动结束定时任务。
* 每小时在单 worker 中扫描一次 wms_pack_task 表:
* - 扫描所有未结束的任务(status < 2),区分散单(pack_mode=1)/ 团购(pack_mode=2);
* - 按仓库拉取 subject_config 中配置的打包最大时长
* (散单 single_order_pack_max_minutes / 团购 group_order_pack_max_minutes,单位分钟);
* - 当 当前时间 - 开始时间 > 配置时长 时自动结束,
* 将 end_time 置为 开始时间 + 上限时长,状态置为已完成(2)。
*/
module.exports = {
schedule: {
interval: '1h',
type: 'worker',
immediate: false,
disable: false,
},
/**
* 执行打包任务自动结束任务。
*
* @param {Object} ctx Egg 上下文。
* @return {Promise<void>} 无返回值。
*/
async task(ctx) {
if (ctx.app._autoFinishPackTaskRunning) {
return;
}
ctx.app._autoFinishPackTaskRunning = true;
try {
const result = await ctx.service.systems.packTaskAutoFinish.executeAutoFinishPackTasks();
ctx.logger.info(
'[auto-finish-pack-task] 本轮扫描 %d 条未结束打包任务(涉及 %d 个仓库),自动结束 %d 条',
result && result.total_scanned || 0,
result && result.warehouse_count || 0,
result && result.auto_finished || 0
);
} catch (error) {
ctx.logger.error(`[auto-finish-pack-task] 打包任务自动结束执行失败: ${error.message}`);
} finally {
ctx.app._autoFinishPackTaskRunning = false;
}
},
};