492 lines
14 KiB
TypeScript
492 lines
14 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
HttpException,
|
|
HttpStatus,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import { randomUUID } from 'crypto';
|
|
import mongoose from 'mongoose';
|
|
import { ConnectionService } from '../global/database/connection.service';
|
|
import type {
|
|
CommunityDocument,
|
|
RiotAccount,
|
|
UserDocument,
|
|
} from '../global/database/schema';
|
|
import { RedisService } from '../global/redis/redis.service';
|
|
import {
|
|
isRiotQueueGroup,
|
|
resolveRiotQueueIds,
|
|
type RiotQueueGroup,
|
|
} from '../riot/riot-queue.constants';
|
|
import {
|
|
EnqueueRefreshResult,
|
|
RIOT_JOB_AVG_SECONDS,
|
|
RIOT_JOB_DEDUP_TTL_SECONDS,
|
|
RIOT_JOB_HIGH_COOLDOWN_SECONDS,
|
|
RIOT_JOB_HIGH_STREAM,
|
|
RIOT_JOB_LOW_COOLDOWN_SECONDS,
|
|
RIOT_JOB_LOW_STREAM,
|
|
RIOT_JOB_SCHEDULED_ZSET,
|
|
RIOT_JOB_STATUS_TTL_SECONDS,
|
|
RiotJobPriority,
|
|
RiotJobState,
|
|
RiotJobStatus,
|
|
RiotParentJobStatus,
|
|
RiotRefreshEvent,
|
|
RiotRefreshSubJob,
|
|
} from './riot-job-queue.types';
|
|
import type {
|
|
EnqueueTournamentResultPollResult,
|
|
PollTournamentResultJob,
|
|
} from '../tournament-result/tournament-result.types';
|
|
|
|
interface RefreshContext {
|
|
community: CommunityDocument;
|
|
user: UserDocument;
|
|
account: RiotAccount;
|
|
}
|
|
|
|
@Injectable()
|
|
export class RiotJobQueueService {
|
|
constructor(
|
|
private readonly connectionService: ConnectionService,
|
|
private readonly redisService: RedisService,
|
|
) {}
|
|
|
|
public async enqueueRefresh(params: {
|
|
communityId: mongoose.Types.ObjectId;
|
|
userId: mongoose.Types.ObjectId;
|
|
queueGroups: string[];
|
|
priority: RiotJobPriority;
|
|
requestedBy?: string;
|
|
}): Promise<EnqueueRefreshResult> {
|
|
const context = await this.resolveContext(params);
|
|
const queueGroups = this.normalizeQueueGroups(params.queueGroups);
|
|
const stream = this.resolveStream(params.priority);
|
|
const streamLength = await this.redisService.streamLength(stream);
|
|
const dedupKeys = queueGroups.map((queueGroup) =>
|
|
this.dedupKey(context.account.puuid, queueGroup),
|
|
);
|
|
const existingJobIds = await Promise.all(
|
|
dedupKeys.map((key) => this.redisService.getString(key)),
|
|
);
|
|
const needsNewJob = existingJobIds.some((jobId) => !jobId);
|
|
|
|
if (params.priority === 'high' && needsNewJob) {
|
|
this.assertHighQueueAccepting(streamLength);
|
|
this.assertRefreshBudget(context.account.lastRefreshRequestedAt, 'high');
|
|
}
|
|
if (params.priority === 'low' && needsNewJob) {
|
|
this.assertRefreshBudget(context.account.lastRefreshRequestedAt, 'low');
|
|
}
|
|
|
|
const parentJobId = randomUUID();
|
|
const subJobs: EnqueueRefreshResult['subJobs'] = [];
|
|
|
|
for (let index = 0; index < queueGroups.length; index += 1) {
|
|
const queueGroup = queueGroups[index];
|
|
const existingJobId = existingJobIds[index];
|
|
const queuePosition = streamLength + subJobs.length + 1;
|
|
|
|
if (existingJobId) {
|
|
const existingStatus = await this.getStatus(existingJobId);
|
|
subJobs.push({
|
|
jobId: existingJobId,
|
|
queueGroup,
|
|
alreadyQueued: true,
|
|
etaSeconds:
|
|
existingStatus?.etaSeconds ?? queuePosition * RIOT_JOB_AVG_SECONDS,
|
|
queuePosition: existingStatus?.queuePosition ?? queuePosition,
|
|
});
|
|
continue;
|
|
}
|
|
|
|
const job: RiotRefreshSubJob = {
|
|
jobId: randomUUID(),
|
|
parentJobId,
|
|
userId: context.user._id.toString(),
|
|
puuid: context.account.puuid,
|
|
region: context.community.riotRegion ?? 'asia',
|
|
platform: context.community.riotPlatform ?? 'kr',
|
|
queueGroup,
|
|
queueIds: resolveRiotQueueIds(queueGroup, 'all'),
|
|
requestedAt: Date.now(),
|
|
requestedBy: params.requestedBy,
|
|
priority: params.priority,
|
|
};
|
|
const acquired = await this.redisService.setStringIfAbsent(
|
|
dedupKeys[index],
|
|
job.jobId,
|
|
RIOT_JOB_DEDUP_TTL_SECONDS,
|
|
);
|
|
if (!acquired) {
|
|
const jobId = await this.redisService.getString(dedupKeys[index]);
|
|
if (!jobId) continue;
|
|
const status = await this.getStatus(jobId);
|
|
subJobs.push({
|
|
jobId,
|
|
queueGroup,
|
|
alreadyQueued: true,
|
|
etaSeconds:
|
|
status?.etaSeconds ?? queuePosition * RIOT_JOB_AVG_SECONDS,
|
|
queuePosition: status?.queuePosition ?? queuePosition,
|
|
});
|
|
continue;
|
|
}
|
|
|
|
const status: RiotJobStatus = {
|
|
jobId: job.jobId,
|
|
parentJobId,
|
|
state: 'queued',
|
|
priority: params.priority,
|
|
queueGroup,
|
|
queueIds: job.queueIds,
|
|
puuid: job.puuid,
|
|
userId: job.userId,
|
|
enqueuedAt: new Date(job.requestedAt).toISOString(),
|
|
etaSeconds: queuePosition * RIOT_JOB_AVG_SECONDS,
|
|
queuePosition,
|
|
};
|
|
|
|
await Promise.all([
|
|
this.setStatus(status),
|
|
this.redisService.appendStream(
|
|
stream,
|
|
{ job: JSON.stringify(job) },
|
|
{ maxLen: 10_000 },
|
|
),
|
|
]);
|
|
subJobs.push({
|
|
jobId: job.jobId,
|
|
queueGroup,
|
|
alreadyQueued: false,
|
|
etaSeconds: status.etaSeconds,
|
|
queuePosition,
|
|
});
|
|
}
|
|
|
|
await this.setParent(
|
|
parentJobId,
|
|
subJobs.map((job) => job.jobId),
|
|
);
|
|
if (params.priority === 'high' && needsNewJob) {
|
|
await this.markRefreshRequested(context.user._id, context.account.puuid);
|
|
}
|
|
|
|
return {
|
|
parentJobId,
|
|
subJobs,
|
|
totalEtaSeconds: subJobs.reduce((sum, job) => sum + job.etaSeconds, 0),
|
|
};
|
|
}
|
|
|
|
public async enqueueTournamentResultPoll(params: {
|
|
matchId: string;
|
|
attempt: number;
|
|
expandedToAllPuuids: boolean;
|
|
priority: RiotJobPriority;
|
|
deferUntilMs?: number;
|
|
}): Promise<EnqueueTournamentResultPollResult> {
|
|
const job: PollTournamentResultJob = {
|
|
kind: 'poll-tournament-result',
|
|
jobId: randomUUID(),
|
|
matchId: params.matchId,
|
|
attempt: params.attempt,
|
|
expandedToAllPuuids: params.expandedToAllPuuids,
|
|
enqueuedAt: Date.now(),
|
|
priority: params.priority,
|
|
};
|
|
|
|
if (params.deferUntilMs && params.deferUntilMs > Date.now()) {
|
|
await this.scheduleJob(job, params.deferUntilMs);
|
|
} else {
|
|
await this.appendJob(job);
|
|
}
|
|
|
|
return {
|
|
jobId: job.jobId,
|
|
matchId: job.matchId,
|
|
priority: job.priority,
|
|
attempt: job.attempt,
|
|
expandedToAllPuuids: job.expandedToAllPuuids,
|
|
};
|
|
}
|
|
|
|
public async promoteDueScheduledJobs(limit = 100): Promise<number> {
|
|
const client = this.redisService.raw();
|
|
const entries = await client.sendCommand<string[]>([
|
|
'ZRANGEBYSCORE',
|
|
RIOT_JOB_SCHEDULED_ZSET,
|
|
'-inf',
|
|
String(Date.now()),
|
|
'LIMIT',
|
|
'0',
|
|
String(limit),
|
|
]);
|
|
let promoted = 0;
|
|
|
|
for (const entry of entries) {
|
|
const removed = await client.sendCommand<number>([
|
|
'ZREM',
|
|
RIOT_JOB_SCHEDULED_ZSET,
|
|
entry,
|
|
]);
|
|
if (removed !== 1) continue;
|
|
const job = this.parseScheduledJob(entry);
|
|
if (!job) continue;
|
|
await this.appendJob(job);
|
|
promoted += 1;
|
|
}
|
|
|
|
return promoted;
|
|
}
|
|
|
|
public async getStatus(jobId: string): Promise<RiotJobStatus | null> {
|
|
return this.redisService.getJson<RiotJobStatus>(this.statusKey(jobId));
|
|
}
|
|
|
|
public async getParentStatus(
|
|
parentJobId: string,
|
|
): Promise<RiotParentJobStatus | null> {
|
|
const jobIds = await this.redisService.getJson<string[]>(
|
|
this.parentKey(parentJobId),
|
|
);
|
|
if (!jobIds) return null;
|
|
|
|
const subJobs = (
|
|
await Promise.all(jobIds.map((jobId) => this.getStatus(jobId)))
|
|
).filter((status): status is RiotJobStatus => Boolean(status));
|
|
const done = subJobs.filter(
|
|
(job) => job.state === 'completed' || job.state === 'failed',
|
|
).length;
|
|
const state = this.resolveParentState(subJobs);
|
|
|
|
return {
|
|
parentJobId,
|
|
state,
|
|
done,
|
|
total: subJobs.length,
|
|
etaSeconds: subJobs
|
|
.filter((job) => job.state === 'queued' || job.state === 'processing')
|
|
.reduce((sum, job) => sum + job.etaSeconds, 0),
|
|
subJobs,
|
|
};
|
|
}
|
|
|
|
public async markProcessing(job: RiotRefreshSubJob): Promise<RiotJobStatus> {
|
|
const status = await this.requireStatus(job.jobId);
|
|
const next: RiotJobStatus = {
|
|
...status,
|
|
state: 'processing',
|
|
startedAt: new Date().toISOString(),
|
|
etaSeconds: RIOT_JOB_AVG_SECONDS,
|
|
queuePosition: 0,
|
|
};
|
|
await this.setStatus(next);
|
|
return next;
|
|
}
|
|
|
|
public async markCompleted(
|
|
job: RiotRefreshSubJob,
|
|
): Promise<RiotRefreshEvent> {
|
|
const status = await this.requireStatus(job.jobId);
|
|
const next: RiotJobStatus = {
|
|
...status,
|
|
state: 'completed',
|
|
finishedAt: new Date().toISOString(),
|
|
etaSeconds: 0,
|
|
queuePosition: 0,
|
|
};
|
|
await Promise.all([
|
|
this.setStatus(next),
|
|
this.redisService.delete(this.dedupKey(job.puuid, job.queueGroup)),
|
|
]);
|
|
return this.toEvent(next);
|
|
}
|
|
|
|
public async markFailed(
|
|
job: RiotRefreshSubJob,
|
|
error: unknown,
|
|
): Promise<RiotRefreshEvent> {
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
const status = await this.requireStatus(job.jobId);
|
|
const next: RiotJobStatus = {
|
|
...status,
|
|
state: 'failed',
|
|
finishedAt: new Date().toISOString(),
|
|
error: message,
|
|
etaSeconds: 0,
|
|
queuePosition: 0,
|
|
};
|
|
await Promise.all([
|
|
this.setStatus(next),
|
|
this.redisService.delete(this.dedupKey(job.puuid, job.queueGroup)),
|
|
]);
|
|
return this.toEvent(next);
|
|
}
|
|
|
|
private async resolveContext(params: {
|
|
communityId: mongoose.Types.ObjectId;
|
|
userId: mongoose.Types.ObjectId;
|
|
}): Promise<RefreshContext> {
|
|
const UserModel = await this.connectionService.getModel('user');
|
|
const CommunityModel = await this.connectionService.getModel('community');
|
|
const [user, community] = await Promise.all([
|
|
UserModel.findById(params.userId).exec(),
|
|
CommunityModel.findById(params.communityId).exec(),
|
|
]);
|
|
if (!community) throw new NotFoundException('Community not found.');
|
|
if (!user || !user.communityId.equals(params.communityId)) {
|
|
throw new NotFoundException('User not found.');
|
|
}
|
|
|
|
const account =
|
|
user.riotAccounts.find((riotAccount) => riotAccount.isPrimary) ??
|
|
user.riotAccounts[0];
|
|
if (!account) throw new NotFoundException('Riot account not found.');
|
|
return { community, user, account };
|
|
}
|
|
|
|
private normalizeQueueGroups(queueGroups: string[]): RiotQueueGroup[] {
|
|
const values = queueGroups.length > 0 ? queueGroups : ['summoners-rift'];
|
|
const unique = [...new Set(values)];
|
|
if (!unique.every(isRiotQueueGroup)) {
|
|
throw new BadRequestException('Invalid queueGroups.');
|
|
}
|
|
return unique;
|
|
}
|
|
|
|
private assertHighQueueAccepting(streamLength: number): void {
|
|
if (streamLength <= 50) return;
|
|
throw new HttpException(
|
|
{
|
|
reason: 'queue-full',
|
|
retryAfterSeconds: RIOT_JOB_AVG_SECONDS * streamLength,
|
|
},
|
|
HttpStatus.TOO_MANY_REQUESTS,
|
|
);
|
|
}
|
|
|
|
private assertRefreshBudget(
|
|
lastRequestedAt: Date | undefined,
|
|
priority: RiotJobPriority,
|
|
): void {
|
|
if (!lastRequestedAt) return;
|
|
const cooldownSeconds =
|
|
priority === 'high'
|
|
? RIOT_JOB_HIGH_COOLDOWN_SECONDS
|
|
: RIOT_JOB_LOW_COOLDOWN_SECONDS;
|
|
const elapsedSeconds = (Date.now() - lastRequestedAt.getTime()) / 1000;
|
|
if (elapsedSeconds >= cooldownSeconds) return;
|
|
throw new HttpException(
|
|
{
|
|
reason: 'cooldown',
|
|
retryAfterSeconds: Math.ceil(cooldownSeconds - elapsedSeconds),
|
|
},
|
|
HttpStatus.TOO_MANY_REQUESTS,
|
|
);
|
|
}
|
|
|
|
private async markRefreshRequested(
|
|
userId: mongoose.Types.ObjectId,
|
|
puuid: string,
|
|
): Promise<void> {
|
|
const UserModel = await this.connectionService.getModel('user');
|
|
await UserModel.updateOne(
|
|
{ _id: userId, 'riotAccounts.puuid': puuid },
|
|
{ $set: { 'riotAccounts.$.lastRefreshRequestedAt': new Date() } },
|
|
).exec();
|
|
}
|
|
|
|
private resolveStream(priority: RiotJobPriority): string {
|
|
return priority === 'high' ? RIOT_JOB_HIGH_STREAM : RIOT_JOB_LOW_STREAM;
|
|
}
|
|
|
|
private async appendJob(job: RiotRefreshSubJob | PollTournamentResultJob) {
|
|
await this.redisService.appendStream(
|
|
this.resolveStream(job.priority),
|
|
{ job: JSON.stringify(job) },
|
|
{ maxLen: 10_000 },
|
|
);
|
|
}
|
|
|
|
private async scheduleJob(
|
|
job: PollTournamentResultJob,
|
|
deferUntilMs: number,
|
|
): Promise<void> {
|
|
await this.redisService
|
|
.raw()
|
|
.sendCommand([
|
|
'ZADD',
|
|
RIOT_JOB_SCHEDULED_ZSET,
|
|
String(deferUntilMs),
|
|
JSON.stringify(job),
|
|
]);
|
|
}
|
|
|
|
private parseScheduledJob(value: string): PollTournamentResultJob | null {
|
|
try {
|
|
const parsed = JSON.parse(value) as PollTournamentResultJob;
|
|
return parsed.kind === 'poll-tournament-result' ? parsed : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private resolveParentState(subJobs: RiotJobStatus[]): RiotJobState {
|
|
if (subJobs.some((job) => job.state === 'processing')) return 'processing';
|
|
if (subJobs.some((job) => job.state === 'queued')) return 'queued';
|
|
if (subJobs.some((job) => job.state === 'failed')) return 'failed';
|
|
return 'completed';
|
|
}
|
|
|
|
private async requireStatus(jobId: string): Promise<RiotJobStatus> {
|
|
const status = await this.getStatus(jobId);
|
|
if (!status) throw new Error(`Missing riot job status: ${jobId}`);
|
|
return status;
|
|
}
|
|
|
|
private async setStatus(status: RiotJobStatus): Promise<void> {
|
|
await this.redisService.setJson(
|
|
this.statusKey(status.jobId),
|
|
status,
|
|
RIOT_JOB_STATUS_TTL_SECONDS,
|
|
);
|
|
}
|
|
|
|
private async setParent(
|
|
parentJobId: string,
|
|
jobIds: string[],
|
|
): Promise<void> {
|
|
await this.redisService.setJson(
|
|
this.parentKey(parentJobId),
|
|
jobIds,
|
|
RIOT_JOB_STATUS_TTL_SECONDS,
|
|
);
|
|
}
|
|
|
|
private toEvent(status: RiotJobStatus): RiotRefreshEvent {
|
|
return {
|
|
parentJobId: status.parentJobId,
|
|
jobId: status.jobId,
|
|
queueGroup: status.queueGroup,
|
|
puuid: status.puuid,
|
|
state: status.state,
|
|
error: status.error,
|
|
};
|
|
}
|
|
|
|
private statusKey(jobId: string): string {
|
|
return `riot:job:status:${jobId}`;
|
|
}
|
|
|
|
private parentKey(parentJobId: string): string {
|
|
return `riot:job:parent:${parentJobId}`;
|
|
}
|
|
|
|
private dedupKey(puuid: string, queueGroup: RiotQueueGroup): string {
|
|
return `riot:job:dedup:${puuid}:${queueGroup}`;
|
|
}
|
|
}
|