WMS_MAIN/script/tmp-watch-report-delivery.cjs
liufumingren c3f1c67592 chore(script): 沉淀波次池/报表/库存/订单本地排查脚本
新增一批一次性排查脚本(script/tmp-*.cjs),与仓库既有 script/tmp- 约定保持一致,
覆盖波次池边界与优先级核对、库位/出库报表核对、订单状态与更新范围核验等场景
2026-09-13 15:31:27 +08:00

101 lines
4.1 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';
/**
* 临时观察脚本:轮询开发库投递记录,等待指定时间点的报表投递完成并打印结果。
*
* 作用:设置新投递时间点后无人值守地等结果(到点自动投递 → 打印 status/文件名/下载链接/错误)。
* 只读(仅 SELECT),不修改任何数据。
*
* 用法:node script/tmp-watch-report-delivery.cjs [timePoint] [timeoutSeconds]
* 默认 timePoint=20:25、timeout=900 秒(15 分钟)
*/
const path = require('node:path');
const { createRequire } = require('node:module');
const projectRoot = path.resolve(__dirname, '..');
const projectRequire = createRequire(path.join(projectRoot, 'package.json'));
const mysql = projectRequire('mysql2/promise');
const TIME_POINT = process.argv[2] || '20:25';
const TIMEOUT_SECONDS = Number(process.argv[3] || 900);
const TARGET_DEFINITION_IDS = [ 1, 3 ];
const POLL_INTERVAL_MS = 10000;
/**
* 读取开发库连接配置。
* @return {Object} mysql 连接参数
*/
function loadDevDbConfig() {
const factory = require(path.join(projectRoot, 'config', 'config.local.js'));
const config = typeof factory === 'function' ? factory({ name: 'tmp-watch-delivery', baseDir: projectRoot }) : factory;
const client = config && config.mysql && config.mysql.clients && config.mysql.clients.wms;
if (!client || !client.host) {
throw new Error('无法解析开发库配置 config.local.js(mysql.clients.wms)');
}
return {
host: client.host,
port: Number(client.port),
user: client.user,
password: client.password,
database: client.database,
charset: client.charset || 'utf8mb4',
};
}
(async () => {
const connection = await mysql.createConnection(loadDevDbConfig());
const deadline = Date.now() + TIMEOUT_SECONDS * 1000;
console.log(`[watch] 等待 time_point=${TIME_POINT} 的投递(定义 ${TARGET_DEFINITION_IDS.join('/')}),超时 ${TIMEOUT_SECONDS}s`);
try {
for (;;) {
const [ rows ] = await connection.query(
`SELECT id, store_id, report_definition_id, report_name, biz_date, time_point, status,
retry_count, error_message, delivered_at, file_url
FROM wms_report_delivery_log
WHERE time_point = ?
AND report_definition_id IN (${TARGET_DEFINITION_IDS.map(() => '?').join(',')})
ORDER BY report_definition_id`,
[ TIME_POINT, ...TARGET_DEFINITION_IDS ]
);
const list = Array.isArray(rows) ? rows : [];
const pending = list.filter(row => [ 'pending', 'exporting' ].includes(String(row.status)));
const terminal = list.filter(row => [ 'delivered', 'failed', 'cancelled' ].includes(String(row.status)));
if (list.length > 0) {
console.log(`[watch] ${new Date().toLocaleTimeString('zh-CN', { hour12: false })} 记录 ${list.length} 条(终态 ${terminal.length})`);
}
if (terminal.length >= TARGET_DEFINITION_IDS.length || (list.length > 0 && pending.length === 0)) {
for (const row of list) {
console.log(
`\n[结果] 定义=${row.report_definition_id}(${row.report_name})`
+ `\n status=${row.status} biz_date=${row.biz_date} time_point=${row.time_point}`
+ `\n retry_count=${row.retry_count}`
+ `\n delivered_at=${row.delivered_at || '(空)'}`
+ `\n 文件=${row.file_url || '(空)'}`
+ (row.error_message ? `\n 错误=${row.error_message}` : '')
);
}
process.exit(0);
}
if (Date.now() >= deadline) {
console.log(`\n[watch] 等待超时(${TIMEOUT_SECONDS}s),当前命中 ${list.length} 条记录`);
for (const row of list) {
console.log(` 定义=${row.report_definition_id} status=${row.status} 文件=${row.file_url || '(空)'} 错误=${row.error_message || '(空)'}`);
}
process.exit(list.length > 0 ? 0 : 1);
}
await new Promise(resolve => setTimeout(resolve, POLL_INTERVAL_MS));
}
} finally {
await connection.end();
}
})().catch(error => {
console.error('[watch] 执行异常:', error && error.message ? error.message : error);
process.exit(1);
});