48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
import {
|
|
Injectable,
|
|
Logger,
|
|
OnModuleDestroy,
|
|
OnModuleInit,
|
|
} from '@nestjs/common';
|
|
import { DakaoOutboxService } from './dakao-outbox.service';
|
|
|
|
const SWEEP_INTERVAL_MS = 60 * 1000;
|
|
const STALE_OUTBOX_MS = 5 * 60 * 1000;
|
|
|
|
@Injectable()
|
|
export class DakaoOutboxSweeperService
|
|
implements OnModuleInit, OnModuleDestroy
|
|
{
|
|
private readonly logger = new Logger(DakaoOutboxSweeperService.name);
|
|
private timer: NodeJS.Timeout | undefined;
|
|
|
|
constructor(private readonly outboxService: DakaoOutboxService) {}
|
|
|
|
onModuleInit() {
|
|
this.timer = setInterval(() => {
|
|
void this.sweep();
|
|
}, SWEEP_INTERVAL_MS);
|
|
this.timer.unref();
|
|
}
|
|
|
|
onModuleDestroy() {
|
|
if (this.timer) clearInterval(this.timer);
|
|
}
|
|
|
|
private async sweep(): Promise<void> {
|
|
try {
|
|
const swept = await this.outboxService.sweepStale({
|
|
staleMs: STALE_OUTBOX_MS,
|
|
limit: 20,
|
|
});
|
|
if (swept > 0) {
|
|
this.logger.warn(`Re-published ${swept} stale Dakao outbox messages.`);
|
|
}
|
|
} catch (err) {
|
|
this.logger.error(
|
|
`Dakao outbox sweep failed: ${(err as Error)?.stack ?? err}`,
|
|
);
|
|
}
|
|
}
|
|
}
|