feat: auto provision tournament codes

This commit is contained in:
dahyeon committed 2026-06-20 02:29:30 -07:00
1 parent 65bc214fb2
commit bac29ac933
13 files changed
+1164 -117

No files matched your search

@@ -1,4 +1,5 @@
import { Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { TournamentCodeAutoProvisionService } from './tournament-code-auto-provision.service';
import { TournamentCodeService } from './tournament-code.service';
@Injectable()
@@ -6,8 +7,12 @@ export class TournamentCodeScheduler implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(TournamentCodeScheduler.name);
private attachTimer?: NodeJS.Timeout;
private expireTimer?: NodeJS.Timeout;
private refillTimer?: NodeJS.Timeout;
constructor(private readonly tournamentCodeService: TournamentCodeService) {}
constructor(
private readonly tournamentCodeService: TournamentCodeService,
private readonly autoProvisionService: TournamentCodeAutoProvisionService,
) {}
public onModuleInit() {
this.attachTimer = setInterval(() => {
@@ -16,16 +21,26 @@ export class TournamentCodeScheduler implements OnModuleInit, OnModuleDestroy {
this.expireTimer = setInterval(() => {
void this.markExpired();
}, 60 * 60 * 1000);
if (isAutoProvisionEnabled()) {
this.refillTimer = setInterval(() => {
void this.ensureVendorPool();
}, 5 * 60 * 1000);
}
this.attachTimer.unref?.();
this.expireTimer.unref?.();
this.refillTimer?.unref?.();
void this.attachPending();
void this.markExpired();
if (isAutoProvisionEnabled()) {
void this.ensureVendorPool();
}
}
public onModuleDestroy() {
if (this.attachTimer) clearInterval(this.attachTimer);
if (this.expireTimer) clearInterval(this.expireTimer);
if (this.refillTimer) clearInterval(this.refillTimer);
}
private async attachPending() {
@@ -54,4 +69,23 @@ export class TournamentCodeScheduler implements OnModuleInit, OnModuleDestroy {
);
}
}
private async ensureVendorPool() {
try {
await this.autoProvisionService.ensurePool();
} catch (error) {
this.logger.warn(
`Failed to auto-provision tournament codes: ${
error instanceof Error ? error.message : error
}`,
);
}
}
}
function isAutoProvisionEnabled(): boolean {
const raw = process.env.TOURNAMENT_CODE_AUTO_PROVISION_ENABLED
?.trim()
.toLowerCase();
return raw !== 'false' && raw !== '0' && raw !== 'off';
}