fix: harden result polling and Redis 8.10 operations

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.
This commit is contained in:
dahyeon committed 2026-11-09 17:33:13 +09:00
1 parent 0c4c4239ec
commit 4b8ccf0077
49 files changed
+2214 -371

No files matched your search

@@ -59,16 +59,15 @@ describe('DistributedLockService', () => {
).resolves.toBe(true);
expect(sendCommand).toHaveBeenCalledWith([
'EVAL',
expect.stringContaining('redis.call("GET", KEYS[1]) == ARGV[1]'),
'1',
'DELEX',
'distributed-lock:test',
'IFEQ',
'owner-token',
]);
});
it('extends a lock only when the ownership token matches', async () => {
const sendCommand = jest.fn().mockResolvedValue(1);
const sendCommand = jest.fn().mockResolvedValue('OK');
const service = createService(sendCommand);
await expect(
@@ -83,11 +82,12 @@ describe('DistributedLockService', () => {
).resolves.toBe(true);
expect(sendCommand).toHaveBeenCalledWith([
'EVAL',
expect.stringContaining('PEXPIRE'),
'1',
'SET',
'distributed-lock:test',
'owner-token',
'IFEQ',
'owner-token',
'PX',
'45000',
]);
});
@@ -2,20 +2,6 @@ import { Injectable, Logger } from '@nestjs/common';
import { randomUUID } from 'crypto';
import { RedisService } from './redis.service';
const RELEASE_LOCK_IF_OWNED_SCRIPT = `
if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("DEL", KEYS[1])
end
return 0
`;
const EXTEND_LOCK_IF_OWNED_SCRIPT = `
if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("PEXPIRE", KEYS[1], ARGV[2])
end
return 0
`;
export interface DistributedLockOwnership {
lockKey: string;
lockOwnershipToken: string;
@@ -75,10 +61,9 @@ export class DistributedLockService {
const result = await this.redisService
.raw()
.sendCommand([
'EVAL',
RELEASE_LOCK_IF_OWNED_SCRIPT,
'1',
'DELEX',
lockOwnership.lockKey,
'IFEQ',
lockOwnership.lockOwnershipToken,
]);
return Number(result) === 1;
@@ -95,15 +80,10 @@ export class DistributedLockService {
const result = await this.redisService
.raw()
.sendCommand([
'EVAL',
EXTEND_LOCK_IF_OWNED_SCRIPT,
'1',
lockOwnership.lockKey,
lockOwnership.lockOwnershipToken,
String(lockExpirationDurationMilliseconds),
]);
return Number(result) === 1;
.sendCommand<
string | null
>(['SET', lockOwnership.lockKey, lockOwnership.lockOwnershipToken, 'IFEQ', lockOwnership.lockOwnershipToken, 'PX', String(lockExpirationDurationMilliseconds)]);
return result === 'OK';
}
public async runWithLock<TResult>(
@@ -5,6 +5,24 @@ function createRedisService(client: Record<string, unknown>) {
}
describe('RedisService stream pending recovery', () => {
it('destroys a dedicated client when connection startup fails without closing the shared client', async () => {
const error = new Error('connection failed');
const dedicated = {
on: jest.fn(),
connect: jest.fn().mockRejectedValue(error),
isOpen: true,
destroy: jest.fn(),
};
const shared = {
duplicate: jest.fn().mockReturnValue(dedicated),
destroy: jest.fn(),
};
const service = createRedisService(shared);
await expect(service.createDedicatedConnection()).rejects.toBe(error);
expect(dedicated.destroy).toHaveBeenCalledTimes(1);
expect(shared.destroy).not.toHaveBeenCalled();
});
it('normalizes XAUTOCLAIM array replies into stream entries', async () => {
const sendCommand = jest
.fn()
@@ -83,6 +83,20 @@ export class RedisService implements OnModuleDestroy {
return this.client;
}
public async createDedicatedConnection(): Promise<RedisService> {
const client = this.client.duplicate();
client.on('error', (error: unknown) => {
this.logger.warn(`Dedicated Redis connection error: ${String(error)}`);
});
try {
await client.connect();
return new RedisService(client);
} catch (error) {
if (client.isOpen) client.destroy();
throw error;
}
}
public async getJson<T>(key: string): Promise<T | null> {
const raw = await this.client.get(key);
if (!raw) return null;
@@ -142,6 +156,13 @@ export class RedisService implements OnModuleDestroy {
return this.client.del(key);
}
public async deleteStringIfEqual(
key: string,
value: string,
): Promise<number> {
return this.client.sendCommand<number>(['DELEX', key, 'IFEQ', value]);
}
public async appendStream(
key: string,
fields: Record<string, string>,
@@ -158,7 +179,10 @@ export class RedisService implements OnModuleDestroy {
}
: undefined;
return this.client.xAdd(key, '*', fields, trimOptions);
return this.client.xAdd(key, '*', fields, {
...trimOptions,
policy: 'ACKED',
});
}
public async createConsumerGroup(
@@ -174,11 +198,13 @@ export class RedisService implements OnModuleDestroy {
group: string,
consumer: string,
startId: string,
options?: { blockMs?: number; count?: number },
options?: { blockMs?: number; count?: number; claimMinIdleMs?: number },
): Promise<StreamReadResult[] | null> {
const readOptions: { BLOCK?: number; COUNT?: number } = {};
const readOptions: { BLOCK?: number; COUNT?: number; CLAIM?: number } = {};
if (options?.blockMs !== undefined) readOptions.BLOCK = options.blockMs;
if (options?.count !== undefined) readOptions.COUNT = options.count;
if (options?.claimMinIdleMs !== undefined)
readOptions.CLAIM = options.claimMinIdleMs;
const reply = await this.client.xReadGroup(
group,
@@ -244,6 +270,14 @@ export class RedisService implements OnModuleDestroy {
return this.client.xAck(key, group, ids);
}
public async acknowledgeAndDeleteStream(
key: string,
group: string,
ids: string | string[],
): Promise<void> {
await this.client.xAckDel(key, group, ids, 'ACKED');
}
public async deleteStreamEntries(
key: string,
ids: string | string[],