This commit is contained in:
dahyeon committed 2026-04-26 06:48:32 -07:00
1 parent ca56af79e5
commit 8b8dec4c44
14 files changed
+1155 -7

No files matched your search

@@ -9,6 +9,49 @@ import { GARNET_CLIENT } from './garnet.constants';
export type GarnetRedisClient = ReturnType<typeof createClient>;
export interface StreamEntry {
deliveriesCounter?: number;
id: string;
message: Record<string, string>;
millisElapsedFromDelivery?: number;
}
export interface StreamReadResult {
messages: StreamEntry[];
name: string;
}
interface RawStreamMessage {
deliveriesCounter?: number;
id: string;
message: unknown;
millisElapsedFromDelivery?: number;
}
interface RawStreamReadResult {
messages: RawStreamMessage[];
name: string;
}
function normalizeStreamFields(fields: unknown): Record<string, string> {
if (fields instanceof Map) {
return Object.fromEntries(
[...fields.entries()].map(([key, value]) => [String(key), String(value)]),
);
}
if (typeof fields === 'object' && fields !== null) {
return Object.fromEntries(
Object.entries(fields as Record<string, unknown>).map(([key, value]) => [
key,
String(value),
]),
);
}
return {};
}
@Injectable()
export class GarnetService implements OnModuleDestroy {
private readonly logger = new Logger(GarnetService.name);
@@ -87,4 +130,84 @@ export class GarnetService implements OnModuleDestroy {
public async delete(key: string): Promise<number> {
return this.client.del(key);
}
public async appendStream(
key: string,
fields: Record<string, string>,
options?: { maxLen?: number },
): Promise<string> {
const trimOptions =
options?.maxLen && options.maxLen > 0
? {
TRIM: {
strategy: 'MAXLEN' as const,
strategyModifier: '~' as const,
threshold: options.maxLen,
},
}
: undefined;
return this.client.xAdd(key, '*', fields, trimOptions);
}
public async createConsumerGroup(
key: string,
group: string,
startId = '$',
): Promise<void> {
await this.client.xGroupCreate(key, group, startId, { MKSTREAM: true });
}
public async readGroup(
key: string,
group: string,
consumer: string,
startId: string,
options?: { blockMs?: number; count?: number },
): Promise<StreamReadResult[] | null> {
const readOptions: { BLOCK?: number; COUNT?: number } = {};
if (options?.blockMs !== undefined) readOptions.BLOCK = options.blockMs;
if (options?.count !== undefined) readOptions.COUNT = options.count;
const reply = await this.client.xReadGroup(
group,
consumer,
{ key, id: startId },
readOptions,
);
if (!reply) return null;
return (reply as RawStreamReadResult[]).map((stream) => ({
name: String(stream.name),
messages: stream.messages.map((message) => ({
deliveriesCounter:
typeof message.deliveriesCounter === 'number'
? message.deliveriesCounter
: undefined,
id: String(message.id),
message: normalizeStreamFields(message.message),
millisElapsedFromDelivery:
typeof message.millisElapsedFromDelivery === 'number'
? message.millisElapsedFromDelivery
: undefined,
})),
}));
}
public async acknowledgeStream(
key: string,
group: string,
ids: string | string[],
): Promise<number> {
return this.client.xAck(key, group, ids);
}
public async streamLength(key: string): Promise<number> {
return this.client.xLen(key);
}
public async publish(channel: string, message: string): Promise<number> {
return this.client.publish(channel, message);
}
}
@@ -1,12 +1,24 @@
import { Controller, Get, Param, Query } from '@nestjs/common';
import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
import mongoose from 'mongoose';
import { AllowPublicAccess } from '../auth/allow-public-access.decorator';
import { ParseObjectIdPipe } from '../common/parse-object-id.pipe';
import { RiotJobQueueService } from '../riot-job-queue/riot-job-queue.service';
import type {
RiotJobPriority,
} from '../riot-job-queue/riot-job-queue.types';
import { MatchHistoryService } from './match-history.service';
interface RefreshBody {
queueGroups?: string[];
priority?: RiotJobPriority;
}
@Controller('communities/:communityId/users/:userId')
export class MatchHistoryController {
constructor(private readonly matchHistoryService: MatchHistoryService) {}
constructor(
private readonly matchHistoryService: MatchHistoryService,
private readonly riotJobQueueService: RiotJobQueueService,
) {}
@Get('history-profile')
@AllowPublicAccess()
@@ -87,6 +99,22 @@ export class MatchHistoryController {
});
}
@Post('refresh')
@AllowPublicAccess()
public refresh(
@Param('communityId', ParseObjectIdPipe)
communityId: mongoose.Types.ObjectId,
@Param('userId', ParseObjectIdPipe) userId: mongoose.Types.ObjectId,
@Body() body: RefreshBody,
) {
return this.riotJobQueueService.enqueueRefresh({
communityId,
userId,
queueGroups: body.queueGroups ?? ['summoners-rift'],
priority: body.priority ?? 'high',
});
}
private toNumber(value: string | undefined): number | undefined {
if (!value) return undefined;
const parsed = Number(value);
@@ -1,8 +1,10 @@
import { Module } from '@nestjs/common';
import { RiotJobQueueModule } from '../riot-job-queue/riot-job-queue.module';
import { MatchHistoryController } from './match-history.controller';
import { MatchHistoryService } from './match-history.service';
@Module({
imports: [RiotJobQueueModule],
controllers: [MatchHistoryController],
providers: [MatchHistoryService],
exports: [MatchHistoryService],
@@ -0,0 +1,24 @@
import { Controller, Get, NotFoundException, Param } from '@nestjs/common';
import { AllowPublicAccess } from '../auth/allow-public-access.decorator';
import { RiotJobQueueService } from './riot-job-queue.service';
@Controller('riot/jobs')
export class RiotJobQueueController {
constructor(private readonly jobQueueService: RiotJobQueueService) {}
@Get('parents/:parentJobId')
@AllowPublicAccess()
public async getParent(@Param('parentJobId') parentJobId: string) {
const status = await this.jobQueueService.getParentStatus(parentJobId);
if (!status) throw new NotFoundException('Riot parent job not found.');
return status;
}
@Get(':jobId')
@AllowPublicAccess()
public async getJob(@Param('jobId') jobId: string) {
const status = await this.jobQueueService.getStatus(jobId);
if (!status) throw new NotFoundException('Riot job not found.');
return status;
}
}
@@ -0,0 +1,14 @@
import { Module } from '@nestjs/common';
import { RiotModule } from '../riot/riot.module';
import { RiotJobQueueController } from './riot-job-queue.controller';
import { RiotJobQueueService } from './riot-job-queue.service';
import { RiotJobWorkerService } from './riot-job-worker.service';
import { RiotRefreshGateway } from './riot-refresh.gateway';
@Module({
imports: [RiotModule],
controllers: [RiotJobQueueController],
providers: [RiotJobQueueService, RiotJobWorkerService, RiotRefreshGateway],
exports: [RiotJobQueueService],
})
export class RiotJobQueueModule {}
@@ -0,0 +1,386 @@
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 { GarnetService } from '../global/garnet/garnet.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_STATUS_TTL_SECONDS,
RiotJobPriority,
RiotJobState,
RiotJobStatus,
RiotParentJobStatus,
RiotRefreshEvent,
RiotRefreshSubJob,
} from './riot-job-queue.types';
interface RefreshContext {
community: CommunityDocument;
user: UserDocument;
account: RiotAccount;
}
@Injectable()
export class RiotJobQueueService {
constructor(
private readonly connectionService: ConnectionService,
private readonly garnetService: GarnetService,
) {}
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.garnetService.streamLength(stream);
const dedupKeys = queueGroups.map((queueGroup) =>
this.dedupKey(context.account.puuid, queueGroup),
);
const existingJobIds = await Promise.all(
dedupKeys.map((key) => this.garnetService.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.garnetService.setStringIfAbsent(
dedupKeys[index],
job.jobId,
RIOT_JOB_DEDUP_TTL_SECONDS,
);
if (!acquired) {
const jobId = await this.garnetService.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.garnetService.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 getStatus(jobId: string): Promise<RiotJobStatus | null> {
return this.garnetService.getJson<RiotJobStatus>(this.statusKey(jobId));
}
public async getParentStatus(
parentJobId: string,
): Promise<RiotParentJobStatus | null> {
const jobIds = await this.garnetService.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.garnetService.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.garnetService.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 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.garnetService.setJson(
this.statusKey(status.jobId),
status,
RIOT_JOB_STATUS_TTL_SECONDS,
);
}
private async setParent(parentJobId: string, jobIds: string[]): Promise<void> {
await this.garnetService.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}`;
}
}
@@ -0,0 +1,79 @@
import type {
RiotPlatformRoute,
RiotRegionalRoute,
} from '../riot/riot.types';
import type { RiotQueueGroup } from '../riot/riot-queue.constants';
export const RIOT_JOB_HIGH_STREAM = 'riot:queue:high';
export const RIOT_JOB_LOW_STREAM = 'riot:queue:low';
export const RIOT_JOB_GROUP = 'riot-refresh-worker';
export const RIOT_JOB_STATUS_TTL_SECONDS = 24 * 60 * 60;
export const RIOT_JOB_DEDUP_TTL_SECONDS = 5 * 60;
export const RIOT_JOB_HIGH_COOLDOWN_SECONDS = 5 * 60;
export const RIOT_JOB_LOW_COOLDOWN_SECONDS = 60 * 60;
export const RIOT_JOB_AVG_SECONDS = 20;
export type RiotJobPriority = 'high' | 'low';
export type RiotJobState = 'queued' | 'processing' | 'completed' | 'failed';
export interface RiotRefreshSubJob {
jobId: string;
parentJobId: string;
userId: string;
puuid: string;
region: RiotRegionalRoute;
platform: RiotPlatformRoute;
queueGroup: RiotQueueGroup;
queueIds: number[];
requestedAt: number;
requestedBy?: string;
priority: RiotJobPriority;
}
export interface RiotJobStatus {
jobId: string;
parentJobId: string;
state: RiotJobState;
priority: RiotJobPriority;
queueGroup: RiotQueueGroup;
queueIds: number[];
puuid: string;
userId: string;
enqueuedAt: string;
startedAt?: string;
finishedAt?: string;
error?: string;
etaSeconds: number;
queuePosition: number;
alreadyQueued?: boolean;
}
export interface RiotParentJobStatus {
parentJobId: string;
state: RiotJobState;
done: number;
total: number;
etaSeconds: number;
subJobs: RiotJobStatus[];
}
export interface RiotRefreshEvent {
parentJobId: string;
jobId: string;
queueGroup: RiotQueueGroup;
puuid: string;
state: RiotJobState;
error?: string;
}
export interface EnqueueRefreshResult {
parentJobId: string;
subJobs: Array<{
jobId: string;
queueGroup: RiotQueueGroup;
alreadyQueued: boolean;
etaSeconds: number;
queuePosition: number;
}>;
totalEtaSeconds: number;
}
@@ -0,0 +1,181 @@
import {
Injectable,
Logger,
OnModuleDestroy,
OnModuleInit,
} from '@nestjs/common';
import mongoose from 'mongoose';
import { ConnectionService } from '../global/database/connection.service';
import { GarnetService, StreamEntry } from '../global/garnet/garnet.service';
import { RiotRecentMatchesService } from '../riot/riot-recent-matches.service';
import {
RIOT_JOB_GROUP,
RIOT_JOB_HIGH_STREAM,
RIOT_JOB_LOW_STREAM,
RiotRefreshSubJob,
} from './riot-job-queue.types';
import { RiotJobQueueService } from './riot-job-queue.service';
import { RiotRefreshGateway } from './riot-refresh.gateway';
const LEADER_KEY = 'riot:worker:leader';
const LEADER_TTL_SECONDS = 60;
@Injectable()
export class RiotJobWorkerService implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(RiotJobWorkerService.name);
private readonly consumer = `${process.pid}:${Math.random().toString(36).slice(2)}`;
private stopped = false;
constructor(
private readonly garnetService: GarnetService,
private readonly jobQueueService: RiotJobQueueService,
private readonly recentMatchesService: RiotRecentMatchesService,
private readonly connectionService: ConnectionService,
private readonly refreshGateway: RiotRefreshGateway,
) {}
public async onModuleInit() {
await this.ensureConsumerGroups();
void this.loop();
}
public onModuleDestroy() {
this.stopped = true;
}
private async loop(): Promise<void> {
while (!this.stopped) {
try {
const isLeader = await this.tryBecomeLeader();
if (!isLeader) {
await this.sleep(1_000);
continue;
}
const entry =
(await this.readOne(RIOT_JOB_HIGH_STREAM, 100)) ??
(await this.readOne(RIOT_JOB_LOW_STREAM, 1_000));
if (!entry) continue;
await this.processEntry(entry.stream, entry.message);
} catch (error) {
this.logger.warn(
`Riot job worker loop failed: ${
error instanceof Error ? error.message : error
}`,
);
await this.sleep(1_000);
}
}
}
private async processEntry(stream: string, entry: StreamEntry): Promise<void> {
const job = this.parseJob(entry);
if (!job) {
await this.garnetService.acknowledgeStream(stream, RIOT_JOB_GROUP, entry.id);
return;
}
try {
await this.jobQueueService.markProcessing(job);
for (const queueId of job.queueIds) {
await this.recentMatchesService.ensureFreshForQueue({
puuid: job.puuid,
region: job.region,
queueId,
});
}
await this.markUserFetchedAt(job);
const event = await this.jobQueueService.markCompleted(job);
this.refreshGateway.broadcastRefreshEvent(event);
} catch (error) {
const event = await this.jobQueueService.markFailed(job, error);
this.refreshGateway.broadcastRefreshEvent(event);
this.logger.warn(
`Riot refresh job failed ${job.jobId}: ${
error instanceof Error ? error.message : error
}`,
);
} finally {
await this.garnetService.acknowledgeStream(stream, RIOT_JOB_GROUP, entry.id);
}
}
private async markUserFetchedAt(job: RiotRefreshSubJob): Promise<void> {
const UserModel = await this.connectionService.getModel('user');
const now = new Date();
const $set: Record<string, Date> = {
'riotAccounts.$.matchIdsFetchedAt': now,
};
for (const queueId of job.queueIds) {
$set[`riotAccounts.$.matchIdsByQueueFetchedAt.${queueId}`] = now;
}
await UserModel.updateOne(
{
_id: new mongoose.Types.ObjectId(job.userId),
'riotAccounts.puuid': job.puuid,
},
{ $set },
).exec();
}
private parseJob(entry: StreamEntry): RiotRefreshSubJob | null {
const raw = entry.message.job;
if (!raw) return null;
try {
return JSON.parse(raw) as RiotRefreshSubJob;
} catch {
return null;
}
}
private async readOne(
stream: string,
blockMs: number,
): Promise<{ stream: string; message: StreamEntry } | null> {
const result = await this.garnetService.readGroup(
stream,
RIOT_JOB_GROUP,
this.consumer,
'>',
{ count: 1, blockMs },
);
const message = result?.[0]?.messages[0];
return message ? { stream, message } : null;
}
private async ensureConsumerGroups(): Promise<void> {
await Promise.all(
[RIOT_JOB_HIGH_STREAM, RIOT_JOB_LOW_STREAM].map(async (stream) => {
try {
await this.garnetService.createConsumerGroup(stream, RIOT_JOB_GROUP);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
if (!message.includes('BUSYGROUP')) throw error;
}
}),
);
}
private async tryBecomeLeader(): Promise<boolean> {
const current = await this.garnetService.getString(LEADER_KEY);
if (current === this.consumer) {
await this.garnetService.setString(
LEADER_KEY,
this.consumer,
LEADER_TTL_SECONDS,
);
return true;
}
if (current) return false;
return this.garnetService.setStringIfAbsent(
LEADER_KEY,
this.consumer,
LEADER_TTL_SECONDS,
);
}
private sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
}
@@ -0,0 +1,78 @@
import { Logger } from '@nestjs/common';
import {
ConnectedSocket,
MessageBody,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';
import type { Namespace, Socket } from 'socket.io';
import type { RiotRefreshEvent } from './riot-job-queue.types';
interface SubscribePayload {
puuid?: string;
parentJobId?: string;
}
const MAX_ROOMS_PER_SOCKET = 5;
@WebSocketGateway({
namespace: '/riot-refresh',
cors: {
origin: true,
credentials: true,
allowedHeaders: ['Authorization', 'Content-Type', 'X-Auth-User-Id'],
},
})
export class RiotRefreshGateway {
@WebSocketServer()
private server!: Namespace;
private readonly logger = new Logger(RiotRefreshGateway.name);
@SubscribeMessage('subscribe')
public async handleSubscribe(
@ConnectedSocket() socket: Socket,
@MessageBody() payload: SubscribePayload,
) {
const rooms = this.resolveRooms(payload);
if (rooms.length === 0) {
socket.emit('error', { message: 'Invalid riot refresh subscription.' });
return;
}
const currentRooms = [...socket.rooms].filter((room) => room !== socket.id);
if (currentRooms.length + rooms.length > MAX_ROOMS_PER_SOCKET) {
socket.emit('error', { message: 'Too many riot refresh subscriptions.' });
return;
}
await Promise.all(rooms.map((room) => socket.join(room)));
socket.emit('subscribed', { rooms });
}
public broadcastRefreshEvent(event: RiotRefreshEvent): void {
const rooms = [
`puuid:${event.puuid}`,
`parent:${event.parentJobId}`,
`job:${event.jobId}`,
];
this.logger.debug(
`Broadcast riot refresh ${event.state}: ${event.jobId} (${event.queueGroup})`,
);
for (const room of rooms) {
this.server.to(room).emit('riot-refresh', event);
}
}
private resolveRooms(payload: SubscribePayload): string[] {
const rooms: string[] = [];
if (typeof payload?.puuid === 'string' && payload.puuid.trim()) {
rooms.push(`puuid:${payload.puuid.trim()}`);
}
if (typeof payload?.parentJobId === 'string' && payload.parentJobId.trim()) {
rooms.push(`parent:${payload.parentJobId.trim()}`);
}
return rooms;
}
}
@@ -105,6 +105,15 @@ export class RiotRecentMatchesService {
}
}
public async ensureFreshForQueue(params: {
puuid: string;
region?: RiotRegionalRoute;
queueId: number;
limit?: number;
}): Promise<RiotMatchDocument[]> {
return this.syncRecentMatches(params);
}
public async loadFromDb(params: {
puuid: string;
limit?: number;
+21 -1
View File
@@ -1,6 +1,6 @@
import { InteractionStatus } from '@azure/msal-browser';
import { useMsal } from '@azure/msal-react';
import { useInfiniteQuery, useQuery } from '@tanstack/react-query';
import { useInfiniteQuery, useMutation, useQuery } from '@tanstack/react-query';
import { DEFAULT_COMMUNITY_ID, useAuthUserId } from '../config';
import { apiFetch } from '../lib/api';
import {
@@ -11,6 +11,7 @@ import type {
ApiAuction,
ApiChampionStatsResponse,
ApiCommunity,
ApiEnqueueRefreshResult,
ApiMatch,
ApiRecentMatchesResponse,
ApiRecentSummaryResponse,
@@ -206,6 +207,25 @@ export function useRecentMatches(
});
}
export function useRefreshUserHistory(
userId: string | undefined,
communityId = DEFAULT_COMMUNITY_ID,
) {
return useMutation({
mutationFn: (body: {
queueGroups: RiotQueueGroup[];
priority: 'high' | 'low';
}) =>
apiFetch<ApiEnqueueRefreshResult>(
`/communities/${communityId}/users/${userId}/refresh`,
{
method: 'POST',
body: JSON.stringify(body),
},
),
});
}
export function useDdragonManifest(locale = 'ko_KR') {
return useQuery({
queryKey: ['ddragon-manifest', locale],
+24
View File
@@ -390,3 +390,27 @@ export interface ApiRecentMatchesResponse {
lastFetchedAt: string | null;
season: ApiHistorySeason;
}
export type ApiRiotJobPriority = 'high' | 'low';
export type ApiRiotJobState = 'queued' | 'processing' | 'completed' | 'failed';
export interface ApiEnqueueRefreshResult {
parentJobId: string;
subJobs: Array<{
jobId: string;
queueGroup: RiotQueueGroup;
alreadyQueued: boolean;
etaSeconds: number;
queuePosition: number;
}>;
totalEtaSeconds: number;
}
export interface ApiRiotRefreshEvent {
parentJobId: string;
jobId: string;
queueGroup: RiotQueueGroup;
puuid: string;
state: ApiRiotJobState;
error?: string;
}
+73
View File
@@ -0,0 +1,73 @@
import { useEffect, useRef, useState } from 'react';
import { io, type Socket } from 'socket.io-client';
import { API_BASE_URL } from '../config';
import type { ApiRiotRefreshEvent } from './types';
type ConnectionStatus =
| 'idle'
| 'connecting'
| 'connected'
| 'disconnected'
| 'error';
export function useRiotRefreshSocket({
puuid,
parentJobId,
onEvent,
}: {
puuid?: string;
parentJobId?: string;
onEvent: (event: ApiRiotRefreshEvent) => void;
}) {
const socketRef = useRef<Socket | null>(null);
const onEventRef = useRef(onEvent);
const [connectionStatus, setConnectionStatus] =
useState<ConnectionStatus>('idle');
const [error, setError] = useState<string | null>(null);
useEffect(() => {
onEventRef.current = onEvent;
}, [onEvent]);
useEffect(() => {
if (!puuid && !parentJobId) {
setConnectionStatus('idle');
return;
}
setConnectionStatus('connecting');
setError(null);
const socket = io(`${API_BASE_URL}/riot-refresh`, {
transports: ['websocket'],
});
socketRef.current = socket;
socket.on('connect', () => {
setConnectionStatus('connected');
socket.emit('subscribe', { puuid, parentJobId });
});
socket.on('disconnect', () => {
setConnectionStatus('disconnected');
});
socket.on('riot-refresh', (event: ApiRiotRefreshEvent) => {
onEventRef.current(event);
});
socket.on('error', (payload: { message?: string }) => {
setConnectionStatus('error');
setError(payload.message ?? '전적 갱신 소켓 연결 중 오류가 발생했습니다.');
});
return () => {
socket.removeAllListeners();
socket.disconnect();
socketRef.current = null;
setConnectionStatus('idle');
};
}, [parentJobId, puuid]);
return { connectionStatus, error };
}
+111 -4
View File
@@ -1,25 +1,31 @@
import { useMemo, useState } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { Link, useParams, useSearchParams } from 'react-router';
import {
useChampionStats,
useDdragonManifest,
useRecentMatches,
useRecentSummary,
useRefreshUserHistory,
useUserHistoryProfile,
type UserHistoryFilters,
} from '../api/hooks';
import { useRiotRefreshSocket } from '../api/useRiotRefreshSocket';
import {
formatTierLabel,
getTierColorToken,
} from '../api/mappers';
import type {
ApiChampionStatsRow,
ApiEnqueueRefreshResult,
ApiRiotRefreshEvent,
ApiRecentMatchView,
DdragonManifest,
RiotQueueGroup,
RiotRankFilter,
} from '../api/types';
import { DEFAULT_COMMUNITY_ID } from '../config';
import { ApiError } from '../lib/api';
import {
championDisplayName,
championIconUrl,
@@ -68,6 +74,27 @@ function formatLastFetched(value: string | null | undefined) {
}).format(new Date(value));
}
function formatEta(seconds: number) {
if (seconds < 60) return `${Math.max(1, Math.ceil(seconds))}초`;
return `${Math.ceil(seconds / 60)}분`;
}
function formatRefreshError(error: unknown) {
if (error instanceof ApiError) {
const data = error.data;
if (
typeof data === 'object' &&
data !== null &&
'retryAfterSeconds' in data &&
typeof data.retryAfterSeconds === 'number'
) {
return `잠시 후 다시 시도해 주세요. 약 ${formatEta(data.retryAfterSeconds)} 뒤에 요청할 수 있습니다.`;
}
return error.message;
}
return error instanceof Error ? error.message : '갱신 요청에 실패했습니다.';
}
function ratioClass(value: number) {
if (value >= 55) return 'text-blue-400';
if (value >= 50) return 'text-amber-400';
@@ -428,9 +455,13 @@ function RecentMatchCard({
export default function UserHistoryPage() {
const { userId } = useParams();
const queryClient = useQueryClient();
const [searchParams, setSearchParams] = useSearchParams();
const [championSearch, setChampionSearch] = useState('');
const [sortKey, setSortKey] = useState<ChampionSortKey>('games');
const [refreshResult, setRefreshResult] =
useState<ApiEnqueueRefreshResult | null>(null);
const [refreshNotice, setRefreshNotice] = useState<string | null>(null);
const queueGroupParam = searchParams.get('queueGroup');
const rankParam = searchParams.get('rank');
const queueGroup: RiotQueueGroup = isRiotQueueGroup(queueGroupParam)
@@ -461,6 +492,7 @@ export default function UserHistoryPage() {
);
const summaryQuery = useRecentSummary(userId, filters, DEFAULT_COMMUNITY_ID);
const recentMatchesQuery = useRecentMatches(userId, filters, DEFAULT_COMMUNITY_ID);
const refreshMutation = useRefreshUserHistory(userId, DEFAULT_COMMUNITY_ID);
const profile = profileQuery.data;
const primaryTier =
@@ -485,6 +517,25 @@ export default function UserHistoryPage() {
.sort((left, right) => right[sortKey] - left[sortKey]);
}, [championSearch, championStatsQuery.data?.champions, ddragon, sortKey]);
const refreshSocket = useRiotRefreshSocket({
puuid: profile?.puuid,
parentJobId: refreshResult?.parentJobId,
onEvent: (event: ApiRiotRefreshEvent) => {
if (!profile || event.puuid !== profile.puuid) return;
void invalidateHistoryQueries();
if (event.state === 'completed') {
setRefreshNotice('갱신이 완료되어 전적을 다시 불러왔습니다.');
}
if (event.state === 'failed') {
setRefreshNotice(
event.error
? `갱신 실패: ${event.error}`
: '갱신에 실패했습니다. 잠시 후 다시 시도해 주세요.',
);
}
},
});
function updateSearch(next: Partial<Record<string, string | number | null>>) {
const params = new URLSearchParams(searchParams);
for (const [key, value] of Object.entries(next)) {
@@ -497,6 +548,44 @@ export default function UserHistoryPage() {
setSearchParams(params);
}
async function invalidateHistoryQueries() {
await Promise.all([
queryClient.invalidateQueries({
queryKey: ['user-history-profile', DEFAULT_COMMUNITY_ID, userId ?? null],
}),
queryClient.invalidateQueries({
queryKey: ['user-champion-stats', DEFAULT_COMMUNITY_ID, userId ?? null],
}),
queryClient.invalidateQueries({
queryKey: ['user-recent-summary', DEFAULT_COMMUNITY_ID, userId ?? null],
}),
queryClient.invalidateQueries({
queryKey: ['user-recent-matches', DEFAULT_COMMUNITY_ID, userId ?? null],
}),
]);
}
function handleRefresh() {
if (!userId) return;
refreshMutation.mutate(
{ queueGroups: [queueGroup], priority: 'high' },
{
onSuccess: (result) => {
setRefreshResult(result);
const alreadyQueued = result.subJobs.every((job) => job.alreadyQueued);
setRefreshNotice(
alreadyQueued
? `이미 갱신 작업이 대기 중입니다. 예상 ${formatEta(result.totalEtaSeconds)} 후 반영됩니다.`
: `갱신을 요청했습니다. 예상 ${formatEta(result.totalEtaSeconds)} 후 반영됩니다.`,
);
},
onError: (error) => {
setRefreshNotice(formatRefreshError(error));
},
},
);
}
if (!DEFAULT_COMMUNITY_ID) {
return (
<main className="max-w-6xl mx-auto px-4 py-20 text-center">
@@ -568,12 +657,30 @@ export default function UserHistoryPage() {
<span>최근 저장: {formatLastFetched(profile.lastFetchedAt)}</span>
</div>
</div>
<div className="border border-dashed border-base-content/10 px-3 py-2 text-[11px] text-base-content/35 sm:text-right">
이 페이지는 저장된 경기 DB만 조회합니다.
<br />
화면 진입 시 Riot Match API 호출은 없습니다.
<div className="border border-dashed border-base-content/10 px-3 py-2 text-[11px] text-base-content/35 sm:text-right space-y-2">
<div>
이 페이지는 저장된 경기 DB만 조회합니다.
<br />
Riot API 갱신은 버튼 클릭 시 큐에서 처리합니다.
</div>
<button
type="button"
className="btn btn-primary btn-xs px-3"
disabled={refreshMutation.isPending || !userId}
onClick={handleRefresh}
>
{refreshMutation.isPending ? '요청 중...' : '현재 탭 갱신'}
</button>
<div className="text-[10px] text-base-content/25">
갱신 소켓: {refreshSocket.connectionStatus}
</div>
</div>
</div>
{(refreshNotice || refreshSocket.error) && (
<div className="mt-4 border border-primary/15 bg-primary/5 px-3 py-2 text-xs text-base-content/55">
{refreshNotice ?? refreshSocket.error}
</div>
)}
</section>
<section className="border border-base-content/10 bg-base-100 mb-5">