WMS_MAIN/app/schedule/retry_inbox_push.js
liufumingren e666abc0a2 feat(inbox): 新增出站推送失败自动重推定时任务
新增 app/schedule/retry_inbox_push.js:每 3 分钟扫描「push_status=3 且失败时间在 2 小时内」的
出站消息并重推(config.inbox.pushRetryWindowMs / pushRetryLimit,单轮默认 20 条、硬上限 200),
成功即停、失败下一轮继续、超出窗口不再补推;worker 单进程执行 + app._inboxPushRetryRunning
重入锁,scanned>0 才写日志。

- inbox.message 新增 retryFailed():按 created_at ASC 逐条串行重推(先补更接近窗口边界的),
  缺 warehouse_id/group_id/group_name/content 的记录跳过并告警,retry_count 递增仅作观测
- inbox.message 新增 _repush():复用原记录重推(reuse_message_id),ext_id 剥离 sys:ack: 前缀,
  有用户信息走 ai_reply、系统通知走 system;重推期间不改记录状态,进程中断时下一轮仍能扫到
- processor.writeOutboundReply 支持 reuse_message_id:重推时跳过按 sys:ack:{ext_id} 查建记录,
  推送结果回写到原记录本身,修复系统通知(sys:notice:*)重推会另建记录、原记录状态永远停留在
  失败/待推送的问题;状态回写由 ext_id 改为按记录 id(对既有调用路径等价)
- 手动重推 retry() 改走 _repush,入参与响应文案不变,methods/system.json 无需变更

失败主因是桥接离线(回调 HTTP 200 但 body code=503:wx offline / qiwe offline),属可自愈的
临时故障;此前仅支持人工单条重推,生产已积压 2441 条未送达的客户回信与系统通知。

单测 test/inbox/retry_inbox_push.test.js 18 例(纯 mock,覆盖调度配置/重入/异常兜底/扫描条件/
窗口与上限/复用原记录/跳过与容错);AI_METHOD_ROUTING.md 方法清单已刷新(inbox/message.js 7 → 9)。
2026-09-15 22:38:38 +08:00

55 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';
/**
* 站内信出站推送失败自动重推定时任务。
*
* 每 3 分钟在单 worker 中扫描一次「推送失败(push_status=3)且失败时间在窗口内」的出站消息
* (窗口默认 2 小时,超窗口的消息不再补推,避免陈旧回信/通知打扰用户),逐条复用原记录重推:
* 推送结果仍回写到同一条记录(成功 2 / 失败 3),成功后不再被扫到,失败则由下一轮继续重试,
* 直到推送成功或滑出时间窗。
*
* 典型失败原因是桥接离线(回调 HTTP 200 但 body code=503,如 wx offline / qiwe offline),
* 属可自愈的临时故障:桥接恢复后靠本任务补发,无需人工介入。
*
* 单轮最多重推条数与失败时间窗由 config.inbox 控制:
* - pushRetryWindowMs:失败时间窗(毫秒,默认 2 小时)
* - pushRetryLimit:单轮最多重推条数(默认 20,硬上限 200)
*/
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.retryFailed();
// 无待重推记录时不刷日志,避免每 3 分钟噪音
if (result && result.scanned > 0) {
ctx.logger.info(
`[retry-inbox-push] 本轮重推 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;
}
},
};