Compare commits
7 Commits
yanhui-wle
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 2c49df3ff7 | |||
| 2defbe9192 | |||
| 9924c8d1c9 | |||
| 936d391142 | |||
| 2f44cdce82 | |||
| 3062182417 | |||
| 6c4f36f9fd |
@ -76,6 +76,24 @@ function isTransientError(error) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ⚠️ `options.timeoutMs` 是个「看着能用、其实没人读」的参数 —— 历史踩坑,留个告警别再犯。
|
||||||
|
*
|
||||||
|
* 根因:mssql 12.x 上请求级 `request.timeout` 不生效,超时只能靠**池配置级** requestTimeout,
|
||||||
|
* 而共享池(query())的池级超时在 pool 创建时就定死了,调用方改不了。想放宽只有两条路:
|
||||||
|
* · querySlow(sql, params, { connectRequestTimeoutMs }) —— 临时独立连接,可任意放宽;
|
||||||
|
* · query(sql, params, { requestTimeout }) —— 只对独立连接路径(queryWithFreshConnection)有意义。
|
||||||
|
* 2026-11-14 实况:WLE 服务层两处 `{ timeoutMs: 240000 }` 想着「给足 4 分钟」,
|
||||||
|
* 实际一路按 60s/300s 跑,11-13 那轮钉钉推送就因此超时没推出去。
|
||||||
|
*/
|
||||||
|
function warnLegacyTimeoutOption(options, caller) {
|
||||||
|
if (!options || options.timeoutMs === undefined) return;
|
||||||
|
console.warn(
|
||||||
|
`[CloudDB] ${caller} 收到无效参数 timeoutMs=${options.timeoutMs}(已被忽略,不会放宽超时);`
|
||||||
|
+ '需要更长超时请改用 querySlow + connectRequestTimeoutMs'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function isTruthy(value) {
|
function isTruthy(value) {
|
||||||
return value === true || value === 1 || String(value || '').toLowerCase() === 'true' || String(value || '') === '1';
|
return value === true || value === 1 || String(value || '').toLowerCase() === 'true' || String(value || '') === '1';
|
||||||
}
|
}
|
||||||
@ -1036,6 +1054,16 @@ const resetPool = async () => {
|
|||||||
* @param {object} params - 命名参数对象
|
* @param {object} params - 命名参数对象
|
||||||
*/
|
*/
|
||||||
const query = async (sqlText, params = {}, options = {}) => {
|
const query = async (sqlText, params = {}, options = {}) => {
|
||||||
|
warnLegacyTimeoutOption(options, 'query()');
|
||||||
|
// 🔥 2026-11-17:`requestTimeout` 只写 request.timeout(**请求级**),mssql 12.x 上不生效,
|
||||||
|
// 实际仍按共享池创建时定死的池级 requestTimeout(默认 60s)掐断 —— MKH-BOM 的订单大表
|
||||||
|
// 查询就是因此「代码里写 180s、实际 60s 必超时并降级」,页面待发订单量长期为 0。
|
||||||
|
// 需要真正放宽超时的调用方,显式传 `freshConnection: true` 走临时独立连接
|
||||||
|
// (池级超时随 connectRequestTimeoutMs 任意放大),比让调用方各自调用 querySlow 更好维护,
|
||||||
|
// 也让单测只需 mock query() 一个入口。
|
||||||
|
if (options && options.freshConnection === true) {
|
||||||
|
return queryWithFreshConnection(sqlText, params, options);
|
||||||
|
}
|
||||||
const maxAttempts = parsePositiveInt(options.maxAttempts, 2);
|
const maxAttempts = parsePositiveInt(options.maxAttempts, 2);
|
||||||
|
|
||||||
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
||||||
@ -1072,6 +1100,7 @@ const query = async (sqlText, params = {}, options = {}) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const queryWithFreshConnection = async (sqlText, params = {}, options = {}) => {
|
const queryWithFreshConnection = async (sqlText, params = {}, options = {}) => {
|
||||||
|
warnLegacyTimeoutOption(options, 'queryWithFreshConnection()');
|
||||||
const maxAttempts = parsePositiveInt(options.maxAttempts, 2);
|
const maxAttempts = parsePositiveInt(options.maxAttempts, 2);
|
||||||
|
|
||||||
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
||||||
|
|||||||
@ -69,6 +69,9 @@ const {
|
|||||||
const {
|
const {
|
||||||
startMkhReplenishAlertScheduler,
|
startMkhReplenishAlertScheduler,
|
||||||
} = require('./jobs/mkhReplenishAlertJob');
|
} = require('./jobs/mkhReplenishAlertJob');
|
||||||
|
const {
|
||||||
|
startMkhBomPrewarmScheduler,
|
||||||
|
} = require('./jobs/mkhBomPrewarmJob');
|
||||||
const {
|
const {
|
||||||
startWarehouseLogisticsExceptionNotifyScheduler,
|
startWarehouseLogisticsExceptionNotifyScheduler,
|
||||||
} = require('./jobs/warehouseLogisticsExceptionNotifyJob');
|
} = require('./jobs/warehouseLogisticsExceptionNotifyJob');
|
||||||
@ -800,6 +803,9 @@ async function startServer(options = {}) {
|
|||||||
startGoodsFirstSeenScheduler();
|
startGoodsFirstSeenScheduler();
|
||||||
startAlibaba1688ImageSearchExpiryScheduler();
|
startAlibaba1688ImageSearchExpiryScheduler();
|
||||||
startMkhReplenishAlertScheduler();
|
startMkhReplenishAlertScheduler();
|
||||||
|
// MKH-BOM 页面索引预热:订单两表全表扫很贵(冷查分钟级),必须由后台保持热态,
|
||||||
|
// 不能让「打开页面 / F5」的用户去等(见 jobs/mkhBomPrewarmJob.js 文件头)
|
||||||
|
startMkhBomPrewarmScheduler();
|
||||||
startWarehouseLogisticsExceptionNotifyScheduler();
|
startWarehouseLogisticsExceptionNotifyScheduler();
|
||||||
startShipTimelinessNotifyScheduler();
|
startShipTimelinessNotifyScheduler();
|
||||||
startDailyInventorySnapshotScheduler();
|
startDailyInventorySnapshotScheduler();
|
||||||
|
|||||||
90
backend/src/jobs/mkhBomPrewarmJob.js
Normal file
90
backend/src/jobs/mkhBomPrewarmJob.js
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MKH-BOM 页面索引后台预热。
|
||||||
|
*
|
||||||
|
* 背景(2026-11-17 排查结论):
|
||||||
|
* 这个页面除了「订单两表」之外,全部数据都在内存里算。而订单查询是
|
||||||
|
* `skus LIKE '%MKH%'` 前导通配 → 必然全表扫;副本库又慢到
|
||||||
|
* `rpt.jst_Orders` 158 万行**光 COUNT(*)** 要 152.8s(≈1 万行/秒)。
|
||||||
|
* 于是「谁把缓存打穿,谁就等 2~8 分钟」。
|
||||||
|
*
|
||||||
|
* 旧行为之所以每次打开都慢:
|
||||||
|
* 前端「首屏 + 每 2 分钟」都带 `fresh=1` → 后端清运营缓存 → 必冷查。
|
||||||
|
*
|
||||||
|
* 现在的分工:
|
||||||
|
* · 请求路径只读缓存(过期了也先返回旧值,见 loadOrderIndex 的 stale-while-revalidate);
|
||||||
|
* · 只有「同步最新数据」按钮会清缓存强制重读;
|
||||||
|
* · **索引新鲜度由这个任务负责**:启动 20 秒后跑一次,之后每 20 分钟跑一次,
|
||||||
|
* 始终赶在 30 分钟 TTL 到期之前把订单索引换掉。
|
||||||
|
*
|
||||||
|
* 配置(都不用发版):
|
||||||
|
* · MKH_BOM_PREWARM_INTERVAL_MINUTES 预热间隔,默认 20
|
||||||
|
* · MKH_BOM_PREWARM_DISABLED=true 关闭(排查 / 本地开发时用)
|
||||||
|
*/
|
||||||
|
|
||||||
|
const cron = require('node-cron');
|
||||||
|
const { prewarmMkhBomIndex } = require('../services/mkhBomService');
|
||||||
|
|
||||||
|
const TIMEZONE = 'Asia/Shanghai';
|
||||||
|
const DEFAULT_INTERVAL_MINUTES = 20;
|
||||||
|
// 启动后等一会儿再跑:别和数据库连接自检、其它启动预热抢业务库
|
||||||
|
const STARTUP_DELAY_MS = 20 * 1000;
|
||||||
|
|
||||||
|
// 单飞:上一轮没跑完(冷查可能好几分钟)就不叠加下一轮
|
||||||
|
let running = false;
|
||||||
|
|
||||||
|
async function runMkhBomPrewarm(source = 'scheduled') {
|
||||||
|
if (running) return { skipped: true, reason: 'running' };
|
||||||
|
running = true;
|
||||||
|
try {
|
||||||
|
const result = await prewarmMkhBomIndex();
|
||||||
|
console.log(
|
||||||
|
`[MkhBomPrewarm] ${source} 完成,耗时 ${Math.round(result.elapsedMs / 1000)}s (${result.steps.join(', ')})`
|
||||||
|
);
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
// 预热失败不影响功能:下一次请求会自己撞冷查(只是慢一点)
|
||||||
|
console.warn(`[MkhBomPrewarm] ${source} 失败:`, error.message || error);
|
||||||
|
return { error: error.message || String(error) };
|
||||||
|
} finally {
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveIntervalMinutes() {
|
||||||
|
const raw = Number.parseInt(process.env.MKH_BOM_PREWARM_INTERVAL_MINUTES, 10);
|
||||||
|
if (!Number.isFinite(raw) || raw < 5 || raw > 59) return DEFAULT_INTERVAL_MINUTES;
|
||||||
|
return raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
function startMkhBomPrewarmScheduler() {
|
||||||
|
const disabled = String(process.env.MKH_BOM_PREWARM_DISABLED || '').trim().toLowerCase() === 'true';
|
||||||
|
if (disabled) {
|
||||||
|
console.log('[Scheduler] MKH-BOM 索引预热已禁用');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const minutes = resolveIntervalMinutes();
|
||||||
|
// node-cron 的 */n 在 n 不整除 60 时会在跨小时处多跑一次,20 分钟整除,没问题
|
||||||
|
const expression = `*/${minutes} * * * *`;
|
||||||
|
cron.schedule(expression, () => {
|
||||||
|
runMkhBomPrewarm('interval').catch(() => {});
|
||||||
|
}, { timezone: TIMEZONE });
|
||||||
|
|
||||||
|
const startupTimer = setTimeout(() => {
|
||||||
|
runMkhBomPrewarm('startup').catch(() => {});
|
||||||
|
}, STARTUP_DELAY_MS);
|
||||||
|
if (typeof startupTimer.unref === 'function') startupTimer.unref();
|
||||||
|
|
||||||
|
console.log(`[Scheduler] MKH-BOM 索引预热已注册: 启动后 ${STARTUP_DELAY_MS / 1000}s 跑一次,之后 ${expression} (${TIMEZONE})`);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
DEFAULT_INTERVAL_MINUTES,
|
||||||
|
STARTUP_DELAY_MS,
|
||||||
|
TIMEZONE,
|
||||||
|
resolveIntervalMinutes,
|
||||||
|
runMkhBomPrewarm,
|
||||||
|
startMkhBomPrewarmScheduler,
|
||||||
|
};
|
||||||
@ -6,6 +6,8 @@
|
|||||||
* 业务口径(2026-10-22 定稿):
|
* 业务口径(2026-10-22 定稿):
|
||||||
* ① 每天 09:10(核验 09:00 之后)把**当天核验批次**的异常情况发到指定钉钉群;
|
* ① 每天 09:10(核验 09:00 之后)把**当天核验批次**的异常情况发到指定钉钉群;
|
||||||
* **0 条也推**(群里看到「本批 0 异常」= 任务没挂)。
|
* **0 条也推**(群里看到「本批 0 异常」= 任务没挂)。
|
||||||
|
* ⚠️ 失败必须重试(2026-11-11 加):09:10 起 30 分钟内每 5 分钟再试一次,成功即停;
|
||||||
|
* 窗口过了不再补推(上午的数据下午才发会误导业务)。见 `resolveScheduledPushDecision`。
|
||||||
* ② 不发 .xlsx 附件 —— 群机器人 webhook 发不了文件,业务也确认群里看文本就够。
|
* ② 不发 .xlsx 附件 —— 群机器人 webhook 发不了文件,业务也确认群里看文本就够。
|
||||||
* 内容 = 汇总(发货/异常/异常率/分类型/分仓)+ 前 N 条明细 + 页面链接。
|
* 内容 = 汇总(发货/异常/异常率/分类型/分仓)+ 前 N 条明细 + 页面链接。
|
||||||
* N 由页面配置(默认 30,上限 50);要全部明细就点链接去页面导出 Excel。
|
* N 由页面配置(默认 30,上限 50);要全部明细就点链接去页面导出 Excel。
|
||||||
@ -45,6 +47,32 @@ const DEFAULT_TICK_CRON = '* * * * *';
|
|||||||
// 钉钉 text 上限 20000 字节;留 300 字节给「已精简」尾巴,避免临界失败
|
// 钉钉 text 上限 20000 字节;留 300 字节给「已精简」尾巴,避免临界失败
|
||||||
const SAFE_CONTENT_BYTES = MAX_CONTENT_BYTES - 300;
|
const SAFE_CONTENT_BYTES = MAX_CONTENT_BYTES - 300;
|
||||||
|
|
||||||
|
// 🔥 2026-11-11 加:推送失败要重试,不能「一天只有一次机会」。
|
||||||
|
// 实况:11-11 09:10 那次撞上业务库连不上(状态里 `Failed to connect to 218.93.9.21:1433`),
|
||||||
|
// 老逻辑只在 `HH:mm === sendTime` 这一分钟内跑一次,失败后当天彻底不推 —— 群里一整天没消息。
|
||||||
|
// 现在改成:从推送时刻起 `RETRY_WINDOW_MINUTES` 分钟内,每隔 `RETRY_INTERVAL_MINUTES` 分钟再试,
|
||||||
|
// 成功即停(`lastSentDate` 落库后后续 tick 自动 skip),窗口过了就不再补(避免上午的数据下午才发)。
|
||||||
|
// 两个值都可用环境变量覆盖,现网想调不用改前端配置、也不用发版。
|
||||||
|
const DEFAULT_RETRY_WINDOW_MINUTES = 30;
|
||||||
|
const DEFAULT_RETRY_INTERVAL_MINUTES = 5;
|
||||||
|
|
||||||
|
function readPositiveMinutes(value, fallback, max) {
|
||||||
|
const num = Number(value);
|
||||||
|
if (!Number.isFinite(num) || num <= 0) return fallback;
|
||||||
|
return Math.min(Math.round(num), max);
|
||||||
|
}
|
||||||
|
|
||||||
|
const RETRY_WINDOW_MINUTES = readPositiveMinutes(
|
||||||
|
process.env.WLE_NOTIFY_RETRY_WINDOW_MINUTES,
|
||||||
|
DEFAULT_RETRY_WINDOW_MINUTES,
|
||||||
|
12 * 60
|
||||||
|
);
|
||||||
|
const RETRY_INTERVAL_MINUTES = readPositiveMinutes(
|
||||||
|
process.env.WLE_NOTIFY_RETRY_INTERVAL_MINUTES,
|
||||||
|
DEFAULT_RETRY_INTERVAL_MINUTES,
|
||||||
|
RETRY_WINDOW_MINUTES
|
||||||
|
);
|
||||||
|
|
||||||
let tickRunning = false;
|
let tickRunning = false;
|
||||||
|
|
||||||
function pad2(value) {
|
function pad2(value) {
|
||||||
@ -75,6 +103,66 @@ function chinaHourMinute(date = new Date()) {
|
|||||||
}).format(base);
|
}).format(base);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 'HH:mm' → 当天第几分钟(09:10 → 550)。非法输入返回 null,调用方按「不跑」处理 */
|
||||||
|
function minuteOfDay(hhmm) {
|
||||||
|
const match = /^([01]\d|2[0-3]):([0-5]\d)$/.exec(String(hhmm || '').trim());
|
||||||
|
if (!match) return null;
|
||||||
|
return Number(match[1]) * 60 + Number(match[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定时器每一分钟问一次「现在该不该推」。纯函数,便于把时间窗口/节流的边界钉死在单测里。
|
||||||
|
*
|
||||||
|
* 返回 `{ run, reason, pushDate, attempt }`:
|
||||||
|
* · `already_sent_today` 今天已推成功 → 不再动(重试自然也停)
|
||||||
|
* · `before_time` 还没到配置的推送时刻
|
||||||
|
* · `window_closed` 已过「推送时刻 + 重试窗口」,不再补推(避免 10 点失败、下午才发出去)
|
||||||
|
* · `wait_interval` 上一轮失败距现在不到重试间隔,等着
|
||||||
|
* · `first_round` 到点第一次跑
|
||||||
|
* · `retry` 上一轮失败后的第 N 次重试
|
||||||
|
*
|
||||||
|
* ⚠️ 窗口**不跨日**:`sendTime + window` 超过 23:59 就截到当天最后一分钟。
|
||||||
|
* 跨日会让 `pushDate` 换天,去重键(`lastSentDate`)跟着变,容易出现同一天推两遍。
|
||||||
|
*/
|
||||||
|
function resolveScheduledPushDecision(options = {}) {
|
||||||
|
const now = options.now instanceof Date ? options.now : new Date(options.now ?? Date.now());
|
||||||
|
const pushDate = chinaDateKey(now);
|
||||||
|
const state = options.state && typeof options.state === 'object' ? options.state : {};
|
||||||
|
const windowMinutes = readPositiveMinutes(options.retryWindowMinutes, RETRY_WINDOW_MINUTES, 12 * 60);
|
||||||
|
const intervalMinutes = readPositiveMinutes(options.retryIntervalMinutes, RETRY_INTERVAL_MINUTES, windowMinutes);
|
||||||
|
|
||||||
|
if (state.lastSentDate === pushDate) {
|
||||||
|
return { run: false, reason: 'already_sent_today', pushDate, attempt: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
const nowMinute = minuteOfDay(chinaHourMinute(now));
|
||||||
|
const startMinute = minuteOfDay(options.sendTime);
|
||||||
|
if (nowMinute === null || startMinute === null) {
|
||||||
|
return { run: false, reason: 'bad_send_time', pushDate, attempt: 0 };
|
||||||
|
}
|
||||||
|
if (nowMinute < startMinute) {
|
||||||
|
return { run: false, reason: 'before_time', pushDate, attempt: 0 };
|
||||||
|
}
|
||||||
|
const endMinute = Math.min(startMinute + windowMinutes, 23 * 60 + 59);
|
||||||
|
if (nowMinute > endMinute) {
|
||||||
|
return { run: false, reason: 'window_closed', pushDate, attempt: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
// 当天的尝试次数:跨天或没有记录都归零,避免沿用昨天的计数把节流算歪
|
||||||
|
const attempt = state.attemptDate === pushDate ? Number(state.attemptCount) || 0 : 0;
|
||||||
|
if (attempt > 0) {
|
||||||
|
const lastAttemptMs = Date.parse(String(state.lastAttemptAt || ''));
|
||||||
|
if (Number.isFinite(lastAttemptMs)) {
|
||||||
|
const gapMinutes = (now.getTime() - lastAttemptMs) / 60000;
|
||||||
|
if (gapMinutes < intervalMinutes) {
|
||||||
|
return { run: false, reason: 'wait_interval', pushDate, attempt };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { run: true, reason: attempt > 0 ? 'retry' : 'first_round', pushDate, attempt: attempt + 1 };
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字段清洗:换行/连续空白压成单空格。
|
* 字段清洗:换行/连续空白压成单空格。
|
||||||
* ⚠️ 物流描述里常有 `\n`,直接拼进 text 消息会把一条明细劈成两行,看着像两条异常。
|
* ⚠️ 物流描述里常有 `\n`,直接拼进 text 消息会把一条明细劈成两行,看着像两条异常。
|
||||||
@ -532,8 +620,9 @@ async function runWarehouseLogisticsExceptionNotifyJob(source = 'scheduled', opt
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调度:每分钟比对「北京时间 HH:mm == 配置的 sendTime」。
|
* 调度:每分钟问一次 `resolveScheduledPushDecision`。
|
||||||
* 之所以不用固定 cron 表达式:业务在页面上改时间后要立即生效,不能等后端重启。
|
* 之所以不用固定 cron 表达式:业务在页面上改时间后要立即生效,不能等后端重启。
|
||||||
|
* 之所以不是「HH:mm 严格相等」:那样只给一次机会,09:10 撞上业务库连不上就整天不推(11-11 实况)。
|
||||||
*/
|
*/
|
||||||
function startWarehouseLogisticsExceptionNotifyScheduler() {
|
function startWarehouseLogisticsExceptionNotifyScheduler() {
|
||||||
const disabled = String(process.env.DISABLE_WLE_NOTIFY || '').trim().toLowerCase() === 'true';
|
const disabled = String(process.env.DISABLE_WLE_NOTIFY || '').trim().toLowerCase() === 'true';
|
||||||
@ -542,6 +631,7 @@ function startWarehouseLogisticsExceptionNotifyScheduler() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const expression = String(process.env.WLE_NOTIFY_TICK_CRON || DEFAULT_TICK_CRON).trim();
|
const expression = String(process.env.WLE_NOTIFY_TICK_CRON || DEFAULT_TICK_CRON).trim();
|
||||||
|
let windowClosedLoggedDate = '';
|
||||||
cron.schedule(expression, () => {
|
cron.schedule(expression, () => {
|
||||||
if (tickRunning) return;
|
if (tickRunning) return;
|
||||||
tickRunning = true;
|
tickRunning = true;
|
||||||
@ -550,8 +640,32 @@ function startWarehouseLogisticsExceptionNotifyScheduler() {
|
|||||||
const now = new Date();
|
const now = new Date();
|
||||||
const config = await getWarehouseLogisticsExceptionNotifyRuntimeConfig();
|
const config = await getWarehouseLogisticsExceptionNotifyRuntimeConfig();
|
||||||
if (!config.enabled) return;
|
if (!config.enabled) return;
|
||||||
if (chinaHourMinute(now) !== config.sendTime) return;
|
const state = await loadWarehouseLogisticsExceptionNotifyState();
|
||||||
await runWarehouseLogisticsExceptionNotifyJob('daily_schedule', { now });
|
const decision = resolveScheduledPushDecision({ now, sendTime: config.sendTime, state });
|
||||||
|
if (!decision.run) {
|
||||||
|
// 窗口刚过、当天又一直没推成功 → 打一条,明确「今天不会自己来了」,别让人一直等
|
||||||
|
if (decision.reason === 'window_closed' && windowClosedLoggedDate !== decision.pushDate) {
|
||||||
|
windowClosedLoggedDate = decision.pushDate;
|
||||||
|
console.error(
|
||||||
|
`[WleNotify] 今日推送窗口已关闭(${config.sendTime} 起 ${RETRY_WINDOW_MINUTES} 分钟内重试,`
|
||||||
|
+ '始终未成功),今天不再自动重试,请人工处理'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先落「本轮尝试」:失败后下一分钟的 tick 才能按间隔节流。
|
||||||
|
// 成功时 push 内部会覆盖 lastSentDate(去重键),此后本函数直接走 already_sent_today。
|
||||||
|
await saveWarehouseLogisticsExceptionNotifyState({
|
||||||
|
...state,
|
||||||
|
attemptDate: decision.pushDate,
|
||||||
|
attemptCount: decision.attempt,
|
||||||
|
lastAttemptAt: now.toISOString(),
|
||||||
|
});
|
||||||
|
await runWarehouseLogisticsExceptionNotifyJob(
|
||||||
|
decision.reason === 'retry' ? 'daily_retry' : 'daily_schedule',
|
||||||
|
{ now }
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[WleNotify] 定时检查失败:', error.message || error);
|
console.error('[WleNotify] 定时检查失败:', error.message || error);
|
||||||
} finally {
|
} finally {
|
||||||
@ -560,10 +674,15 @@ function startWarehouseLogisticsExceptionNotifyScheduler() {
|
|||||||
})();
|
})();
|
||||||
}, { timezone: TIMEZONE });
|
}, { timezone: TIMEZONE });
|
||||||
|
|
||||||
console.log(`[Scheduler] 物流转运异常钉钉推送已注册: 每分钟检查,到点(默认 09:10)推送 (${TIMEZONE})`);
|
console.log(
|
||||||
|
`[Scheduler] 物流转运异常钉钉推送已注册: 每分钟检查,到点(默认 09:10)推送,`
|
||||||
|
+ `失败后每 ${RETRY_INTERVAL_MINUTES} 分钟重试、最多 ${RETRY_WINDOW_MINUTES} 分钟内 (${TIMEZONE})`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
DEFAULT_RETRY_INTERVAL_MINUTES,
|
||||||
|
DEFAULT_RETRY_WINDOW_MINUTES,
|
||||||
DEFAULT_TICK_CRON,
|
DEFAULT_TICK_CRON,
|
||||||
SAFE_CONTENT_BYTES,
|
SAFE_CONTENT_BYTES,
|
||||||
TIMEZONE,
|
TIMEZONE,
|
||||||
@ -575,9 +694,11 @@ module.exports = {
|
|||||||
chinaDateKey,
|
chinaDateKey,
|
||||||
chinaHourMinute,
|
chinaHourMinute,
|
||||||
groupRowsByWarehouse,
|
groupRowsByWarehouse,
|
||||||
|
minuteOfDay,
|
||||||
pickDetailRows,
|
pickDetailRows,
|
||||||
cleanCell,
|
cleanCell,
|
||||||
pushWarehouseLogisticsExceptionNotify,
|
pushWarehouseLogisticsExceptionNotify,
|
||||||
|
resolveScheduledPushDecision,
|
||||||
runWarehouseLogisticsExceptionNotifyJob,
|
runWarehouseLogisticsExceptionNotifyJob,
|
||||||
startWarehouseLogisticsExceptionNotifyScheduler,
|
startWarehouseLogisticsExceptionNotifyScheduler,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -501,10 +501,40 @@ const ORDER_TABLES = Object.freeze([
|
|||||||
]);
|
]);
|
||||||
// 不计入需求的订单状态:Cancelled 已取消 / Merged 已合并进别的单
|
// 不计入需求的订单状态:Cancelled 已取消 / Merged 已合并进别的单
|
||||||
const PENDING_ORDER_EXCLUDED_STATUS = Object.freeze(['Cancelled', 'Merged']);
|
const PENDING_ORDER_EXCLUDED_STATUS = Object.freeze(['Cancelled', 'Merged']);
|
||||||
// 只捞含 MKH 的订单,避免全表扫描(实测 1.18M 行 + 4.91M 行,合并查询约 1.25s)
|
// 只捞含 MKH 的订单,避免把结果集全拉回来。
|
||||||
|
// ⚠️ 注意:这是**前导通配**(`LIKE '%MKH%'`),索引完全用不上 → SQL Server 必然全表扫。
|
||||||
|
// 2026-11-17 实测副本库 `rpt.jst_Orders` 158 万行光 COUNT(*) 就要 152.8s(≈1 万行/秒),
|
||||||
|
// 所以这条查询必须走独立连接 + 长超时,并且不能放在用户请求路径上(见 loadOrderIndex)。
|
||||||
const PENDING_ORDER_SKU_LIKE = '%MKH%';
|
const PENDING_ORDER_SKU_LIKE = '%MKH%';
|
||||||
// 订单数据源异常时的降级缓存时长
|
// 订单数据源异常时的降级缓存时长。
|
||||||
const PENDING_ORDER_DEGRADED_TTL_MS = 60 * 1000;
|
// 🔥 2026-11-17:60 秒 → 5 分钟。降级态下每次重试都是一次**全表扫描**(实测单次 60~130s,
|
||||||
|
// 慢的时候 400s+),60 秒 TTL 等于「刚失败就重打」,库越慢越打、越打越慢 —— 自残循环。
|
||||||
|
// 5 分钟仍然够快恢复,但不会把本来就慢的副本库打穿。
|
||||||
|
const PENDING_ORDER_DEGRADED_TTL_MS = 5 * 60 * 1000;
|
||||||
|
// 订单查询超时(毫秒)。**必须走独立连接**(`freshConnection: true`):
|
||||||
|
// 共享池的池级超时在 pool 创建时就定死了(默认 60s),`requestTimeout` 只是请求级设置、
|
||||||
|
// 在 mssql 12.x 上不生效。2026-11-17 实测:rpt.jst_Orders 158 万行**光 COUNT(*)** 就要
|
||||||
|
// 152.8s(≈1 万行/秒),而 `skus LIKE '%MKH%'` 前导通配必然全表扫 → 60s 无解。
|
||||||
|
// 可用 CANGKU_MKH_ORDER_QUERY_TIMEOUT_MS 覆盖(默认 5 分钟)。
|
||||||
|
const ORDER_QUERY_TIMEOUT_MS = Number(process.env.CANGKU_MKH_ORDER_QUERY_TIMEOUT_MS) || 300000;
|
||||||
|
// 不重连重试:这类失败是「库慢」而不是瞬时抖动,重试只会把等待翻倍(60s → 120s → …)。
|
||||||
|
const ORDER_QUERY_MAX_ATTEMPTS = 1;
|
||||||
|
// 待发货分支的订单日期下界(天)。原 SQL 只有 `send_date IS NULL`、**没有任何时间下界**,
|
||||||
|
// 历史僵尸单全部参与扫描与累计。180 天足够覆盖真实待发(聚水潭超过半年的未发单
|
||||||
|
// 基本已人工作废),同时把扫描量从「全表」压到「近半年」。
|
||||||
|
// order_date 为空的单照旧纳入(宁可多算,不可漏算真实待发)。
|
||||||
|
const PENDING_ORDER_MAX_AGE_DAYS = 180;
|
||||||
|
// 订单索引缓存时长。10 分钟 → 30 分钟:订单索引是这个页面唯一的重查询,而它承载的
|
||||||
|
// 待发货 / 近 15 天已发货都是「分钟级变化」的数据,30 分钟对补货预警完全够用。
|
||||||
|
// 配合 jobs/mkhBomPrewarmJob(每 20 分钟后台预热)→ 请求路径基本不会撞上冷查。
|
||||||
|
// 注意:其它 10 分钟缓存(商品主数据 / 库存合并索引)仍用 MKH_CACHE_TTL_MS,别一起放大,
|
||||||
|
// 库存是业务要看的实时口径,30 分钟太旧了。
|
||||||
|
const ORDER_INDEX_CACHE_TTL_MS = 30 * 60 * 1000;
|
||||||
|
// 后台刷新失败后,至少隔这么久才允许下一次尝试 —— 否则每个请求都会去撞一次全表扫描。
|
||||||
|
const ORDER_INDEX_RETRY_AFTER_FAILURE_MS = 60 * 1000;
|
||||||
|
// 出库校验查询超时:**故意比订单主查询短**。出库校验失败是安全降级
|
||||||
|
// (宁可漏杀不可错杀,见 ⑦-c),不值得为它多等 5 分钟。
|
||||||
|
const OUTBOUND_QUERY_TIMEOUT_MS = 180000;
|
||||||
// 日销兜底(⑦-a):这些店铺的单上游不计入日销,兜底也必须排除,否则会误拉进预警
|
// 日销兜底(⑦-a):这些店铺的单上游不计入日销,兜底也必须排除,否则会误拉进预警
|
||||||
const SALE_EXCLUDED_SHOP_KEYWORDS = Object.freeze(['样品', '测试']);
|
const SALE_EXCLUDED_SHOP_KEYWORDS = Object.freeze(['样品', '测试']);
|
||||||
// 日销兜底(⑦-a):**异常单**同样不能计入「已发货」——
|
// 日销兜底(⑦-a):**异常单**同样不能计入「已发货」——
|
||||||
@ -1584,7 +1614,13 @@ async function loadOutboundOrderIds(orderIds) {
|
|||||||
${union}
|
${union}
|
||||||
) outbound_rows`,
|
) outbound_rows`,
|
||||||
params,
|
params,
|
||||||
{ requestTimeout: 180000 }
|
{
|
||||||
|
// 同订单主查询:必须独立连接,否则按共享池 60s 掐断(`requestTimeout` 不生效)。
|
||||||
|
// 但超时故意更短,见 OUTBOUND_QUERY_TIMEOUT_MS。
|
||||||
|
freshConnection: true,
|
||||||
|
connectRequestTimeoutMs: OUTBOUND_QUERY_TIMEOUT_MS,
|
||||||
|
maxAttempts: 1,
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@ -1614,11 +1650,32 @@ async function loadOutboundOrderIds(orderIds) {
|
|||||||
* · 两者商品都取自主表 `skus` 文本列,`MKH-` 打头的组合装不计
|
* · 两者商品都取自主表 `skus` 文本列,`MKH-` 打头的组合装不计
|
||||||
* · 三条过滤只作用于**已发货**分支:待发货(⑩)本来就是没出货的订单,不做出库校验
|
* · 三条过滤只作用于**已发货**分支:待发货(⑩)本来就是没出货的订单,不做出库校验
|
||||||
* ⚠️ 不降级为「抛错」:取不到时按空索引处理(补货预警退化成旧口径),并缩短缓存以便重试。
|
* ⚠️ 不降级为「抛错」:取不到时按空索引处理(补货预警退化成旧口径),并缩短缓存以便重试。
|
||||||
|
*
|
||||||
|
* 🔥 2026-11-17 性能重构(「每次打开/刷新都慢、导出也卡」的根因):
|
||||||
|
* ① 走独立连接(`freshConnection`)+ ORDER_QUERY_TIMEOUT_MS。原来传的
|
||||||
|
* `{ requestTimeout: 180000 }` 在共享池上**不生效**(请求级 request.timeout 在 mssql 12.x
|
||||||
|
* 无效),实际一律 60s 被掐断 → 每次加载都降级成「待发SKU=0 / 订单驱动=0」。
|
||||||
|
* ② 待发货分支补 order_date 下界(见 PENDING_ORDER_MAX_AGE_DAYS),不再扫历史僵尸单。
|
||||||
|
* ③ 缓存改为 stale-while-revalidate:过期后**先返回旧值**、后台补新值,
|
||||||
|
* 请求路径不再为冷查买单(实测首屏冷查 126.9s vs 缓存命中 4~5s)。
|
||||||
|
* ④ TTL 10 → 30 分钟(ORDER_INDEX_CACHE_TTL_MS),配合 jobs/mkhBomPrewarmJob
|
||||||
|
* 每 20 分钟后台预热 → 正常使用期间请求路径根本撞不上冷查。
|
||||||
*/
|
*/
|
||||||
async function loadOrderIndex() {
|
async function loadOrderIndex(queryOptions = {}) {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (orderIndexCache?.value && orderIndexCache.expiresAt > now) {
|
const force = queryOptions.force === true;
|
||||||
return orderIndexCache.value;
|
const cached = orderIndexCache?.value || null;
|
||||||
|
|
||||||
|
if (!force && cached && orderIndexCache.expiresAt > now) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
// 过期但有旧值:先给旧值,后台刷新(不阻塞请求)。
|
||||||
|
// 只有「确实没有旧值」(进程刚起 / 缓存被清)才让调用方等冷查。
|
||||||
|
if (cached && !force) {
|
||||||
|
if (!orderIndexCache.pending) {
|
||||||
|
loadOrderIndex({ force: true }).catch(() => {});
|
||||||
|
}
|
||||||
|
return cached;
|
||||||
}
|
}
|
||||||
if (orderIndexCache?.pending) return orderIndexCache.pending;
|
if (orderIndexCache?.pending) return orderIndexCache.pending;
|
||||||
|
|
||||||
@ -1645,14 +1702,26 @@ async function loadOrderIndex() {
|
|||||||
WHERE o.skus LIKE @pendingOrderSkuLike
|
WHERE o.skus LIKE @pendingOrderSkuLike
|
||||||
AND o.status NOT IN (${statusPlaceholders})
|
AND o.status NOT IN (${statusPlaceholders})
|
||||||
AND (
|
AND (
|
||||||
o.send_date IS NULL
|
(
|
||||||
|
-- 待发货:只认近 N 天的单(见 PENDING_ORDER_MAX_AGE_DAYS),
|
||||||
|
-- 原写法没有时间下界,半年以前的僵尸单也会全表参与扫描与累计。
|
||||||
|
-- order_date 为空视为「新单」照旧纳入:宁可多算,不可漏算真实待发。
|
||||||
|
o.send_date IS NULL
|
||||||
|
AND ISNULL(o.order_date, CAST(GETDATE() AS date))
|
||||||
|
>= DATEADD(DAY, -${PENDING_ORDER_MAX_AGE_DAYS}, CAST(GETDATE() AS date))
|
||||||
|
)
|
||||||
OR (
|
OR (
|
||||||
o.send_date >= DATEADD(DAY, -${SALE_WINDOW_DAYS - 1}, CAST(GETDATE() AS date))
|
o.send_date >= DATEADD(DAY, -${SALE_WINDOW_DAYS - 1}, CAST(GETDATE() AS date))
|
||||||
AND o.send_date < DATEADD(DAY, 1, CAST(GETDATE() AS date))
|
AND o.send_date < DATEADD(DAY, 1, CAST(GETDATE() AS date))
|
||||||
)
|
)
|
||||||
)`,
|
)`,
|
||||||
{ pendingOrderSkuLike: PENDING_ORDER_SKU_LIKE, ...statusParams },
|
{ pendingOrderSkuLike: PENDING_ORDER_SKU_LIKE, ...statusParams },
|
||||||
{ requestTimeout: 180000 }
|
{
|
||||||
|
// 必须走独立连接:共享池的池级 60s 超时改不动(见 ORDER_QUERY_TIMEOUT_MS)
|
||||||
|
freshConnection: true,
|
||||||
|
connectRequestTimeoutMs: ORDER_QUERY_TIMEOUT_MS,
|
||||||
|
maxAttempts: ORDER_QUERY_MAX_ATTEMPTS,
|
||||||
|
}
|
||||||
))).then(async (results) => {
|
))).then(async (results) => {
|
||||||
const pendingBySku = new Map();
|
const pendingBySku = new Map();
|
||||||
const shippedBySku = new Map();
|
const shippedBySku = new Map();
|
||||||
@ -1751,12 +1820,15 @@ async function loadOrderIndex() {
|
|||||||
ttl: PENDING_ORDER_DEGRADED_TTL_MS,
|
ttl: PENDING_ORDER_DEGRADED_TTL_MS,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return { pendingBySku, shippedBySku, degraded: false, error: null, ttl: MKH_CACHE_TTL_MS };
|
return { pendingBySku, shippedBySku, degraded: false, error: null, ttl: ORDER_INDEX_CACHE_TTL_MS };
|
||||||
});
|
});
|
||||||
|
|
||||||
const epoch = cacheEpoch;
|
const epoch = cacheEpoch;
|
||||||
const operationalEpoch = operationalCacheEpoch;
|
const operationalEpoch = operationalCacheEpoch;
|
||||||
orderIndexCache = { pending, value: null, expiresAt: 0 };
|
// 刷新期间保留旧值:并发请求照旧拿到旧快照,不必陪着这次(可能几分钟的)全表扫描一起等。
|
||||||
|
const staleValue = orderIndexCache?.value || null;
|
||||||
|
const staleExpiresAt = orderIndexCache?.expiresAt || 0;
|
||||||
|
orderIndexCache = { pending, value: staleValue, expiresAt: staleExpiresAt };
|
||||||
try {
|
try {
|
||||||
const value = await pending;
|
const value = await pending;
|
||||||
if (epoch === cacheEpoch && operationalEpoch === operationalCacheEpoch) {
|
if (epoch === cacheEpoch && operationalEpoch === operationalCacheEpoch) {
|
||||||
@ -1764,7 +1836,13 @@ async function loadOrderIndex() {
|
|||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (epoch === cacheEpoch && operationalEpoch === operationalCacheEpoch) orderIndexCache = null;
|
if (epoch === cacheEpoch && operationalEpoch === operationalCacheEpoch) {
|
||||||
|
// 刷新失败:有旧值就继续用(比空索引好得多,空索引会让待发货量显示 0),
|
||||||
|
// 并把「下次再试」压到 1 分钟后 —— 否则每个请求都会去撞一次全表扫描。
|
||||||
|
orderIndexCache = staleValue
|
||||||
|
? { pending: null, value: staleValue, expiresAt: Date.now() + ORDER_INDEX_RETRY_AFTER_FAILURE_MS }
|
||||||
|
: null;
|
||||||
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2634,6 +2712,40 @@ async function refreshMkhOperationalData() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 后台预热:把「订单两表全表扫」这个重查询挪出用户请求路径。
|
||||||
|
* 由 jobs/mkhBomPrewarmJob 在启动后、以及每 20 分钟调用一次(可用环境变量调整)。
|
||||||
|
*
|
||||||
|
* 为什么需要它:订单索引是整页唯一的重查询(冷查实测 126.9s 首屏、`loadOrderIndex` 单跑 408.5s),
|
||||||
|
* 而页面其它数据全在内存里算。只要索引一直是热的,打开页面就稳定在 4~5 秒;
|
||||||
|
* 一旦让它自然过期,下一位用户就得为冷查买单。
|
||||||
|
*
|
||||||
|
* 失败不影响功能(下一次请求会自己撞冷查),所以调用方只记日志、不重试。
|
||||||
|
* @returns {Promise<{elapsedMs: number, steps: string[]}>}
|
||||||
|
*/
|
||||||
|
async function prewarmMkhBomIndex() {
|
||||||
|
const startedAt = Date.now();
|
||||||
|
const steps = [];
|
||||||
|
|
||||||
|
// ① 订单索引:剩余有效期不足一半就强制刷一次。
|
||||||
|
// 预热 20 分钟一轮 + TTL 30 分钟 → 实际每轮都会刷(剩余 ≈10 分钟 < 15 分钟)。
|
||||||
|
const remaining = orderIndexCache?.expiresAt ? orderIndexCache.expiresAt - Date.now() : 0;
|
||||||
|
if (!orderIndexCache?.value || remaining < ORDER_INDEX_CACHE_TTL_MS / 2) {
|
||||||
|
const orderStartedAt = Date.now();
|
||||||
|
await loadOrderIndex({ force: true });
|
||||||
|
steps.push(`orderIndex=${Date.now() - orderStartedAt}ms`);
|
||||||
|
} else {
|
||||||
|
steps.push('orderIndex=fresh');
|
||||||
|
}
|
||||||
|
|
||||||
|
// ② 库存/箱表/加工单/待发货合并索引(内部会复用上一步刚刷好的订单索引)
|
||||||
|
const replenishStartedAt = Date.now();
|
||||||
|
await loadReplenishIndex();
|
||||||
|
steps.push(`replenishIndex=${Date.now() - replenishStartedAt}ms`);
|
||||||
|
|
||||||
|
return { elapsedMs: Date.now() - startedAt, steps };
|
||||||
|
}
|
||||||
|
|
||||||
function clearMkhBomCache() {
|
function clearMkhBomCache() {
|
||||||
// 🔥 先自增代际,再清各缓存:这样「本次清缓存之前已经发起、还没返回」的查询,
|
// 🔥 先自增代际,再清各缓存:这样「本次清缓存之前已经发起、还没返回」的查询,
|
||||||
// 回来时 epoch 对不上,就不会把旧快照写回缓存(否则会覆盖掉清缓存后新查到的值,
|
// 回来时 epoch 对不上,就不会把旧快照写回缓存(否则会覆盖掉清缓存后新查到的值,
|
||||||
@ -2668,10 +2780,21 @@ function clearMkhBomCache() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── 仅供单测:观察 / 催熟订单索引缓存状态 ─────────────────────────
|
||||||
|
// stale-while-revalidate 的行为依赖「缓存过期」这个时刻,用真实时钟没法稳定构造,
|
||||||
|
// 所以给测试两个只读/催熟钩子(挂在 _private 下,不参与业务逻辑)。
|
||||||
|
const getOrderIndexCacheState = () => (orderIndexCache
|
||||||
|
? { hasValue: Boolean(orderIndexCache.value), expiresAt: orderIndexCache.expiresAt, pending: Boolean(orderIndexCache.pending) }
|
||||||
|
: null);
|
||||||
|
const expireOrderIndexCacheForTest = () => {
|
||||||
|
if (orderIndexCache?.value) orderIndexCache = { ...orderIndexCache, expiresAt: 0 };
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getMkhBomList,
|
getMkhBomList,
|
||||||
clearMkhBomCache,
|
clearMkhBomCache,
|
||||||
refreshMkhOperationalData,
|
refreshMkhOperationalData,
|
||||||
|
prewarmMkhBomIndex,
|
||||||
getMkhBomDetail,
|
getMkhBomDetail,
|
||||||
listReplenishAlertRows,
|
listReplenishAlertRows,
|
||||||
listMkhBomExportRows,
|
listMkhBomExportRows,
|
||||||
@ -2741,6 +2864,17 @@ module.exports = {
|
|||||||
buildComponentExportRows,
|
buildComponentExportRows,
|
||||||
clearMkhOperationalCache,
|
clearMkhOperationalCache,
|
||||||
refreshMkhOperationalData,
|
refreshMkhOperationalData,
|
||||||
|
prewarmMkhBomIndex,
|
||||||
OPERATIONAL_REFRESH_DEDUPE_MS,
|
OPERATIONAL_REFRESH_DEDUPE_MS,
|
||||||
|
// 订单链路性能参数(2026-11-17)
|
||||||
|
ORDER_QUERY_TIMEOUT_MS,
|
||||||
|
ORDER_QUERY_MAX_ATTEMPTS,
|
||||||
|
ORDER_INDEX_CACHE_TTL_MS,
|
||||||
|
ORDER_INDEX_RETRY_AFTER_FAILURE_MS,
|
||||||
|
PENDING_ORDER_MAX_AGE_DAYS,
|
||||||
|
PENDING_ORDER_DEGRADED_TTL_MS,
|
||||||
|
OUTBOUND_QUERY_TIMEOUT_MS,
|
||||||
|
getOrderIndexCacheState,
|
||||||
|
expireOrderIndexCacheForTest,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -152,6 +152,14 @@ async function loadShippedPackages(batch) {
|
|||||||
return rows || [];
|
return rows || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 🔥 2026-11-14 修:原来调这里写的是 `{ timeoutMs: 240000 }` —— **这个参数没人读**。
|
||||||
|
// mssql 12.x 上请求级 request.timeout 不生效,超时只能靠池配置级 requestTimeout,
|
||||||
|
// 而共享池(query())的池级超时在 pool 创建时就定死了,改不了。结果是「想给 4 分钟」
|
||||||
|
// 实际一路按默认 60s 掐断(11-14 实测全表 MAX 已经跑不进 60s → 探测永远返回空)。
|
||||||
|
// 真正生效的写法只有 querySlow(临时独立连接)+ connectRequestTimeoutMs。
|
||||||
|
const SOURCE_LAST_UPDATE_TIMEOUT_MS = Number(process.env.CANGKU_WLE_SOURCE_LAST_UPDATE_TIMEOUT_MS)
|
||||||
|
|| 180000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 整张镜像表最后被灌库的时刻(表级新鲜度)。
|
* 整张镜像表最后被灌库的时刻(表级新鲜度)。
|
||||||
*
|
*
|
||||||
@ -163,16 +171,18 @@ async function loadShippedPackages(batch) {
|
|||||||
*
|
*
|
||||||
* 成本:全表 `MAX(query_time)` 在一个 402 万行的视图上实测 **13.5s**(谓词下推差),
|
* 成本:全表 `MAX(query_time)` 在一个 402 万行的视图上实测 **13.5s**(谓词下推差),
|
||||||
* 所以**只在窗口 0 行时才调**(正常批次一行都不用付这笔钱),结果随主结果进 5 分钟缓存。
|
* 所以**只在窗口 0 行时才调**(正常批次一行都不用付这笔钱),结果随主结果进 5 分钟缓存。
|
||||||
|
* ⚠️ 11-14 实测已退化到 60s 以上,所以超时上限 180s + maxAttempts=1
|
||||||
|
* (超时就退回「不知道」,重试只会把等待翻倍 —— 原来 query() 默认 2 次 × 60s 白等)。
|
||||||
*
|
*
|
||||||
* @returns {Promise<string>} 'YYYY-MM-DD HH:mm:ss',取不到返回空串
|
* @returns {Promise<string>} 'YYYY-MM-DD HH:mm:ss',取不到返回空串
|
||||||
*/
|
*/
|
||||||
async function loadSourceLastUpdate() {
|
async function loadSourceLastUpdate() {
|
||||||
try {
|
try {
|
||||||
const rows = await cloudDatabase.query(
|
const rows = await cloudDatabase.querySlow(
|
||||||
`SELECT MAX(e.query_time) AS max_q
|
`SELECT MAX(e.query_time) AS max_q
|
||||||
FROM dbo.jst_order_express_details e WITH (NOLOCK)`,
|
FROM dbo.jst_order_express_details e WITH (NOLOCK)`,
|
||||||
{},
|
{},
|
||||||
{ timeoutMs: 240000 }
|
{ connectRequestTimeoutMs: SOURCE_LAST_UPDATE_TIMEOUT_MS, maxAttempts: 1 }
|
||||||
);
|
);
|
||||||
return formatDateTime(rows?.[0]?.max_q);
|
return formatDateTime(rows?.[0]?.max_q);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -188,8 +198,15 @@ async function loadSourceLastUpdate() {
|
|||||||
// · 只查「本来就会报异常」的 oId(正常单不查,量级从 9800 降到几十);
|
// · 只查「本来就会报异常」的 oId(正常单不查,量级从 9800 降到几十);
|
||||||
// · 两套店铺表**并发**查,总耗时取较慢的那条(~8.5s),而不是相加的 13s;
|
// · 两套店铺表**并发**查,总耗时取较慢的那条(~8.5s),而不是相加的 13s;
|
||||||
// · 结果进 5 分钟结果缓存,页面自动刷新时基本命中缓存。
|
// · 结果进 5 分钟结果缓存,页面自动刷新时基本命中缓存。
|
||||||
|
//
|
||||||
|
// 🔥 2026-11-14 修:超时从「名义 240s」改成「真 20s,且不重试」。
|
||||||
|
// 原来写 `{ timeoutMs: 240000 }`(无效参数),实际按共享池 60s 掐;
|
||||||
|
// 超时后 query() 还把它当可重试错误 —— resetPool + 再查一次,**再多等 60s**。
|
||||||
|
// 11-13 09:10 那轮推送就是这么被拖掉 2 分钟的(日志:售后拦截 60s 超时 → 重连重试)。
|
||||||
|
// 售后表跟不上时业务影响只是「少剔几单」,不值得为它把整批核验拖进超时窗口:
|
||||||
|
// 2 倍实测余量(8.5s → 20s)够用,超了就降级不剔(catch 里已有兜底)。
|
||||||
const INTERCEPT_QUERY_BATCH = 500;
|
const INTERCEPT_QUERY_BATCH = 500;
|
||||||
const INTERCEPT_QUERY_TIMEOUT_MS = 240000;
|
const INTERCEPT_QUERY_TIMEOUT_MS = 20000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 找出「现场拦截、货已被售后仓签收」的订单(业务 2026-10-23 口径)。
|
* 找出「现场拦截、货已被售后仓签收」的订单(业务 2026-10-23 口径)。
|
||||||
@ -219,7 +236,7 @@ async function loadInterceptedOrders(rows) {
|
|||||||
const oIds = ids.slice(i, i + INTERCEPT_QUERY_BATCH).join(',');
|
const oIds = ids.slice(i, i + INTERCEPT_QUERY_BATCH).join(',');
|
||||||
for (const source of registry.INTERCEPT_AFTERSALE_SOURCES) {
|
for (const source of registry.INTERCEPT_AFTERSALE_SOURCES) {
|
||||||
tasks.push((async () => {
|
tasks.push((async () => {
|
||||||
const hits = await cloudDatabase.query(
|
const hits = await cloudDatabase.querySlow(
|
||||||
`SELECT h.o_id
|
`SELECT h.o_id
|
||||||
FROM ${source.header} h WITH (NOLOCK)
|
FROM ${source.header} h WITH (NOLOCK)
|
||||||
JOIN ${source.item} it WITH (NOLOCK) ON it.as_id = ${source.headerAsId}
|
JOIN ${source.item} it WITH (NOLOCK) ON it.as_id = ${source.headerAsId}
|
||||||
@ -228,7 +245,8 @@ async function loadInterceptedOrders(rows) {
|
|||||||
GROUP BY h.o_id
|
GROUP BY h.o_id
|
||||||
HAVING SUM(ISNULL(it.r_qty, 0)) > 0`,
|
HAVING SUM(ISNULL(it.r_qty, 0)) > 0`,
|
||||||
{},
|
{},
|
||||||
{ timeoutMs: INTERCEPT_QUERY_TIMEOUT_MS }
|
// maxAttempts: 1 —— 超时就在 catch 里降级「不剔除」,别让重连重试再白等一轮
|
||||||
|
{ connectRequestTimeoutMs: INTERCEPT_QUERY_TIMEOUT_MS, maxAttempts: 1 }
|
||||||
);
|
);
|
||||||
return hits || [];
|
return hits || [];
|
||||||
})().catch((err) => {
|
})().catch((err) => {
|
||||||
|
|||||||
@ -1518,3 +1518,190 @@ test('operational refresh: 并发刷新只查一次运营数据,短时重试
|
|||||||
svc.clearMkhBomCache();
|
svc.clearMkhBomCache();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ── 2026-11-17 订单链路性能(「每次打开 / 刷新都慢、导出也卡」)──────────
|
||||||
|
// 实测背景:rpt.jst_Orders 158 万行光 COUNT(*) 要 152.8s(≈1 万行/秒),
|
||||||
|
// `skus LIKE '%MKH%'` 前导通配必然全表扫;而旧代码传的 `requestTimeout: 180000`
|
||||||
|
// 在共享池上不生效(请求级 request.timeout 在 mssql 12.x 无效)→ 60s 必超时降级。
|
||||||
|
|
||||||
|
test('order index: 订单查询走独立连接并放大超时(否则共享池 60s 必超时降级)', async () => {
|
||||||
|
const cloudDatabase = require('../src/config/cloudDatabase');
|
||||||
|
const svc = require('../src/services/mkhBomService');
|
||||||
|
const originalQuery = cloudDatabase.query;
|
||||||
|
const calls = [];
|
||||||
|
|
||||||
|
cloudDatabase.query = async (sql, params, options) => {
|
||||||
|
calls.push({ sql: String(sql), params: params || {}, options: options || {} });
|
||||||
|
return [];
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
svc.clearMkhBomCache();
|
||||||
|
await svc._private.loadOrderIndex();
|
||||||
|
|
||||||
|
const orderCalls = calls.filter((call) => call.sql.includes('o.skus LIKE'));
|
||||||
|
assert.equal(orderCalls.length, 2, '两张渠道订单表都要读(只读一张会漏需求)');
|
||||||
|
orderCalls.forEach((call) => {
|
||||||
|
assert.equal(call.options.freshConnection, true, '必须显式要一条独立连接');
|
||||||
|
assert.equal(
|
||||||
|
call.options.connectRequestTimeoutMs,
|
||||||
|
svc._private.ORDER_QUERY_TIMEOUT_MS,
|
||||||
|
'超时必须走连接级参数'
|
||||||
|
);
|
||||||
|
assert.ok(call.options.connectRequestTimeoutMs >= 300000, '超时下限 5 分钟(实测单表 COUNT 就要 152.8s)');
|
||||||
|
assert.equal(call.options.maxAttempts, 1, '慢库重连重试只会把等待翻倍');
|
||||||
|
assert.equal(call.options.requestTimeout, undefined, '不要再传那个名义上生效的 requestTimeout');
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
cloudDatabase.query = originalQuery;
|
||||||
|
svc.clearMkhBomCache();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('order index: 待发货分支带近 180 天下界,不再扫历史僵尸单', async () => {
|
||||||
|
const cloudDatabase = require('../src/config/cloudDatabase');
|
||||||
|
const svc = require('../src/services/mkhBomService');
|
||||||
|
const originalQuery = cloudDatabase.query;
|
||||||
|
const sqls = [];
|
||||||
|
|
||||||
|
cloudDatabase.query = async (sql) => {
|
||||||
|
sqls.push(String(sql));
|
||||||
|
return [];
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
svc.clearMkhBomCache();
|
||||||
|
await svc._private.loadOrderIndex();
|
||||||
|
const orderSql = sqls.find((sql) => sql.includes('o.skus LIKE')) || '';
|
||||||
|
assert.match(
|
||||||
|
orderSql,
|
||||||
|
/o\.send_date IS NULL\s+AND ISNULL\(o\.order_date/,
|
||||||
|
'待发货必须把 order_date 下界和 send_date IS NULL 绑在一起'
|
||||||
|
);
|
||||||
|
assert.ok(
|
||||||
|
orderSql.includes(`DATEADD(DAY, -${svc._private.PENDING_ORDER_MAX_AGE_DAYS}`),
|
||||||
|
'下界天数必须来自常量(默认 180 天)'
|
||||||
|
);
|
||||||
|
assert.equal(svc._private.PENDING_ORDER_MAX_AGE_DAYS, 180);
|
||||||
|
} finally {
|
||||||
|
cloudDatabase.query = originalQuery;
|
||||||
|
svc.clearMkhBomCache();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('order index: 源失败时降级缓存 5 分钟,避免每分钟重打慢库', async () => {
|
||||||
|
const cloudDatabase = require('../src/config/cloudDatabase');
|
||||||
|
const svc = require('../src/services/mkhBomService');
|
||||||
|
const originalQuery = cloudDatabase.query;
|
||||||
|
let queryCount = 0;
|
||||||
|
|
||||||
|
cloudDatabase.query = async (sql) => {
|
||||||
|
queryCount += 1;
|
||||||
|
if (String(sql).includes('jst_qmcust_orderlist')) {
|
||||||
|
throw new Error('Timeout: Request failed to complete in 60000ms');
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
svc.clearMkhBomCache();
|
||||||
|
const index = await svc._private.loadOrderIndex();
|
||||||
|
assert.equal(index.degraded, true, '一张表失败要标降级');
|
||||||
|
assert.equal(svc._private.PENDING_ORDER_DEGRADED_TTL_MS, 5 * 60 * 1000, '降级缓存必须是 5 分钟');
|
||||||
|
assert.equal(index.ttl, 5 * 60 * 1000);
|
||||||
|
|
||||||
|
const state = svc._private.getOrderIndexCacheState();
|
||||||
|
const remaining = state.expiresAt - Date.now();
|
||||||
|
assert.ok(remaining > 4 * 60 * 1000, `降级态有效期应接近 5 分钟,实际 ${remaining}ms`);
|
||||||
|
|
||||||
|
const before = queryCount;
|
||||||
|
await svc._private.loadOrderIndex();
|
||||||
|
assert.equal(queryCount, before, '降级缓存有效期内不得重复扫表');
|
||||||
|
} finally {
|
||||||
|
cloudDatabase.query = originalQuery;
|
||||||
|
svc.clearMkhBomCache();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('order index: 缓存过期后先返回旧值、后台刷新(用户不再为冷查买单)', async () => {
|
||||||
|
const cloudDatabase = require('../src/config/cloudDatabase');
|
||||||
|
const svc = require('../src/services/mkhBomService');
|
||||||
|
const originalQuery = cloudDatabase.query;
|
||||||
|
let phase = 'cold';
|
||||||
|
let orderQueries = 0;
|
||||||
|
// 模拟「后台刷新卡在 300s 大表扫描上」:永不 resolve,但绝不能拖住请求路径
|
||||||
|
const pendingForever = new Promise(() => {});
|
||||||
|
|
||||||
|
cloudDatabase.query = async (sql) => {
|
||||||
|
if (!String(sql).includes('o.skus LIKE')) return [];
|
||||||
|
orderQueries += 1;
|
||||||
|
if (phase === 'cold') {
|
||||||
|
return [{
|
||||||
|
oId: 1,
|
||||||
|
orderDate: new Date('2026-11-10T00:00:00Z'),
|
||||||
|
sendDate: null,
|
||||||
|
skus: '1.MKHDNZ0409260700059',
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
return pendingForever;
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
svc.clearMkhBomCache();
|
||||||
|
const first = await svc._private.loadOrderIndex();
|
||||||
|
assert.equal(first.pendingBySku.get('MKHDNZ0409260700059').pendingOrderQty, 1);
|
||||||
|
|
||||||
|
phase = 'stale';
|
||||||
|
svc._private.expireOrderIndexCacheForTest();
|
||||||
|
const startedAt = Date.now();
|
||||||
|
const second = await svc._private.loadOrderIndex();
|
||||||
|
const elapsed = Date.now() - startedAt;
|
||||||
|
|
||||||
|
assert.equal(second, first, '过期后必须立刻返回旧快照,而不是等后台刷新');
|
||||||
|
assert.ok(elapsed < 1000, `必须立刻返回(旧写法要等 126.9s 冷查),实际 ${elapsed}ms`);
|
||||||
|
|
||||||
|
await new Promise((resolve) => { setTimeout(resolve, 20); });
|
||||||
|
const state = svc._private.getOrderIndexCacheState();
|
||||||
|
assert.equal(state.pending, true, '必须真的发起了一次后台刷新');
|
||||||
|
assert.equal(orderQueries, 4, '前台 2 次(两张表)+ 后台刷新 2 次');
|
||||||
|
} finally {
|
||||||
|
cloudDatabase.query = originalQuery;
|
||||||
|
svc.clearMkhBomCache();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('prewarm: 有效期过半不重查,过期则强制刷新订单索引', async () => {
|
||||||
|
const cloudDatabase = require('../src/config/cloudDatabase');
|
||||||
|
const svc = require('../src/services/mkhBomService');
|
||||||
|
const originalQuery = cloudDatabase.query;
|
||||||
|
let orderQueries = 0;
|
||||||
|
|
||||||
|
cloudDatabase.query = async (sql) => {
|
||||||
|
if (String(sql).includes('o.skus LIKE')) orderQueries += 1;
|
||||||
|
return [];
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
svc.clearMkhBomCache();
|
||||||
|
await svc.prewarmMkhBomIndex();
|
||||||
|
const afterCold = orderQueries;
|
||||||
|
assert.equal(afterCold, 2, '冷启动预热要把两张订单表都查一遍');
|
||||||
|
|
||||||
|
// 刚刷完,剩余 30 分钟 > 一半 → 不该再查(预热 20 分钟一轮,不会白扫)
|
||||||
|
const fresh = await svc.prewarmMkhBomIndex();
|
||||||
|
assert.ok(fresh.steps.includes('orderIndex=fresh'), `实际步骤: ${fresh.steps.join(', ')}`);
|
||||||
|
assert.equal(orderQueries, afterCold, '有效期充足时不得重查订单表');
|
||||||
|
|
||||||
|
// 过期 → 下一轮预热必须强制刷新
|
||||||
|
svc._private.expireOrderIndexCacheForTest();
|
||||||
|
const refreshed = await svc.prewarmMkhBomIndex();
|
||||||
|
assert.ok(
|
||||||
|
refreshed.steps.some((step) => /^orderIndex=\d+ms$/.test(step)),
|
||||||
|
`过期后必须强制刷新,实际步骤: ${refreshed.steps.join(', ')}`
|
||||||
|
);
|
||||||
|
assert.equal(orderQueries, afterCold + 2);
|
||||||
|
} finally {
|
||||||
|
cloudDatabase.query = originalQuery;
|
||||||
|
svc.clearMkhBomCache();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@ -145,11 +145,13 @@ test('状态为空时按运单号区分:有号=未揽收,无号=无物流单
|
|||||||
const dbPath = require.resolve('../src/config/cloudDatabase');
|
const dbPath = require.resolve('../src/config/cloudDatabase');
|
||||||
|
|
||||||
function stubDb(rows, options = {}) {
|
function stubDb(rows, options = {}) {
|
||||||
const calls = { times: 0, sql: [], params: [] };
|
const calls = { times: 0, sql: [], params: [], options: [], api: [] };
|
||||||
const handler = async (sql, params) => {
|
const makeHandler = (api) => async (sql, params, opts) => {
|
||||||
calls.times += 1;
|
calls.times += 1;
|
||||||
calls.sql.push(String(sql));
|
calls.sql.push(String(sql));
|
||||||
calls.params.push(params || {});
|
calls.params.push(params || {});
|
||||||
|
calls.options.push(opts || {});
|
||||||
|
calls.api.push(api);
|
||||||
// 售后拦截判定会额外查两张售后表 —— 按表名分流,别把物流行当成售后行
|
// 售后拦截判定会额外查两张售后表 —— 按表名分流,别把物流行当成售后行
|
||||||
if (/jst_returnAndRefundInfo|jst_qmcust_refundlist/u.test(String(sql))) {
|
if (/jst_returnAndRefundInfo|jst_qmcust_refundlist/u.test(String(sql))) {
|
||||||
return options.interceptRows || [];
|
return options.interceptRows || [];
|
||||||
@ -164,7 +166,7 @@ function stubDb(rows, options = {}) {
|
|||||||
id: dbPath,
|
id: dbPath,
|
||||||
filename: dbPath,
|
filename: dbPath,
|
||||||
loaded: true,
|
loaded: true,
|
||||||
exports: { query: handler, querySlow: handler },
|
exports: { query: makeHandler('query'), querySlow: makeHandler('querySlow') },
|
||||||
};
|
};
|
||||||
return calls;
|
return calls;
|
||||||
}
|
}
|
||||||
@ -698,13 +700,21 @@ test('窗口 0 行 + 数据源停更 → emptyWindow/sourceStale,hint 明说
|
|||||||
|
|
||||||
test('窗口 0 行但数据源是新的 → 仍然提示「0 条记录」,不能静默全绿', async () => {
|
test('窗口 0 行但数据源是新的 → 仍然提示「0 条记录」,不能静默全绿', async () => {
|
||||||
const nov2 = new Date(2026, 10, 2, 10, 0, 0);
|
const nov2 = new Date(2026, 10, 2, 10, 0, 0);
|
||||||
|
// ⚠️ sourceLastUpdate 必须是「相对真实现在」的时间,不能像原来那样硬编码 2026-11-02 09:58。
|
||||||
|
// 服务层算 ageMinutes 用的是 registry.beijingNow()(真实时钟),不是测试传进来的 now ——
|
||||||
|
// 硬编码的日期过一天就变成「停更 24h」,这个用例第二天起就必挂(2026-11-14 实测挂在 main 上)。
|
||||||
|
const freshText = new Intl.DateTimeFormat('sv-SE', {
|
||||||
|
timeZone: 'Asia/Shanghai',
|
||||||
|
year: 'numeric', month: '2-digit', day: '2-digit',
|
||||||
|
hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false,
|
||||||
|
}).format(new Date(Date.now() - 3 * 60 * 1000));
|
||||||
await withStub(async (svc) => {
|
await withStub(async (svc) => {
|
||||||
const data = await svc.getLogisticsExceptions({ date: '2026-11-02', now: nov2, limit: 100 });
|
const data = await svc.getLogisticsExceptions({ date: '2026-11-02', now: nov2, limit: 100 });
|
||||||
assert.equal(data.freshness.emptyWindow, true);
|
assert.equal(data.freshness.emptyWindow, true);
|
||||||
assert.equal(data.freshness.sourceStale, false, '表是新的,不该报停更');
|
assert.equal(data.freshness.sourceStale, false, '表是新的,不该报停更');
|
||||||
assert.match(data.freshness.hint, /0 条发货记录/, '真 0 单也要说清是「库里没有记录」');
|
assert.match(data.freshness.hint, /0 条发货记录/, '真 0 单也要说清是「库里没有记录」');
|
||||||
assert.match(data.freshness.hint, /数据源本身正常/);
|
assert.match(data.freshness.hint, /数据源本身正常/);
|
||||||
}, [], { sourceMaxQueryTime: '2026-11-02 09:58:00' });
|
}, [], { sourceMaxQueryTime: freshText });
|
||||||
});
|
});
|
||||||
|
|
||||||
test('窗口有行时不做全表探测(省掉 13.5s 的表级 MAX)', async () => {
|
test('窗口有行时不做全表探测(省掉 13.5s 的表级 MAX)', async () => {
|
||||||
@ -715,3 +725,37 @@ test('窗口有行时不做全表探测(省掉 13.5s 的表级 MAX)', async
|
|||||||
assert.equal(calls.sql.filter((s) => /MAX\(\s*e\.query_time\s*\)/iu.test(s)).length, 0);
|
assert.equal(calls.sql.filter((s) => /MAX\(\s*e\.query_time\s*\)/iu.test(s)).length, 0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ── ⑨ 超时参数必须「真生效」(2026-11-14)──────────────────────────────────
|
||||||
|
// 起因:11-13 09:10 那轮钉钉推送 300s 超时没推出去。查代码发现两处写了
|
||||||
|
// `{ timeoutMs: 240000 }` 想「给足 4 分钟」,而 cloudDatabase **根本不读 timeoutMs**
|
||||||
|
// (mssql 12.x 请求级 request.timeout 不生效,只能靠池配置级 requestTimeout,
|
||||||
|
// 共享池的池级超时创建时就定死了)—— 实际一路按 60s 掐断,
|
||||||
|
// 超时后 query() 还当可重试错误重连再查一次,日志里白等了约 2 分钟。
|
||||||
|
test('售后拦截查询走 querySlow + 20s 短超时 + 不重试', async () => {
|
||||||
|
await withStub(async (svc, calls) => {
|
||||||
|
await svc.getLogisticsExceptions({ date: '2026-10-21', now: at(21, 10, 0), limit: 100 });
|
||||||
|
const idx = calls.sql.findIndex((s) => /jst_returnAndRefundInfo|jst_qmcust_refundlist/u.test(s));
|
||||||
|
assert.ok(idx >= 0, '这批有异常单,应该去查售后表');
|
||||||
|
assert.equal(calls.api[idx], 'querySlow', '必须走 querySlow:query() 的池级超时调用方改不了');
|
||||||
|
assert.equal(calls.options[idx].connectRequestTimeoutMs, 20000, '2 倍实测余量(8.5s → 20s)就够');
|
||||||
|
assert.equal(calls.options[idx].maxAttempts, 1, '超时就降级「不剔除」,别重连重试白等一轮');
|
||||||
|
assert.equal(calls.options[idx].timeoutMs, undefined, 'timeoutMs 是无效参数,不许再写');
|
||||||
|
}, rows, { interceptRows: [{ o_id: 1 }] });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('空窗口的表级 MAX 探测也走 querySlow(共享池 60s 跑不动全表)', async () => {
|
||||||
|
const nov2 = new Date(2026, 10, 2, 10, 0, 0);
|
||||||
|
await withStub(async (svc, calls) => {
|
||||||
|
await svc.getLogisticsExceptions({ date: '2026-11-02', now: nov2, limit: 100 });
|
||||||
|
const idx = calls.sql.findIndex((s) => /MAX\(\s*e\.query_time\s*\)/iu.test(s));
|
||||||
|
assert.ok(idx >= 0, '空窗口要探测表级最后灌库时刻');
|
||||||
|
assert.equal(calls.api[idx], 'querySlow');
|
||||||
|
assert.ok(
|
||||||
|
Number(calls.options[idx].connectRequestTimeoutMs) >= 120000,
|
||||||
|
`全表 MAX 实测已超 60s,必须放宽,实际 ${calls.options[idx].connectRequestTimeoutMs}`
|
||||||
|
);
|
||||||
|
assert.equal(calls.options[idx].maxAttempts, 1, '探测失败退回「不知道」即可,不值得重试');
|
||||||
|
assert.equal(calls.options[idx].timeoutMs, undefined);
|
||||||
|
}, [], { sourceMaxQueryTime: '2026-11-01 07:55:04' });
|
||||||
|
});
|
||||||
|
|||||||
@ -607,3 +607,137 @@ test('快照字段缺失(老缓存 / 接口未返回)→ 不能崩,也不
|
|||||||
assert.equal(job.buildFreshnessWarning(undefined, {}), '');
|
assert.equal(job.buildFreshnessWarning(undefined, {}), '');
|
||||||
assert.equal(job.buildFreshnessWarning({ coversDeadline: null }, {}), '');
|
assert.equal(job.buildFreshnessWarning({ coversDeadline: null }, {}), '');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ── ⑥ 推送失败要重试,不能「一天只有一次机会」(2026-11-11)──────────────
|
||||||
|
// 实况:11-11 09:10 那次撞上业务库连不上(状态里 `Failed to connect to 218.93.9.21:1433`),
|
||||||
|
// 老逻辑用 `HH:mm === sendTime` 严格相等,只给一分钟机会,失败后当天彻底不推、群里一整天没消息。
|
||||||
|
// 下面把窗口与节流的边界钉死,避免改成「每分钟都重跑」把 240s 的全量核验打成 DDoS。
|
||||||
|
function beijing(isoText) {
|
||||||
|
return new Date(isoText); // 测试用 UTC 写法,避免本机时区把断言带偏
|
||||||
|
}
|
||||||
|
|
||||||
|
test('重试调度:到点第一次跑,推成功之后当天不再跑', () => {
|
||||||
|
const sendTime = '09:10';
|
||||||
|
|
||||||
|
const first = job.resolveScheduledPushDecision({
|
||||||
|
now: beijing('2026-11-11T01:10:00Z'), // 北京 09:10
|
||||||
|
sendTime,
|
||||||
|
state: {},
|
||||||
|
});
|
||||||
|
assert.equal(first.run, true);
|
||||||
|
assert.equal(first.reason, 'first_round');
|
||||||
|
assert.equal(first.attempt, 1);
|
||||||
|
assert.equal(first.pushDate, '2026-11-11');
|
||||||
|
|
||||||
|
const sent = job.resolveScheduledPushDecision({
|
||||||
|
now: beijing('2026-11-11T01:11:00Z'), // 北京 09:11,09:10 那轮已成功
|
||||||
|
sendTime,
|
||||||
|
state: { lastSentDate: '2026-11-11', lastSentAt: '2026-11-11T01:10:03Z' },
|
||||||
|
});
|
||||||
|
assert.equal(sent.run, false);
|
||||||
|
assert.equal(sent.reason, 'already_sent_today');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('重试调度:没到点的分钟一律不跑(不会被「窗口」提前触发)', () => {
|
||||||
|
const decision = job.resolveScheduledPushDecision({
|
||||||
|
now: beijing('2026-11-11T01:09:00Z'), // 北京 09:09
|
||||||
|
sendTime: '09:10',
|
||||||
|
state: {},
|
||||||
|
});
|
||||||
|
assert.equal(decision.run, false);
|
||||||
|
assert.equal(decision.reason, 'before_time');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('重试调度:失败后按 5 分钟间隔重试,间隔内不重复拉全量核验', () => {
|
||||||
|
const sendTime = '09:10';
|
||||||
|
const failedState = { lastAttemptAt: '2026-11-11T01:10:00Z', attemptDate: '2026-11-11', attemptCount: 1 };
|
||||||
|
|
||||||
|
const tooSoon = job.resolveScheduledPushDecision({
|
||||||
|
now: beijing('2026-11-11T01:13:00Z'), // 距上次尝试 3 分钟
|
||||||
|
sendTime,
|
||||||
|
state: failedState,
|
||||||
|
});
|
||||||
|
assert.equal(tooSoon.run, false);
|
||||||
|
assert.equal(tooSoon.reason, 'wait_interval');
|
||||||
|
|
||||||
|
const retry = job.resolveScheduledPushDecision({
|
||||||
|
now: beijing('2026-11-11T01:15:00Z'), // 距上次尝试 5 分钟,正好到间隔
|
||||||
|
sendTime,
|
||||||
|
state: failedState,
|
||||||
|
});
|
||||||
|
assert.equal(retry.run, true);
|
||||||
|
assert.equal(retry.reason, 'retry');
|
||||||
|
assert.equal(retry.attempt, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('重试调度:过了「推送时刻 + 30 分钟」窗口就不再补推', () => {
|
||||||
|
const sendTime = '09:10';
|
||||||
|
const failedState = { lastAttemptAt: '2026-11-11T01:35:00Z', attemptDate: '2026-11-11', attemptCount: 6 };
|
||||||
|
|
||||||
|
const inside = job.resolveScheduledPushDecision({
|
||||||
|
now: beijing('2026-11-11T01:40:00Z'), // 北京 09:40,窗口最后一分钟
|
||||||
|
sendTime,
|
||||||
|
state: failedState,
|
||||||
|
});
|
||||||
|
assert.equal(inside.run, true, '窗口边界(含)仍要重试');
|
||||||
|
|
||||||
|
const closed = job.resolveScheduledPushDecision({
|
||||||
|
now: beijing('2026-11-11T01:41:00Z'), // 北京 09:41
|
||||||
|
sendTime,
|
||||||
|
state: failedState,
|
||||||
|
});
|
||||||
|
assert.equal(closed.run, false);
|
||||||
|
assert.equal(closed.reason, 'window_closed');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('重试调度:跨天后尝试次数归零,昨天的失败不节流今天的首轮', () => {
|
||||||
|
const decision = job.resolveScheduledPushDecision({
|
||||||
|
now: beijing('2026-11-12T01:10:00Z'), // 北京 11-12 09:10
|
||||||
|
sendTime: '09:10',
|
||||||
|
state: { lastAttemptAt: '2026-11-11T01:35:00Z', attemptDate: '2026-11-11', attemptCount: 6 },
|
||||||
|
});
|
||||||
|
assert.equal(decision.run, true);
|
||||||
|
assert.equal(decision.reason, 'first_round');
|
||||||
|
assert.equal(decision.attempt, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('重试调度:重试窗口不跨日(23:50 推送不会被次日 00:10 当成「窗口内」)', () => {
|
||||||
|
const decision = job.resolveScheduledPushDecision({
|
||||||
|
now: beijing('2026-11-11T16:10:00Z'), // 北京 11-12 00:10
|
||||||
|
sendTime: '23:50',
|
||||||
|
state: {},
|
||||||
|
});
|
||||||
|
assert.equal(decision.run, false);
|
||||||
|
assert.equal(decision.reason, 'before_time');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('重试调度:尝试记录残缺(有次数没时间)时不能卡死,该重试就重试', () => {
|
||||||
|
const decision = job.resolveScheduledPushDecision({
|
||||||
|
now: beijing('2026-11-11T01:11:00Z'),
|
||||||
|
sendTime: '09:10',
|
||||||
|
state: { attemptDate: '2026-11-11', attemptCount: 2, lastAttemptAt: '' },
|
||||||
|
});
|
||||||
|
assert.equal(decision.run, true);
|
||||||
|
assert.equal(decision.reason, 'retry');
|
||||||
|
assert.equal(decision.attempt, 3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('重试调度:sendTime 非法时安静不跑(配置坏了不能把定时任务打崩)', () => {
|
||||||
|
const decision = job.resolveScheduledPushDecision({
|
||||||
|
now: beijing('2026-11-11T01:10:00Z'),
|
||||||
|
sendTime: 'not-a-time',
|
||||||
|
state: {},
|
||||||
|
});
|
||||||
|
assert.equal(decision.run, false);
|
||||||
|
assert.equal(decision.reason, 'bad_send_time');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('重试参数:默认 30 分钟窗口 / 5 分钟间隔,环境变量可覆盖', () => {
|
||||||
|
assert.equal(job.DEFAULT_RETRY_WINDOW_MINUTES, 30);
|
||||||
|
assert.equal(job.DEFAULT_RETRY_INTERVAL_MINUTES, 5);
|
||||||
|
assert.equal(job.minuteOfDay('09:10'), 550);
|
||||||
|
assert.equal(job.minuteOfDay('00:00'), 0);
|
||||||
|
assert.equal(job.minuteOfDay('23:59'), 1439);
|
||||||
|
assert.equal(job.minuteOfDay('9:10'), null, '必须是零填充的 HH:mm');
|
||||||
|
assert.equal(job.minuteOfDay(''), null);
|
||||||
|
});
|
||||||
|
|||||||
@ -360,24 +360,17 @@ const MkhBomData = () => {
|
|||||||
|
|
||||||
const load = useCallback(async ({ background = false } = {}) => {
|
const load = useCallback(async ({ background = false } = {}) => {
|
||||||
const requestGeneration = listRequestGateRef.current.begin();
|
const requestGeneration = listRequestGateRef.current.begin();
|
||||||
let showProgress = false;
|
|
||||||
let progressGeneration = null;
|
|
||||||
if (!background) setLoading(true);
|
if (!background) setLoading(true);
|
||||||
if (!background) setError('');
|
if (!background) setError('');
|
||||||
try {
|
try {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
const forceFresh = !lastListRefreshAtRef.current
|
// 🔥 2026-11-17:首屏 / F5 / 每 2 分钟轮询**不再带 fresh=1**。
|
||||||
|| now - lastListRefreshAtRef.current >= AUTO_REFRESH_MS;
|
// 旧逻辑「首屏 || 距上次 ≥ 2 分钟」就发 fresh=1 → 后端清空运营缓存 →
|
||||||
showProgress = forceFresh && !background;
|
// 订单两表必然全表扫(冷查实测首屏 126.9s,而命中缓存只要 4~5 秒)。
|
||||||
if (showProgress) progressGeneration = beginRefreshProgress('正在更新最新业务数据');
|
// 于是「每次打开、每次刷新、每 2 分钟」都在等两分钟以上。
|
||||||
// 首次打开/F5 和定时轮询才重读云库;筛选、分页只复用刚刷新的内存索引。
|
// 现在新鲜度由后端后台预热维持(每 20 分钟一轮,见 jobs/mkhBomPrewarmJob),
|
||||||
// 后端仍会合并多用户的并发刷新,不会对业务云库造成重复全量查询。
|
// 页面走缓存;要强制重读只有一个入口 ——「同步最新数据 / 刷新数据」按钮(handleSync)。
|
||||||
const result = await getMkhBomList({
|
const result = await getMkhBomList({ ...query, live: 1, _refresh: now });
|
||||||
...query,
|
|
||||||
live: 1,
|
|
||||||
...(forceFresh ? { fresh: 1 } : null),
|
|
||||||
_refresh: now,
|
|
||||||
});
|
|
||||||
if (!listRequestGateRef.current.isCurrent(requestGeneration)) return;
|
if (!listRequestGateRef.current.isCurrent(requestGeneration)) return;
|
||||||
if (!result || result.success === false) {
|
if (!result || result.success === false) {
|
||||||
setError(result?.error || '加载 MKH-BOM 资料失败');
|
setError(result?.error || '加载 MKH-BOM 资料失败');
|
||||||
@ -385,22 +378,21 @@ const MkhBomData = () => {
|
|||||||
}
|
}
|
||||||
const snapshot = result?.data || null;
|
const snapshot = result?.data || null;
|
||||||
try {
|
try {
|
||||||
requireMkhFreshContract(snapshot, { requireApplied: forceFresh });
|
requireMkhFreshContract(snapshot);
|
||||||
} catch (contractError) {
|
} catch (contractError) {
|
||||||
setData({ rows: [], total: 0, page: 1, pageSize: 50, bomReady: false });
|
setData({ rows: [], total: 0, page: 1, pageSize: 50, bomReady: false });
|
||||||
throw contractError;
|
throw contractError;
|
||||||
}
|
}
|
||||||
setData(snapshot);
|
setData(snapshot);
|
||||||
if (forceFresh) lastListRefreshAtRef.current = Date.now();
|
lastListRefreshAtRef.current = Date.now();
|
||||||
setError('');
|
setError('');
|
||||||
} catch (requestError) {
|
} catch (requestError) {
|
||||||
if (!listRequestGateRef.current.isCurrent(requestGeneration)) return;
|
if (!listRequestGateRef.current.isCurrent(requestGeneration)) return;
|
||||||
setError(requestError?.response?.data?.error || requestError?.message || '加载 MKH-BOM 资料失败');
|
setError(requestError?.response?.data?.error || requestError?.message || '加载 MKH-BOM 资料失败');
|
||||||
} finally {
|
} finally {
|
||||||
if (!background && listRequestGateRef.current.isCurrent(requestGeneration)) setLoading(false);
|
if (!background && listRequestGateRef.current.isCurrent(requestGeneration)) setLoading(false);
|
||||||
if (showProgress) endRefreshProgress(progressGeneration);
|
|
||||||
}
|
}
|
||||||
}, [beginRefreshProgress, endRefreshProgress, query]);
|
}, [query]);
|
||||||
|
|
||||||
useEffect(() => { load(); }, [load]);
|
useEffect(() => { load(); }, [load]);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user