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 } }