Add analysis pipeline
This commit is contained in:
1 parent
256d776131
commit
770c198ccd
20 files changed
+2572
-1
No files matched your search
@@ -0,0 +1,127 @@
|
||||
import { Processor, WorkerHost } from '@nestjs/bullmq';
|
||||
import type { Job } from 'bullmq';
|
||||
import {
|
||||
ANALYSIS_QUEUE_NAME,
|
||||
normalizeRequestedMatchCount,
|
||||
USER_ANALYSIS_JOB_NAME,
|
||||
} from './analysis.constants';
|
||||
import { AnalysisPipelineService } from './analysis-pipeline.service';
|
||||
import type { 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) {
|
||||
throw new Error(`Unsupported analysis job name "${job.name}".`);
|
||||
}
|
||||
|
||||
const data = this.parseUserAnalysisJobData(job.data);
|
||||
await this.analysisPipelineService.processUserAnalysisJob(data, job.id, {
|
||||
isFinalAttempt: this.isFinalAttempt(job),
|
||||
});
|
||||
}
|
||||
|
||||
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 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 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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user