58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
import { Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
|
|
import { TournamentCodeService } from './tournament-code.service';
|
|
|
|
@Injectable()
|
|
export class TournamentCodeScheduler implements OnModuleInit, OnModuleDestroy {
|
|
private readonly logger = new Logger(TournamentCodeScheduler.name);
|
|
private attachTimer?: NodeJS.Timeout;
|
|
private expireTimer?: NodeJS.Timeout;
|
|
|
|
constructor(private readonly tournamentCodeService: TournamentCodeService) {}
|
|
|
|
public onModuleInit() {
|
|
this.attachTimer = setInterval(() => {
|
|
void this.attachPending();
|
|
}, 30_000);
|
|
this.expireTimer = setInterval(() => {
|
|
void this.markExpired();
|
|
}, 60 * 60 * 1000);
|
|
this.attachTimer.unref?.();
|
|
this.expireTimer.unref?.();
|
|
|
|
void this.attachPending();
|
|
void this.markExpired();
|
|
}
|
|
|
|
public onModuleDestroy() {
|
|
if (this.attachTimer) clearInterval(this.attachTimer);
|
|
if (this.expireTimer) clearInterval(this.expireTimer);
|
|
}
|
|
|
|
private async attachPending() {
|
|
try {
|
|
const touched = await this.tournamentCodeService.attachPendingMatches();
|
|
if (touched > 0) {
|
|
this.logger.debug(`Attached tournament codes for ${touched} matches.`);
|
|
}
|
|
} catch (error) {
|
|
this.logger.warn(
|
|
`Failed to attach pending tournament codes: ${
|
|
error instanceof Error ? error.message : error
|
|
}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
private async markExpired() {
|
|
try {
|
|
await this.tournamentCodeService.markExpired();
|
|
} catch (error) {
|
|
this.logger.warn(
|
|
`Failed to mark expired tournament codes: ${
|
|
error instanceof Error ? error.message : error
|
|
}`,
|
|
);
|
|
}
|
|
}
|
|
}
|