Release 77 #1
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();
|
||||
|
||||
@@ -370,6 +370,73 @@ describe('RiotAccountEnrichmentService', () => {
|
||||
expect(user.save).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('refreshes league entries without refreshing recent matches', async () => {
|
||||
const recent = new Date(Date.now() - 60_000);
|
||||
const account = makeAccount({
|
||||
revisionDate: Date.now() - 60_000,
|
||||
tiers: [
|
||||
{
|
||||
leagueId: 'L1',
|
||||
queueType: 'RANKED_SOLO_5x5',
|
||||
tier: 'GOLD',
|
||||
rank: 'II',
|
||||
puuid: 'puuid-1',
|
||||
leaguePoints: 1,
|
||||
wins: 1,
|
||||
losses: 1,
|
||||
fetchedAt: recent,
|
||||
},
|
||||
],
|
||||
recentStatsFetchedAt: recent,
|
||||
recentStatsSummary: createEmptyRiotRecentStatsSummary(),
|
||||
});
|
||||
const user = makeUser(account);
|
||||
const { service, leagueService, recentMatchesService } = makeService({
|
||||
user,
|
||||
league: {
|
||||
getEntriesByPuuid: jest.fn().mockResolvedValue([
|
||||
{
|
||||
leagueId: 'L2',
|
||||
queueType: 'RANKED_SOLO_5x5',
|
||||
tier: 'PLATINUM',
|
||||
rank: 'IV',
|
||||
puuid: 'puuid-1',
|
||||
leaguePoints: 20,
|
||||
wins: 10,
|
||||
losses: 8,
|
||||
},
|
||||
]),
|
||||
} as Partial<RiotLeagueService>,
|
||||
});
|
||||
|
||||
const result = await service.refreshLeagueEntries({
|
||||
userId: user._id,
|
||||
puuid: 'puuid-1',
|
||||
platform: 'kr',
|
||||
});
|
||||
|
||||
expect(result).toBe('updated');
|
||||
expect(leagueService.getEntriesByPuuid).toHaveBeenCalledWith('puuid-1', {
|
||||
platform: 'kr',
|
||||
});
|
||||
expect(recentMatchesService.syncRecentMatches).not.toHaveBeenCalled();
|
||||
expect(account.tiers).toHaveLength(1);
|
||||
expect(account.tiers[0]).toMatchObject({
|
||||
leagueId: 'L2',
|
||||
queueType: 'RANKED_SOLO_5x5',
|
||||
tier: 'PLATINUM',
|
||||
rank: 'IV',
|
||||
leaguePoints: 20,
|
||||
});
|
||||
expect(account.tierHistory.at(-1)).toMatchObject({
|
||||
queueType: 'RANKED_SOLO_5x5',
|
||||
tier: 'PLATINUM',
|
||||
rank: 'IV',
|
||||
leaguePoints: 20,
|
||||
});
|
||||
expect(user.save).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('records failed for individual failing fetches but updates the rest', async () => {
|
||||
const account = makeAccount();
|
||||
const user = makeUser(account);
|
||||
|
||||
@@ -52,6 +52,8 @@ export interface EnrichResult {
|
||||
account: RiotAccount | null;
|
||||
}
|
||||
|
||||
export type RefreshLeagueEntriesResult = 'updated' | 'failed';
|
||||
|
||||
@Injectable()
|
||||
export class RiotAccountEnrichmentService {
|
||||
private readonly logger = new Logger(RiotAccountEnrichmentService.name);
|
||||
@@ -95,6 +97,29 @@ export class RiotAccountEnrichmentService {
|
||||
return promise;
|
||||
}
|
||||
|
||||
public async refreshLeagueEntries(params: {
|
||||
userId: mongoose.Types.ObjectId;
|
||||
puuid: string;
|
||||
platform?: RiotPlatformRoute;
|
||||
}): Promise<RefreshLeagueEntriesResult> {
|
||||
const UserModel = await this.connectionService.getModel('user');
|
||||
const user = await UserModel.findById(params.userId).exec();
|
||||
if (!user) return 'failed';
|
||||
|
||||
const account = user.riotAccounts.find((a) => a.puuid === params.puuid);
|
||||
if (!account) return 'failed';
|
||||
|
||||
const outcome = await this.fetchLeagueSafe(
|
||||
params.puuid,
|
||||
params.platform ?? DEFAULT_PLATFORM,
|
||||
);
|
||||
if (outcome.status !== 'updated' || !outcome.dto) return 'failed';
|
||||
|
||||
this.applyLeagueEntries(account, outcome.dto);
|
||||
await user.save();
|
||||
return 'updated';
|
||||
}
|
||||
|
||||
private async doEnrichRiotAccount(params: {
|
||||
userId: mongoose.Types.ObjectId;
|
||||
puuid: string;
|
||||
|
||||
Reference in new issue
Block a user