feat(fulfillment): 增加事务 Outbox 同步履约状态
- 用事务 Outbox 和签名 Webhook 替换直接状态回调 - 增加 Outbox 投递 worker、重试与低频对账兜底 - 缓存客户领取页绑定状态并补充签名与迁移测试
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user