fix: resume tournament result polling after delayed codes

This commit is contained in:
dahyeon committed 2026-06-25 04:58:10 -07:00
1 parent e50a683336
commit 2984bd1604
8 files changed
+207 -21

No files matched your search

@@ -1,4 +1,5 @@
import { Module } from '@nestjs/common';
import { TournamentResultModule } from '../tournament-result/tournament-result.module';
import { TournamentCodeController } from './tournament-code.controller';
import { TournamentCodeAutoProvisionService } from './tournament-code-auto-provision.service';
import { TournamentCodeReplacementController } from './tournament-code-replacement.controller';
@@ -7,6 +8,7 @@ import { TournamentCodeService } from './tournament-code.service';
import { TournamentCodeVendorService } from './tournament-code-vendor.service';
@Module({
imports: [TournamentResultModule],
controllers: [TournamentCodeController, TournamentCodeReplacementController],
providers: [
TournamentCodeService,
@@ -0,0 +1,62 @@
import { TournamentCodeScheduler } from './tournament-code.scheduler';
describe('TournamentCodeScheduler', () => {
function createScheduler(params?: {
attachedMatches?: unknown[];
scheduleInitialPoll?: jest.Mock;
}) {
const tournamentCodeService = {
attachPendingMatchDocuments: jest
.fn()
.mockResolvedValue(params?.attachedMatches ?? []),
markExpired: jest.fn().mockResolvedValue(0),
};
const autoProvisionService = {
ensurePool: jest.fn().mockResolvedValue(undefined),
};
const tournamentResultService = {
scheduleInitialPoll:
params?.scheduleInitialPoll ?? jest.fn().mockResolvedValue(null),
};
const scheduler = new TournamentCodeScheduler(
tournamentCodeService as never,
autoProvisionService as never,
tournamentResultService as never,
) as unknown as { attachPending(): Promise<void> };
return {
scheduler,
tournamentCodeService,
tournamentResultService,
};
}
it('starts result polling after attaching delayed tournament codes', async () => {
const attachedMatches = [{ _id: 'match-1' }, { _id: 'match-2' }];
const { scheduler, tournamentResultService } = createScheduler({
attachedMatches,
});
await scheduler.attachPending();
expect(tournamentResultService.scheduleInitialPoll).toHaveBeenCalledTimes(
2,
);
expect(tournamentResultService.scheduleInitialPoll).toHaveBeenNthCalledWith(
1,
attachedMatches[0],
);
expect(tournamentResultService.scheduleInitialPoll).toHaveBeenNthCalledWith(
2,
attachedMatches[1],
);
});
it('does not schedule result polling when no match received new codes', async () => {
const { scheduler, tournamentResultService } = createScheduler();
await scheduler.attachPending();
expect(tournamentResultService.scheduleInitialPoll).not.toHaveBeenCalled();
});
});
@@ -1,4 +1,5 @@
import { Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { TournamentResultService } from '../tournament-result/tournament-result.service';
import { TournamentCodeAutoProvisionService } from './tournament-code-auto-provision.service';
import { TournamentCodeService } from './tournament-code.service';
@@ -12,6 +13,7 @@ export class TournamentCodeScheduler implements OnModuleInit, OnModuleDestroy {
constructor(
private readonly tournamentCodeService: TournamentCodeService,
private readonly autoProvisionService: TournamentCodeAutoProvisionService,
private readonly tournamentResultService: TournamentResultService,
) {}
public onModuleInit() {
@@ -45,9 +47,15 @@ export class TournamentCodeScheduler implements OnModuleInit, OnModuleDestroy {
private async attachPending() {
try {
const touched = await this.tournamentCodeService.attachPendingMatches();
if (touched > 0) {
this.logger.debug(`Attached tournament codes for ${touched} matches.`);
const matches =
await this.tournamentCodeService.attachPendingMatchDocuments();
if (matches.length > 0) {
this.logger.debug(
`Attached tournament codes for ${matches.length} matches.`,
);
for (const match of matches) {
await this.tournamentResultService.scheduleInitialPoll(match);
}
}
} catch (error) {
this.logger.warn(
@@ -430,6 +430,13 @@ export class TournamentCodeService {
public async attachPendingMatches(
communityId?: mongoose.Types.ObjectId,
): Promise<number> {
const matches = await this.attachPendingMatchDocuments(communityId);
return matches.length;
}
public async attachPendingMatchDocuments(
communityId?: mongoose.Types.ObjectId,
): Promise<MatchDocument[]> {
const MatchModel = await this.connectionService.getModel('match');
const filter: {
status: { $in: string[] };
@@ -440,15 +447,18 @@ export class TournamentCodeService {
if (communityId) filter.communityId = communityId;
const matches = await MatchModel.find(filter).exec();
let touched = 0;
const touched: MatchDocument[] = [];
for (const match of matches) {
if ((match.tournamentCodeIds?.length ?? 0) >= this.resolveRequiredSlots(match)) {
if (
(match.tournamentCodeIds?.length ?? 0) >=
this.resolveRequiredSlots(match)
) {
continue;
}
const before = match.tournamentCodeIds?.length ?? 0;
await this.reserveForMatch(match);
const after = match.tournamentCodeIds?.length ?? 0;
if (after > before) touched += 1;
const updatedMatch = await this.reserveForMatch(match);
const after = updatedMatch.tournamentCodeIds?.length ?? 0;
if (after > before) touched.push(updatedMatch);
}
return touched;
}