Preserve tournament result jobs through lock contention and transient failures, atomically schedule retries before acknowledgement, and publish initial polls after releasing the preparation lock. Fix Redis connection blocking, durability Hash cleanup, duplicate auction request ordering, and scheduled-job promotion. Use native Redis 8.10 stream, lock, counter, and field-expiring cache operations with invalidation fencing. Repair 90 test type errors and add a repeatable API typecheck command. Validation: 704 API tests, 129 web tests, 31 real Redis integration tests, 3 dependency checks, workspace type checks and builds, and changed-file lint.
136 lines
3.5 KiB
TypeScript
136 lines
3.5 KiB
TypeScript
import { Inject, Injectable, OnModuleDestroy } from '@nestjs/common';
|
|
import type { createClient } from 'redis';
|
|
import { AUCTION_REDIS_CLIENT } from './auction-redis.constants';
|
|
import type { AuctionRedisScriptDefinition } from './auction-redis.scripts';
|
|
|
|
export type AuctionRedisClient = ReturnType<typeof createClient>;
|
|
|
|
export interface EvalShaWithReloadParams {
|
|
keys: string[];
|
|
script: string;
|
|
scriptSha: string;
|
|
values: string[];
|
|
}
|
|
|
|
export class AuctionRedisNoscriptError extends Error {
|
|
constructor(message = 'NOSCRIPT No matching script. Please use EVAL.') {
|
|
super(message);
|
|
this.name = 'AuctionRedisNoscriptError';
|
|
}
|
|
}
|
|
|
|
function isNoscriptError(error: unknown): boolean {
|
|
if (error instanceof AuctionRedisNoscriptError) return true;
|
|
const message =
|
|
error instanceof Error
|
|
? error.message
|
|
: typeof error === 'string'
|
|
? error
|
|
: '';
|
|
return /\bNOSCRIPT\b/iu.test(message);
|
|
}
|
|
|
|
function buildEvalShaCommand(
|
|
scriptSha: string,
|
|
keys: string[],
|
|
values: string[],
|
|
): string[] {
|
|
return ['EVALSHA', scriptSha, String(keys.length), ...keys, ...values];
|
|
}
|
|
|
|
function parseRedisTimeReply(reply: unknown): number {
|
|
if (Array.isArray(reply) && reply.length >= 2) {
|
|
const seconds = Number(reply[0]);
|
|
const microseconds = Number(reply[1]);
|
|
if (Number.isFinite(seconds) && Number.isFinite(microseconds)) {
|
|
return seconds * 1000 + Math.floor(microseconds / 1000);
|
|
}
|
|
}
|
|
|
|
throw new Error(`Unexpected Redis TIME reply: ${JSON.stringify(reply)}`);
|
|
}
|
|
|
|
@Injectable()
|
|
export class AuctionRedisService implements OnModuleDestroy {
|
|
constructor(
|
|
@Inject(AUCTION_REDIS_CLIENT)
|
|
private readonly client: AuctionRedisClient,
|
|
) {}
|
|
|
|
async onModuleDestroy() {
|
|
try {
|
|
await this.client.quit();
|
|
} catch {
|
|
this.client.destroy();
|
|
}
|
|
}
|
|
|
|
public raw(): AuctionRedisClient {
|
|
return this.client;
|
|
}
|
|
|
|
public async getCurrentRedisTimestampMS(): Promise<number> {
|
|
const reply = await this.client.sendCommand(['TIME']);
|
|
return parseRedisTimeReply(reply);
|
|
}
|
|
|
|
public async evalShaWithReload<T = unknown>(
|
|
params: EvalShaWithReloadParams,
|
|
): Promise<T> {
|
|
try {
|
|
return (await this.client.sendCommand(
|
|
buildEvalShaCommand(params.scriptSha, params.keys, params.values),
|
|
)) as T;
|
|
} catch (error) {
|
|
if (!isNoscriptError(error)) throw error;
|
|
}
|
|
|
|
const loadedShaReply = await this.client.sendCommand([
|
|
'SCRIPT',
|
|
'LOAD',
|
|
params.script,
|
|
]);
|
|
if (typeof loadedShaReply !== 'string') {
|
|
throw new Error('Redis SCRIPT LOAD returned a non-string SHA.');
|
|
}
|
|
const loadedSha = loadedShaReply;
|
|
return (await this.client.sendCommand(
|
|
buildEvalShaCommand(loadedSha, params.keys, params.values),
|
|
)) as T;
|
|
}
|
|
|
|
public async evalScript<T = unknown>(
|
|
script: AuctionRedisScriptDefinition,
|
|
keys: string[],
|
|
values: string[],
|
|
): Promise<T> {
|
|
return this.evalShaWithReload<T>({
|
|
keys,
|
|
script: script.lua,
|
|
scriptSha: script.sha,
|
|
values,
|
|
});
|
|
}
|
|
|
|
public async acquireTokenLock(
|
|
key: string,
|
|
token: string,
|
|
ttlMS: number,
|
|
): Promise<boolean> {
|
|
const result = await this.client.sendCommand<string | null>([
|
|
'SET',
|
|
key,
|
|
token,
|
|
'PX',
|
|
String(ttlMS),
|
|
'NX',
|
|
]);
|
|
return result === 'OK';
|
|
}
|
|
|
|
public async releaseTokenLock(key: string, token: string): Promise<boolean> {
|
|
const result = await this.client.sendCommand(['DELEX', key, 'IFEQ', token]);
|
|
return Number(result) === 1;
|
|
}
|
|
}
|