177 lines
5.2 KiB
TypeScript
177 lines
5.2 KiB
TypeScript
import { Processor, WorkerHost } from '@nestjs/bullmq';
|
|
import type { Job } from 'bullmq';
|
|
import {
|
|
ANALYSIS_QUEUE_NAME,
|
|
FEATURE_RECOMPUTE_JOB_NAME,
|
|
normalizeFeatureRecomputeBatchSize,
|
|
normalizeRequestedMatchCount,
|
|
USER_ANALYSIS_JOB_NAME,
|
|
} from './analysis.constants';
|
|
import { AnalysisPipelineService } from './analysis-pipeline.service';
|
|
import type {
|
|
FeatureRecomputeJobData,
|
|
UserAnalysisJobData,
|
|
} from './analysis.types';
|
|
import {
|
|
RIOT_PLATFORM_ROUTES,
|
|
RIOT_REGIONAL_ROUTES,
|
|
toRegionalRoute,
|
|
type RiotPlatformRoute,
|
|
type RiotRegionalRoute,
|
|
} from '../riot/riot.types';
|
|
|
|
@Processor(ANALYSIS_QUEUE_NAME)
|
|
export class AnalysisProcessor extends WorkerHost {
|
|
constructor(
|
|
private readonly analysisPipelineService: AnalysisPipelineService,
|
|
) {
|
|
super();
|
|
}
|
|
|
|
public async process(job: Job<unknown>): Promise<void> {
|
|
if (!job.id) {
|
|
throw new Error('Analysis job is missing a BullMQ job id.');
|
|
}
|
|
if (job.name === USER_ANALYSIS_JOB_NAME) {
|
|
const data = this.parseUserAnalysisJobData(job.data);
|
|
await this.analysisPipelineService.processUserAnalysisJob(data, job.id, {
|
|
isFinalAttempt: this.isFinalAttempt(job),
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (job.name === FEATURE_RECOMPUTE_JOB_NAME) {
|
|
const data = this.parseFeatureRecomputeJobData(job.data);
|
|
await this.analysisPipelineService.processFeatureRecomputeJob(
|
|
data,
|
|
job.id,
|
|
{
|
|
isFinalAttempt: this.isFinalAttempt(job),
|
|
},
|
|
);
|
|
return;
|
|
}
|
|
|
|
throw new Error(`Unsupported analysis job name "${job.name}".`);
|
|
}
|
|
|
|
private parseUserAnalysisJobData(value: unknown): UserAnalysisJobData {
|
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
throw new Error('Analysis job data must be an object.');
|
|
}
|
|
|
|
const record = value as Record<string, unknown>;
|
|
const gameName = this.parseBoundedString(record.gameName, 'gameName', 64);
|
|
const tagLine = this.parseBoundedString(record.tagLine, 'tagLine', 16);
|
|
const platformRoute = this.parsePlatformRoute(record.platformRoute);
|
|
const regionalRoute = this.parseRegionalRoute(record.regionalRoute);
|
|
if (regionalRoute !== toRegionalRoute(platformRoute)) {
|
|
throw new Error(
|
|
'Analysis job regionalRoute does not match platformRoute.',
|
|
);
|
|
}
|
|
|
|
const queueId = this.parseQueueId(record.queueId);
|
|
const requestedMatchCount = this.parseRequestedMatchCount(
|
|
record.requestedMatchCount,
|
|
);
|
|
|
|
return {
|
|
gameName,
|
|
tagLine,
|
|
platformRoute,
|
|
regionalRoute,
|
|
queueId,
|
|
requestedMatchCount,
|
|
};
|
|
}
|
|
|
|
private parseFeatureRecomputeJobData(
|
|
value: unknown,
|
|
): FeatureRecomputeJobData {
|
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
throw new Error('Feature recompute job data must be an object.');
|
|
}
|
|
|
|
const record = value as Record<string, unknown>;
|
|
const platformRoute = this.parsePlatformRoute(record.platformRoute);
|
|
const queueId = this.parseQueueId(record.queueId);
|
|
const batchSize = this.parseFeatureRecomputeBatchSize(record.batchSize);
|
|
const puuid =
|
|
record.puuid === undefined
|
|
? undefined
|
|
: this.parseBoundedString(record.puuid, 'puuid', 128);
|
|
|
|
return {
|
|
platformRoute,
|
|
queueId,
|
|
batchSize,
|
|
...(puuid ? { puuid } : {}),
|
|
};
|
|
}
|
|
|
|
private parseBoundedString(
|
|
value: unknown,
|
|
fieldName: string,
|
|
maximumLength: number,
|
|
): string {
|
|
if (typeof value !== 'string') {
|
|
throw new Error(`Analysis job ${fieldName} must be a string.`);
|
|
}
|
|
const trimmed = value.trim();
|
|
if (trimmed.length === 0 || trimmed.length > maximumLength) {
|
|
throw new Error(`Analysis job ${fieldName} is out of range.`);
|
|
}
|
|
return trimmed;
|
|
}
|
|
|
|
private parsePlatformRoute(value: unknown): RiotPlatformRoute {
|
|
if (
|
|
typeof value !== 'string' ||
|
|
!RIOT_PLATFORM_ROUTES.includes(value as RiotPlatformRoute)
|
|
) {
|
|
throw new Error('Analysis job platformRoute is unsupported.');
|
|
}
|
|
return value as RiotPlatformRoute;
|
|
}
|
|
|
|
private parseRegionalRoute(value: unknown): RiotRegionalRoute {
|
|
if (
|
|
typeof value !== 'string' ||
|
|
!RIOT_REGIONAL_ROUTES.includes(value as RiotRegionalRoute)
|
|
) {
|
|
throw new Error('Analysis job regionalRoute is unsupported.');
|
|
}
|
|
return value as RiotRegionalRoute;
|
|
}
|
|
|
|
private parseQueueId(value: unknown): number {
|
|
if (value !== 420) {
|
|
throw new Error('Analysis job queueId is unsupported.');
|
|
}
|
|
return value;
|
|
}
|
|
|
|
private parseRequestedMatchCount(value: unknown): number {
|
|
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
throw new Error('Analysis job requestedMatchCount must be finite.');
|
|
}
|
|
return normalizeRequestedMatchCount(value);
|
|
}
|
|
|
|
private parseFeatureRecomputeBatchSize(value: unknown): number {
|
|
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
throw new Error('Feature recompute job batchSize must be finite.');
|
|
}
|
|
return normalizeFeatureRecomputeBatchSize(value);
|
|
}
|
|
|
|
private isFinalAttempt(job: Job<unknown>): boolean {
|
|
const attempts =
|
|
typeof job.opts?.attempts === 'number' && job.opts.attempts > 0
|
|
? job.opts.attempts
|
|
: 1;
|
|
return job.attemptsMade + 1 >= attempts;
|
|
}
|
|
}
|