WMS_MAIN/app/schedule/retry_inbox_push.js
liufumingren a667f1401e feat(inbox): 推送失败自动重发改为仓库级开关与重发时间范围(默认关闭)
仓库设置新增嵌套配置 `subject_config.inbox_push_retry = { enabled, window_minutes }`,
结构与「收货未入库提醒」(receiving_pending_inbound_notice)保持一致:

- `enabled`:默认 false —— 未配置、配置缺失或无法读取时一律**关闭**,需在仓库设置中显式开启
- `window_minutes`:重发时间范围,单位**分钟**,1-1440,默认 30(仅开关开启时生效)

改动:

- app/model/store.js:新增 DEFAULT_INBOX_PUSH_RETRY 与 normalizeInboxPushRetry(非法结构/非法 JSON
  回退默认;enabled 非布尔值、window_minutes 越界抛「subject_config.inbox_push_retry.window_minutes
  必须是 1-1440 的整数(分钟)」),由 normalizeSubjectConfig 统一调用
- app/service/systems/setting/warehouse.js:WAREHOUSE_SETTING_FIELDS 新增 key=inbox_push_retry
  (type=object + item_keys=[enabled, window_minutes]),供仓库设置页经 getFieldMeta 动态渲染
- app/service/inbox/message.js:retryFailedByWarehouse 逐仓读仓库设置(缺省关闭、按仓库配置的窗口
  调用 retryFailed);retryFailed 新增 warehouseId 过滤;readPushRetryConfig 读嵌套结构并兜底
  enabled=false / 30 分钟
- app/schedule/retry_inbox_push.js:注释同步为「仅处理显式开启的仓库、缺省 30 分钟」
- config/config.default.js:兜底失败时间窗 2 小时 → 30 分钟;pushRetryEnabled 保留为全局应急总闸
  (默认 true,关闭后所有仓库都不重发)

单测:test/inbox/retry_inbox_push.test.js + test/app/model/store.test.js 共 40 passing,覆盖默认关闭、
非法结构回退、flag 真值归一化、窗口 1-1440 越界、仓库开关与自定义窗口、复用原记录等场景。
2026-09-16 15:04:25 +08:00

58 lines
2.3 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';
/**
* 站内信出站推送失败自动重推定时任务。
*
* 每 3 分钟在单 worker 中扫描一次,按仓库逐个处理(开关与时间范围均由「仓库设置」控制):
* - 只处理在仓库设置里**显式开启**「站内信推送失败自动重发」的仓库
* (`subject_config.inbox_push_retry.enabled`,缺省/无法读取时为关闭,不重推);
* - 只重推该仓库「失败时间在 `subject_config.inbox_push_retry.window_minutes`
* (分钟,1-1440,缺省 30)内」的消息,超出范围不再补发;
* - 每仓单轮最多重推 config.inbox.pushRetryLimit 条(默认 20,硬上限 200)。
*
* 推送结果仍回写到同一条记录(成功 2 / 失败 3),成功后不再被扫到,失败则由下一轮继续重试,
* 直到推送成功或滑出该仓库配置的时间窗;此外 config.inbox.pushRetryEnabled 可作为全局应急开关。
*
* 典型失败原因是桥接离线(回调 HTTP 200 但 body code=503,如 wx offline / qiwe offline),
* 属可自愈的临时故障:桥接恢复后靠本任务补发,无需人工介入。
*/
module.exports = {
schedule: {
interval: '3m',
type: 'worker',
immediate: false,
disable: false,
},
/**
* 执行出站推送失败消息重推。
*
* @param {Object} ctx Egg 上下文。
* @return {Promise<void>} 无返回值。
*/
async task(ctx) {
if (ctx.app._inboxPushRetryRunning) {
return;
}
ctx.app._inboxPushRetryRunning = true;
try {
const result = await ctx.service.inbox.message.retryFailedByWarehouse();
// 无待重推记录时不刷日志,避免每 3 分钟噪音
if (result && result.scanned > 0) {
ctx.logger.info(
`[retry-inbox-push] 本轮重推 仓库=${result.warehouses}`
+ `(开启=${result.enabledWarehouses} 关闭=${result.disabledWarehouses} 跳过=${result.skippedWarehouses})`
+ ` scanned=${result.scanned} retried=${result.retried} succeeded=${result.succeeded}`
+ ` failed=${result.failed} skipped=${result.skipped}`
);
}
} catch (error) {
ctx.logger.error(`[retry-inbox-push] 任务执行失败: ${error.message}`);
} finally {
ctx.app._inboxPushRetryRunning = false;
}
},
};