feat: redesign auction engine with redis sot
This commit is contained in:
1 parent
737702d78c
commit
e8bb20d988
75 files changed
+12737
-542
No files matched your search
@@ -13,7 +13,7 @@ import type {
|
||||
RiotAccount,
|
||||
UserDocument,
|
||||
} from '../global/database/schema';
|
||||
import { GarnetService } from '../global/garnet/garnet.service';
|
||||
import { RedisService } from '../global/redis/redis.service';
|
||||
import {
|
||||
isRiotQueueGroup,
|
||||
resolveRiotQueueIds,
|
||||
@@ -51,7 +51,7 @@ interface RefreshContext {
|
||||
export class RiotJobQueueService {
|
||||
constructor(
|
||||
private readonly connectionService: ConnectionService,
|
||||
private readonly garnetService: GarnetService,
|
||||
private readonly redisService: RedisService,
|
||||
) {}
|
||||
|
||||
public async enqueueRefresh(params: {
|
||||
@@ -64,12 +64,12 @@ export class RiotJobQueueService {
|
||||
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 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.garnetService.getString(key)),
|
||||
dedupKeys.map((key) => this.redisService.getString(key)),
|
||||
);
|
||||
const needsNewJob = existingJobIds.some((jobId) => !jobId);
|
||||
|
||||
@@ -114,13 +114,13 @@ export class RiotJobQueueService {
|
||||
requestedBy: params.requestedBy,
|
||||
priority: params.priority,
|
||||
};
|
||||
const acquired = await this.garnetService.setStringIfAbsent(
|
||||
const acquired = await this.redisService.setStringIfAbsent(
|
||||
dedupKeys[index],
|
||||
job.jobId,
|
||||
RIOT_JOB_DEDUP_TTL_SECONDS,
|
||||
);
|
||||
if (!acquired) {
|
||||
const jobId = await this.garnetService.getString(dedupKeys[index]);
|
||||
const jobId = await this.redisService.getString(dedupKeys[index]);
|
||||
if (!jobId) continue;
|
||||
const status = await this.getStatus(jobId);
|
||||
subJobs.push({
|
||||
@@ -149,7 +149,7 @@ export class RiotJobQueueService {
|
||||
|
||||
await Promise.all([
|
||||
this.setStatus(status),
|
||||
this.garnetService.appendStream(
|
||||
this.redisService.appendStream(
|
||||
stream,
|
||||
{ job: JSON.stringify(job) },
|
||||
{ maxLen: 10_000 },
|
||||
@@ -209,7 +209,7 @@ export class RiotJobQueueService {
|
||||
}
|
||||
|
||||
public async promoteDueScheduledJobs(limit = 100): Promise<number> {
|
||||
const client = this.garnetService.raw();
|
||||
const client = this.redisService.raw();
|
||||
const raw = (await client.sendCommand([
|
||||
'ZRANGEBYSCORE',
|
||||
RIOT_JOB_SCHEDULED_ZSET,
|
||||
@@ -239,13 +239,13 @@ export class RiotJobQueueService {
|
||||
}
|
||||
|
||||
public async getStatus(jobId: string): Promise<RiotJobStatus | null> {
|
||||
return this.garnetService.getJson<RiotJobStatus>(this.statusKey(jobId));
|
||||
return this.redisService.getJson<RiotJobStatus>(this.statusKey(jobId));
|
||||
}
|
||||
|
||||
public async getParentStatus(
|
||||
parentJobId: string,
|
||||
): Promise<RiotParentJobStatus | null> {
|
||||
const jobIds = await this.garnetService.getJson<string[]>(
|
||||
const jobIds = await this.redisService.getJson<string[]>(
|
||||
this.parentKey(parentJobId),
|
||||
);
|
||||
if (!jobIds) return null;
|
||||
@@ -296,7 +296,7 @@ export class RiotJobQueueService {
|
||||
};
|
||||
await Promise.all([
|
||||
this.setStatus(next),
|
||||
this.garnetService.delete(this.dedupKey(job.puuid, job.queueGroup)),
|
||||
this.redisService.delete(this.dedupKey(job.puuid, job.queueGroup)),
|
||||
]);
|
||||
return this.toEvent(next);
|
||||
}
|
||||
@@ -317,7 +317,7 @@ export class RiotJobQueueService {
|
||||
};
|
||||
await Promise.all([
|
||||
this.setStatus(next),
|
||||
this.garnetService.delete(this.dedupKey(job.puuid, job.queueGroup)),
|
||||
this.redisService.delete(this.dedupKey(job.puuid, job.queueGroup)),
|
||||
]);
|
||||
return this.toEvent(next);
|
||||
}
|
||||
@@ -400,7 +400,7 @@ export class RiotJobQueueService {
|
||||
}
|
||||
|
||||
private async appendJob(job: RiotRefreshSubJob | PollTournamentResultJob) {
|
||||
await this.garnetService.appendStream(
|
||||
await this.redisService.appendStream(
|
||||
this.resolveStream(job.priority),
|
||||
{ job: JSON.stringify(job) },
|
||||
{ maxLen: 10_000 },
|
||||
@@ -411,7 +411,7 @@ export class RiotJobQueueService {
|
||||
job: PollTournamentResultJob,
|
||||
deferUntilMs: number,
|
||||
): Promise<void> {
|
||||
await this.garnetService.raw().sendCommand([
|
||||
await this.redisService.raw().sendCommand([
|
||||
'ZADD',
|
||||
RIOT_JOB_SCHEDULED_ZSET,
|
||||
String(deferUntilMs),
|
||||
@@ -442,7 +442,7 @@ export class RiotJobQueueService {
|
||||
}
|
||||
|
||||
private async setStatus(status: RiotJobStatus): Promise<void> {
|
||||
await this.garnetService.setJson(
|
||||
await this.redisService.setJson(
|
||||
this.statusKey(status.jobId),
|
||||
status,
|
||||
RIOT_JOB_STATUS_TTL_SECONDS,
|
||||
@@ -450,7 +450,7 @@ export class RiotJobQueueService {
|
||||
}
|
||||
|
||||
private async setParent(parentJobId: string, jobIds: string[]): Promise<void> {
|
||||
await this.garnetService.setJson(
|
||||
await this.redisService.setJson(
|
||||
this.parentKey(parentJobId),
|
||||
jobIds,
|
||||
RIOT_JOB_STATUS_TTL_SECONDS,
|
||||
|
||||
Reference in new issue
Block a user