WMS_MAIN/app/schedule/consume_qimen_logs.js
liufumingren cf3b9092c6 fix(qimen): 出站请求日志使用独立 log_id 并幂等消费日志队列
- qimen_client.sendRequest 出站日志改为 generateLogId() 生成 QL-<时间戳>-<随机>,
  不再复用入站 ctx.qimenLogId,避免与入站文件队列落库撞 wms_qimen_log.log_id 唯一索引
- qimen.saveQimenLog 去掉 ctx.qimenLogId 兜底;saveQimenLogResult / saveQimenLogBody
  改为按 log_id 定位更新(原先按 id 字段更新与调用语义不符)
- schedule/consume_qimen_logs 插入遇 ER_DUP_ENTRY 视为幂等命中跳过并推进消费进度,
  避免重复日志卡死文件队列
- test: qimen_query.test.js 断言改为校验新 log_id 格式
2026-09-13 15:29:43 +08:00

78 lines
2.6 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';
const path = require('path');
/**
* 定时消费奇门日志文件队列,逐条幂等插入数据库。
* 间隔 30 秒执行,type: worker(仅一个 worker 执行)。
* 使用 fileWriter.consumeFiles 内置的逐条进度追踪,
* 任一记录插入失败即停止,下次从失败位置重试。
* 同一 log_id 已落库时视为幂等命中直接跳过,避免唯一索引冲突卡死队列。
*/
module.exports = {
schedule: {
interval: '30s',
type: 'worker',
immediate: false,
disable: false,
},
/**
* 消费奇门日志队列任务入口。
* @param {Egg.Context} ctx Egg 上下文。
* @return {Promise<void>} 无返回值。
*/
async task(ctx) {
// 防止重入
if (ctx.app._consumeQimenLogsRunning) return;
ctx.app._consumeQimenLogsRunning = true;
const fileWriter = ctx.app.fileWriter;
const mysql = ctx.app.mysql.get('wms');
const progressFilePath = path.join(fileWriter.baseDir, 'qimen-logs', '.consume-progress');
try {
const result = await fileWriter.consumeFiles({
businessName: 'qimen-logs',
progressFilePath,
maxConsumeRecordsPerWindow: 500,
parseJson: true,
onRecord: async record => {
const row = {
log_id: record.log_id || null,
from_key: record.from_key || '',
to_key: record.to_key || '',
method: record.method || '',
sign: record.sign || '',
costomer_id: record.costomer_id || '',
v: record.v || '',
url: record.url || '',
created_at: record.created_at ? new Date(record.created_at) : new Date(),
body: record.body || '',
result: record.result || '',
use_time: record.use_time || 0,
};
try {
await mysql.insert('wms_qimen_log', row);
} catch (err) {
const isDuplicate = !!err && (err.code === 'ER_DUP_ENTRY' || err.errno === 1062);
if (!isDuplicate) {
throw err;
}
// 同一 log_id 已落库时视为幂等命中,跳过并继续推进消费进度
ctx.logger.warn(`[consume-qimen-logs] 跳过重复日志 log_id=${row.log_id}`);
}
},
});
if (result.processedCount > 0) {
ctx.logger.info(`[consume-qimen-logs] 消费 ${result.processedCount} 条日志`);
}
} catch (err) {
ctx.logger.error(`[consume-qimen-logs] 消费失败: ${err.message}`);
} finally {
ctx.app._consumeQimenLogsRunning = false;
}
},
};