fix: refresh riot tiers from history jobs
This commit is contained in:
1 parent
ad84bf1f89
commit
c4f19d256d
4 files changed
+233
-2
No files matched your search
@@ -0,0 +1,116 @@
|
||||
import mongoose from 'mongoose';
|
||||
import { ConnectionService } from '../global/database/connection.service';
|
||||
import { GarnetService } from '../global/garnet/garnet.service';
|
||||
import { LegacyImportService } from '../legacy-import/legacy-import.service';
|
||||
import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.service';
|
||||
import { RiotRecentMatchesService } from '../riot/riot-recent-matches.service';
|
||||
import { TournamentResultService } from '../tournament-result/tournament-result.service';
|
||||
import { RIOT_JOB_GROUP, RiotRefreshSubJob } from './riot-job-queue.types';
|
||||
import { RiotJobQueueService } from './riot-job-queue.service';
|
||||
import { RiotJobWorkerService } from './riot-job-worker.service';
|
||||
import { RiotRefreshGateway } from './riot-refresh.gateway';
|
||||
|
||||
function execResult<T>(value: T) {
|
||||
return { exec: jest.fn().mockResolvedValue(value) };
|
||||
}
|
||||
|
||||
function makeWorker() {
|
||||
const garnetService = {
|
||||
acknowledgeStream: jest.fn().mockResolvedValue(undefined),
|
||||
} as unknown as jest.Mocked<GarnetService>;
|
||||
const jobQueueService = {
|
||||
markProcessing: jest.fn().mockResolvedValue(undefined),
|
||||
markCompleted: jest.fn().mockResolvedValue({
|
||||
parentJobId: 'parent-1',
|
||||
jobId: 'job-1',
|
||||
queueGroup: 'summoners-rift',
|
||||
puuid: 'puuid-1',
|
||||
state: 'completed',
|
||||
}),
|
||||
markFailed: jest.fn(),
|
||||
} as unknown as jest.Mocked<RiotJobQueueService>;
|
||||
const recentMatchesService = {
|
||||
ensureFreshForQueue: jest.fn().mockResolvedValue(undefined),
|
||||
} as unknown as jest.Mocked<RiotRecentMatchesService>;
|
||||
const userModel = {
|
||||
updateOne: jest.fn().mockReturnValue(execResult({ modifiedCount: 1 })),
|
||||
};
|
||||
const connectionService = {
|
||||
getModel: jest.fn().mockResolvedValue(userModel),
|
||||
} as unknown as jest.Mocked<ConnectionService>;
|
||||
const refreshGateway = {
|
||||
broadcastRefreshEvent: jest.fn(),
|
||||
} as unknown as jest.Mocked<RiotRefreshGateway>;
|
||||
const tournamentResultService = {
|
||||
processPollJob: jest.fn(),
|
||||
} as unknown as jest.Mocked<TournamentResultService>;
|
||||
const legacyImportService = {
|
||||
processMemberJob: jest.fn(),
|
||||
} as unknown as jest.Mocked<LegacyImportService>;
|
||||
const enrichmentService = {
|
||||
refreshLeagueEntries: jest.fn().mockResolvedValue('updated'),
|
||||
} as unknown as jest.Mocked<RiotAccountEnrichmentService>;
|
||||
|
||||
const worker = new RiotJobWorkerService(
|
||||
garnetService,
|
||||
jobQueueService,
|
||||
recentMatchesService,
|
||||
connectionService,
|
||||
refreshGateway,
|
||||
tournamentResultService,
|
||||
legacyImportService,
|
||||
enrichmentService,
|
||||
);
|
||||
|
||||
return {
|
||||
worker,
|
||||
garnetService,
|
||||
jobQueueService,
|
||||
recentMatchesService,
|
||||
connectionService,
|
||||
refreshGateway,
|
||||
enrichmentService,
|
||||
};
|
||||
}
|
||||
|
||||
describe('RiotJobWorkerService', () => {
|
||||
it('refreshes league entries while processing user history refresh jobs', async () => {
|
||||
const { worker, enrichmentService, recentMatchesService, garnetService } =
|
||||
makeWorker();
|
||||
const userId = new mongoose.Types.ObjectId().toString();
|
||||
const job: RiotRefreshSubJob = {
|
||||
jobId: 'job-1',
|
||||
parentJobId: 'parent-1',
|
||||
userId,
|
||||
puuid: 'puuid-1',
|
||||
region: 'asia',
|
||||
platform: 'kr',
|
||||
queueGroup: 'summoners-rift',
|
||||
queueIds: [420, 430],
|
||||
requestedAt: Date.now(),
|
||||
priority: 'high',
|
||||
};
|
||||
|
||||
await (worker as unknown as {
|
||||
processEntry: (
|
||||
stream: string,
|
||||
entry: { id: string; message: Record<string, string> },
|
||||
) => Promise<void>;
|
||||
}).processEntry('riot:queue:high', {
|
||||
id: '1-0',
|
||||
message: { job: JSON.stringify(job) },
|
||||
});
|
||||
|
||||
expect(recentMatchesService.ensureFreshForQueue).toHaveBeenCalledTimes(2);
|
||||
expect(enrichmentService.refreshLeagueEntries).toHaveBeenCalledWith({
|
||||
userId: expect.any(mongoose.Types.ObjectId),
|
||||
puuid: 'puuid-1',
|
||||
platform: 'kr',
|
||||
});
|
||||
expect(garnetService.acknowledgeStream).toHaveBeenCalledWith(
|
||||
'riot:queue:high',
|
||||
RIOT_JOB_GROUP,
|
||||
'1-0',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -9,7 +9,10 @@ import { ConnectionService } from '../global/database/connection.service';
|
||||
import { GarnetService, StreamEntry } from '../global/garnet/garnet.service';
|
||||
import { LegacyImportService } from '../legacy-import/legacy-import.service';
|
||||
import type { LegacyImportMemberJob } from '../legacy-import/legacy-import.types';
|
||||
import { RiotAccountEnrichmentService } from '../riot/riot-account-enrichment.service';
|
||||
import { RiotRecentMatchesService } from '../riot/riot-recent-matches.service';
|
||||
import { TournamentResultService } from '../tournament-result/tournament-result.service';
|
||||
import type { PollTournamentResultJob } from '../tournament-result/tournament-result.types';
|
||||
import {
|
||||
RIOT_JOB_GROUP,
|
||||
RIOT_JOB_HIGH_STREAM,
|
||||
@@ -19,8 +22,6 @@ import {
|
||||
} from './riot-job-queue.types';
|
||||
import { RiotJobQueueService } from './riot-job-queue.service';
|
||||
import { RiotRefreshGateway } from './riot-refresh.gateway';
|
||||
import { TournamentResultService } from '../tournament-result/tournament-result.service';
|
||||
import type { PollTournamentResultJob } from '../tournament-result/tournament-result.types';
|
||||
|
||||
const LEADER_KEY = 'riot:worker:leader';
|
||||
const LEADER_TTL_SECONDS = 60;
|
||||
@@ -39,6 +40,7 @@ export class RiotJobWorkerService implements OnModuleInit, OnModuleDestroy {
|
||||
private readonly refreshGateway: RiotRefreshGateway,
|
||||
private readonly tournamentResultService: TournamentResultService,
|
||||
private readonly legacyImportService: LegacyImportService,
|
||||
private readonly enrichmentService: RiotAccountEnrichmentService,
|
||||
) {}
|
||||
|
||||
public async onModuleInit() {
|
||||
@@ -102,6 +104,7 @@ export class RiotJobWorkerService implements OnModuleInit, OnModuleDestroy {
|
||||
queueId,
|
||||
});
|
||||
}
|
||||
await this.refreshLeagueEntries(job);
|
||||
await this.markUserFetchedAt(job);
|
||||
const event = await this.jobQueueService.markCompleted(job);
|
||||
this.refreshGateway.broadcastRefreshEvent(event);
|
||||
@@ -135,6 +138,26 @@ export class RiotJobWorkerService implements OnModuleInit, OnModuleDestroy {
|
||||
}
|
||||
}
|
||||
|
||||
private async refreshLeagueEntries(job: RiotRefreshSubJob): Promise<void> {
|
||||
try {
|
||||
const result = await this.enrichmentService.refreshLeagueEntries({
|
||||
userId: new mongoose.Types.ObjectId(job.userId),
|
||||
puuid: job.puuid,
|
||||
platform: job.platform,
|
||||
});
|
||||
if (result !== 'failed') return;
|
||||
this.logger.warn(
|
||||
`Riot league refresh failed ${job.jobId}: puuid=${job.puuid}`,
|
||||
);
|
||||
} catch (error) {
|
||||
this.logger.warn(
|
||||
`Riot league refresh failed ${job.jobId}: ${
|
||||
error instanceof Error ? error.message : error
|
||||
}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async markUserFetchedAt(job: RiotRefreshSubJob): Promise<void> {
|
||||
const UserModel = await this.connectionService.getModel('user');
|
||||
const now = new Date();
|
||||
|
||||
Reference in new issue
Block a user