188 lines
5.6 KiB
JavaScript
188 lines
5.6 KiB
JavaScript
'use strict';
|
|
|
|
const DEFAULT_INTERVAL_MS = 60 * 1000;
|
|
const DEFAULT_BATCH_SIZE = 50;
|
|
|
|
/**
|
|
* 归一化打印补偿调度配置。
|
|
* @param {Object} app Egg 应用实例。
|
|
* @return {{enable:boolean,intervalMs:number,batchSize:number,force:boolean}} 标准化配置。
|
|
*/
|
|
function getCompensationConfig(app) {
|
|
const rawConfig = app && app.config && app.config.printRetryCompensation
|
|
? app.config.printRetryCompensation
|
|
: {};
|
|
const intervalMs = Number.parseInt(rawConfig.intervalMs, 10);
|
|
const batchSize = Number.parseInt(rawConfig.batchSize, 10);
|
|
return {
|
|
enable: Boolean(rawConfig.enable),
|
|
intervalMs: Number.isInteger(intervalMs) && intervalMs > 0 ? intervalMs : DEFAULT_INTERVAL_MS,
|
|
batchSize: Number.isInteger(batchSize) && batchSize > 0 ? batchSize : DEFAULT_BATCH_SIZE,
|
|
force: Boolean(rawConfig.force),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 按仓库维度分组打印记录。
|
|
* @param {Array<Object>} printRecords 打印记录列表。
|
|
* @return {Map<number, Array<number>>} 仓库到打印记录 ID 的映射。
|
|
*/
|
|
function groupPrintIdsByWarehouse(printRecords = []) {
|
|
const groupedMap = new Map();
|
|
if (!Array.isArray(printRecords)) {
|
|
return groupedMap;
|
|
}
|
|
|
|
for (const printRecord of printRecords) {
|
|
const warehouseId = Number(printRecord && printRecord.warehouse_id);
|
|
const printId = Number(printRecord && printRecord.id);
|
|
if (!Number.isInteger(warehouseId) || warehouseId <= 0 || !Number.isInteger(printId) || printId <= 0) {
|
|
continue;
|
|
}
|
|
|
|
const currentPrintIds = groupedMap.get(warehouseId) || [];
|
|
currentPrintIds.push(printId);
|
|
groupedMap.set(warehouseId, currentPrintIds);
|
|
}
|
|
|
|
return groupedMap;
|
|
}
|
|
|
|
/**
|
|
* 自动扫描并触发失败打印补偿任务。
|
|
*/
|
|
module.exports = {
|
|
schedule: {
|
|
interval: '30s',
|
|
type: 'worker',
|
|
immediate: false,
|
|
disable: false,
|
|
},
|
|
|
|
/**
|
|
* 执行打印失败自动补偿任务。
|
|
* @param {Object} ctx Egg 上下文。
|
|
* @return {Promise<void>} 无返回值。
|
|
*/
|
|
async task(ctx) {
|
|
const taskName = 'print-retry-compensation';
|
|
const compensationConfig = getCompensationConfig(ctx.app);
|
|
|
|
if (!compensationConfig.enable) {
|
|
return;
|
|
}
|
|
|
|
if (ctx.app._printRetryCompensationRunning) {
|
|
ctx.logger.info(`[${taskName}] 任务仍在运行中,跳过本轮调度`);
|
|
return;
|
|
}
|
|
|
|
const nowMs = Date.now();
|
|
const lastRunAt = Number(ctx.app._printRetryCompensationLastRunAt || 0);
|
|
if (lastRunAt > 0 && (nowMs - lastRunAt) < compensationConfig.intervalMs) {
|
|
return;
|
|
}
|
|
|
|
ctx.app._printRetryCompensationRunning = true;
|
|
ctx.app._printRetryCompensationLastRunAt = nowMs;
|
|
|
|
let scannedCount = 0;
|
|
let invokeCount = 0;
|
|
let retriedSuccessCount = 0;
|
|
let retriedFailedCount = 0;
|
|
let skippedCount = 0;
|
|
|
|
try {
|
|
const { Op } = ctx.app.Sequelize;
|
|
const printRecords = await ctx.model.PrintRecord.findAll({
|
|
attributes: [ 'id', 'warehouse_id', 'retry_count', 'max_retry_count', 'next_retry_at', 'send_status', 'print_status' ],
|
|
where: {
|
|
[Op.and]: [
|
|
{
|
|
[Op.or]: [
|
|
{ send_status: 'send_failed' },
|
|
{
|
|
print_status: {
|
|
[Op.in]: [ 'print_failed', 'timeout' ],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
next_retry_at: {
|
|
[Op.lte]: new Date(),
|
|
},
|
|
},
|
|
{
|
|
retry_count: {
|
|
[Op.lt]: ctx.app.Sequelize.col('max_retry_count'),
|
|
},
|
|
},
|
|
],
|
|
},
|
|
order: [
|
|
[ 'next_retry_at', 'ASC' ],
|
|
[ 'id', 'ASC' ],
|
|
],
|
|
limit: compensationConfig.batchSize,
|
|
raw: true,
|
|
});
|
|
|
|
scannedCount = Array.isArray(printRecords) ? printRecords.length : 0;
|
|
if (scannedCount === 0) {
|
|
return;
|
|
}
|
|
|
|
const groupedPrintIds = groupPrintIdsByWarehouse(printRecords);
|
|
for (const [ warehouseId, printIds ] of groupedPrintIds.entries()) {
|
|
if (!Array.isArray(printIds) || printIds.length === 0) {
|
|
continue;
|
|
}
|
|
|
|
invokeCount += 1;
|
|
try {
|
|
const retryResult = await ctx.service.systemMethods.print.retryFailed({
|
|
print_ids: printIds,
|
|
warehouse_id: warehouseId,
|
|
force: compensationConfig.force,
|
|
});
|
|
|
|
if (!retryResult || retryResult.flag !== 'success') {
|
|
ctx.logger.warn(`[${taskName}] 触发补打失败`, {
|
|
warehouseId,
|
|
printIdsCount: printIds.length,
|
|
message: retryResult && retryResult.message ? retryResult.message : '未知错误',
|
|
});
|
|
continue;
|
|
}
|
|
|
|
retriedSuccessCount += Number(retryResult.success_count || 0);
|
|
retriedFailedCount += Number(retryResult.failed_count || 0);
|
|
skippedCount += Number(retryResult.skipped_count || 0);
|
|
} catch (error) {
|
|
ctx.logger.error(`[${taskName}] 触发补打异常`, {
|
|
warehouseId,
|
|
printIdsCount: printIds.length,
|
|
error: error && error.message ? error.message : `${error}`,
|
|
});
|
|
}
|
|
}
|
|
|
|
ctx.logger.info(`[${taskName}] 任务执行完成`, {
|
|
scannedCount,
|
|
warehouseGroupCount: groupedPrintIds.size,
|
|
invokeCount,
|
|
retriedSuccessCount,
|
|
retriedFailedCount,
|
|
skippedCount,
|
|
});
|
|
} catch (error) {
|
|
ctx.logger.error(`[${taskName}] 执行失败`, {
|
|
error: error && error.message ? error.message : `${error}`,
|
|
});
|
|
} finally {
|
|
ctx.app._printRetryCompensationRunning = false;
|
|
}
|
|
},
|
|
};
|