- 用事务 Outbox 和签名 Webhook 替换直接状态回调 - 增加 Outbox 投递 worker、重试与低频对账兜底 - 缓存客户领取页绑定状态并补充签名与迁移测试
52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
import { logWarn } from '../../utils/logger.js'
|
|
import { reconcileDouyuFulfillmentStates } from './douyu-fulfillment-service.js'
|
|
|
|
const DEFAULT_RECONCILE_SECONDS = 900
|
|
|
|
let timer: NodeJS.Timeout | null = null
|
|
let stopped = true
|
|
|
|
function intervalMs() {
|
|
const value = Number(process.env.PLATFORM_FULFILLMENT_RECONCILE_SECONDS)
|
|
const seconds =
|
|
Number.isFinite(value) && value > 0
|
|
? Math.max(60, Math.trunc(value))
|
|
: DEFAULT_RECONCILE_SECONDS
|
|
return seconds * 1000
|
|
}
|
|
|
|
async function run() {
|
|
if (stopped) return
|
|
try {
|
|
await reconcileDouyuFulfillmentStates()
|
|
} catch (error) {
|
|
logWarn('[douyu-fulfillment]', '低频对账失败', { error })
|
|
} finally {
|
|
if (!stopped) {
|
|
timer = setTimeout(() => {
|
|
void run()
|
|
}, intervalMs())
|
|
}
|
|
}
|
|
}
|
|
|
|
export function startDouyuFulfillmentReconciler() {
|
|
if (timer || !stopped) return
|
|
stopped = false
|
|
// 启动后先让 Webhook 工作一分钟,再执行首轮兜底对账。
|
|
timer = setTimeout(
|
|
() => {
|
|
void run()
|
|
},
|
|
Math.min(60_000, intervalMs()),
|
|
)
|
|
}
|
|
|
|
export function stopDouyuFulfillmentReconciler() {
|
|
stopped = true
|
|
if (timer) {
|
|
clearTimeout(timer)
|
|
timer = null
|
|
}
|
|
}
|