import { Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common'; import { SendChainService } from './send-chain.service'; @Injectable() export class DrainageReceiptRecoveryService implements OnModuleInit, OnModuleDestroy { private timer?: ReturnType; private running = false; private readonly logger = new Logger(DrainageReceiptRecoveryService.name); constructor(private readonly sendChain: SendChainService) {} onModuleInit() { if (['api', 'callback', 'outbox'].includes(process.env.CMPP_PROCESS_ROLE ?? 'all')) return; this.timer = setInterval(() => void this.scan(), 10000); this.timer.unref?.(); } async scan() { if (this.running) return; this.running = true; try { await this.sendChain.recoverDrainageFailureReceipts(); } catch (error) { this.logger.error(`选路拦截回执恢复失败: ${String(error)}`); } finally { this.running = false; } } onModuleDestroy() { if (this.timer) clearInterval(this.timer); } }