开发者 2026-10-17:「后续不能出现这种无法接收到的情况」。 事故链(本地真机):主系统 22:48 派单并推 preparations.allocated(SUP37831443), 我方因"台账指向的本系统订单已不存在"返回 500,主系统重试 3 次后放弃; 旧实现只打日志、界面/通知零痕迹,直到人工查主系统投递记录才发现。 三层保证: ① 自愈(上一提交 ef81c7c):陈旧占坑/台账指针自动清理并重建; ② 可见+告警(本次):失败落 public.main_system_import_failures (键 = 分公司×配送日×主系统分公司×主系统订单,attempts 累加不刷屏), 首次失败站内通知 admin/super_admin(6 小时内去重),新增只读接口 GET /api/integration/main-system/import-failures 供界面/排障查看; 下一次导入成功自动销账(resolved_at)。 ③ 兜底重试:定时对账每 10 分钟一次(已存在);生产需补 client secret 才可用(已在文档标注)。 新增迁移 20261057;trackDayOutcome() 挂在建单/追加两条返回路径上,异常一律吞掉(告警不许反过来把导入搞挂)。 验证: · 服务侧真库冒烟:首次失败 attempts=1/isFirst=true → 再次 attempts=2;open 可查;clear 后 open=0 · 迁移 upsert 语义(同键累加、单行)真库事务内验证通过 · 单测 mainSystemImportFailureLedger 6 条;mainSystem 子集 30 文件 / 473 用例全过 · 设计门禁:8 通过 / 1 已知红 check-ai-cost / verify-facts 因本地缺 tenant_br003 红(均基线)
137 lines
5.3 KiB
TypeScript
137 lines
5.3 KiB
TypeScript
/**
|
|
* 主系统导入**失败台账**(2026-10-17:「后续不能出现这种无法接收到的情况」)。
|
|
*
|
|
* 为什么要有它:推送失败时旧实现只打日志 + 返回 500,主系统重试几次就放弃 ⇒ 订单永远进不来,
|
|
* 而**界面上一点痕迹都没有**。真机事故 `ORD_WEB_20261017_BR002_0001` 就是这样:
|
|
* 连续失败到主系统放弃,直到有人去查主系统的投递记录才发现。
|
|
*
|
|
* 三层保证里的第二层(① 自愈见 `mainSystemOrderImport` 的陈旧占坑清理;③ 定时对账一直在跑):
|
|
* · 失败 → **落台账**(同键累加 attempts,不刷屏);
|
|
* · 首次失败 → **站内通知管理员**(有通知中心,不新增界面文案体系);
|
|
* · 下一次导入成功 → **自动销账**(resolved_at)。
|
|
*
|
|
* 纪律:本模块只做"留痕 + 告警",**不改导入结果**;任何异常都吞掉(告警链路不许反过来把导入搞挂)。
|
|
*/
|
|
import { pool } from '../db';
|
|
|
|
export type ImportFailureRow = {
|
|
id: string;
|
|
branch_code: string;
|
|
delivery_date: string;
|
|
main_branch_code: string;
|
|
main_order_code: string | null;
|
|
our_order_code: string | null;
|
|
error_code: string;
|
|
error_message: string | null;
|
|
attempts: number;
|
|
first_seen_at: string;
|
|
last_seen_at: string;
|
|
resolved_at: string | null;
|
|
};
|
|
|
|
/** 失败分类:只有"能按类统计"的才算,其余落 unknown(不要瞎猜根因) */
|
|
export function classifyImportFailure(message: string | null | undefined): string {
|
|
const text = String(message || '');
|
|
if (text.includes('台账指向的本系统订单已不存在') || text.includes('陈旧')) return 'stale_order_pointer';
|
|
if (text.includes('价格') || text.includes('零价')) return 'zero_price';
|
|
if (text.includes('不存在')) return 'not_found';
|
|
if (text.includes('超时') || text.includes('timeout')) return 'timeout';
|
|
if (text.includes('权限')) return 'permission';
|
|
return 'unknown';
|
|
}
|
|
|
|
/**
|
|
* 记一次导入失败(幂等累加)。返回累加后的次数 —— 调用方据此决定"首次失败才通知"。
|
|
*/
|
|
export async function recordMainSystemImportFailure(input: {
|
|
branchCode: string;
|
|
deliveryDate: string;
|
|
mainBranchCode: string;
|
|
mainOrderCode?: string | null;
|
|
ourOrderCode?: string | null;
|
|
message?: string | null;
|
|
errorCode?: string | null;
|
|
}): Promise<{ attempts: number; isFirst: boolean } | null> {
|
|
try {
|
|
const message = String(input.message || '').slice(0, 1000) || null;
|
|
const code = String(input.errorCode || classifyImportFailure(message)).slice(0, 64);
|
|
const { rows } = await pool.query<{ attempts: number }>(
|
|
`INSERT INTO public.main_system_import_failures AS f
|
|
(branch_code, delivery_date, main_branch_code, main_order_code, our_order_code,
|
|
error_code, error_message)
|
|
VALUES ($1, $2::date, $3, $4, $5, $6, $7)
|
|
ON CONFLICT (branch_code, delivery_date, main_branch_code, COALESCE(main_order_code, ''))
|
|
DO UPDATE SET
|
|
attempts = f.attempts + 1,
|
|
last_seen_at = NOW(),
|
|
error_code = EXCLUDED.error_code,
|
|
error_message = EXCLUDED.error_message,
|
|
our_order_code = COALESCE(EXCLUDED.our_order_code, f.our_order_code),
|
|
resolved_at = NULL
|
|
RETURNING f.attempts`,
|
|
[
|
|
input.branchCode,
|
|
input.deliveryDate,
|
|
input.mainBranchCode,
|
|
input.mainOrderCode ?? null,
|
|
input.ourOrderCode ?? null,
|
|
code,
|
|
message,
|
|
]
|
|
);
|
|
const attempts = Number(rows[0]?.attempts) || 1;
|
|
return { attempts, isFirst: attempts === 1 };
|
|
} catch (error) {
|
|
console.error(
|
|
'[main-system-import-failures] 记失败台账失败(不影响导入):',
|
|
(error as Error)?.message || error
|
|
);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** 导入恢复 ⇒ 销账(把这批未解决的失败都关掉) */
|
|
export async function clearMainSystemImportFailures(input: {
|
|
branchCode: string;
|
|
deliveryDate: string;
|
|
mainBranchCode: string;
|
|
mainOrderCodes?: string[];
|
|
}): Promise<number> {
|
|
try {
|
|
const codes = (input.mainOrderCodes || []).filter(Boolean);
|
|
const result = await pool.query(
|
|
`UPDATE public.main_system_import_failures
|
|
SET resolved_at = NOW(), last_seen_at = NOW()
|
|
WHERE branch_code = $1 AND delivery_date = $2::date AND main_branch_code = $3
|
|
AND resolved_at IS NULL
|
|
AND ($4::text[] IS NULL OR main_order_code = ANY($4::text[]))`,
|
|
[input.branchCode, input.deliveryDate, input.mainBranchCode, codes.length ? codes : null]
|
|
);
|
|
return Number(result.rowCount) || 0;
|
|
} catch (error) {
|
|
console.error(
|
|
'[main-system-import-failures] 销账失败(不影响导入):',
|
|
(error as Error)?.message || error
|
|
);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/** 未解决的失败(界面/排障用;按最近失败倒序) */
|
|
export async function listOpenImportFailures(input: {
|
|
branchCode?: string | null;
|
|
limit?: number;
|
|
}): Promise<ImportFailureRow[]> {
|
|
const branchCode = String(input.branchCode || '').trim();
|
|
const limit = Math.min(Math.max(Number(input.limit) || 20, 1), 100);
|
|
const { rows } = await pool.query<ImportFailureRow>(
|
|
`SELECT * FROM public.main_system_import_failures
|
|
WHERE resolved_at IS NULL
|
|
AND ($1 = '' OR branch_code = $1)
|
|
ORDER BY last_seen_at DESC
|
|
LIMIT $2`,
|
|
[branchCode, limit]
|
|
);
|
|
return rows;
|
|
}
|