WMS_MAIN/app/schedule/consume_qimen_logs.js
liufumingren e73a311076 ok
2026-04-11 17:09:06 +08:00

62 lines
1.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';
const path = require('path');
/**
* 定时消费奇门日志文件队列,逐条插入数据库。
* 间隔 30 秒执行,type: worker(仅一个 worker 执行)。
* 使用 fileWriter.consumeFiles 内置的逐条进度追踪,
* 任一记录插入失败即停止,下次从失败位置重试。
*/
module.exports = {
schedule: {
interval: '30s',
type: 'worker',
immediate: false,
disable: false,
},
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 => {
await mysql.insert('wms_qimen_log', {
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,
});
},
});
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;
}
},
};