fix(redis): share Garnet endpoint and update database versions
This commit is contained in:
1 parent
9117c9c0a4
commit
0c4c4239ec
7 files changed
+157
-231
No files matched your search
@@ -94,9 +94,8 @@ LOLYMPUS는 이 반복 작업을 줄이는 데 초점을 맞췄습니다. 운영
|
|||||||
- TypeScript
|
- TypeScript
|
||||||
- TanStack Query
|
- TanStack Query
|
||||||
- NestJS
|
- NestJS
|
||||||
- MongoDB
|
- MongoDB 8.3
|
||||||
- Redis
|
- Redis 8.10 (일반 캐시/스트림과 경매 실시간 상태 공유)
|
||||||
- Redis 8.4 (경매 실시간 상태 전용)
|
|
||||||
- NATS
|
- NATS
|
||||||
- Riot API
|
- Riot API
|
||||||
- pnpm workspace
|
- pnpm workspace
|
||||||
@@ -118,9 +117,9 @@ scripts/ deployment and maintenance scripts
|
|||||||
|
|
||||||
- Node.js 24 이상
|
- Node.js 24 이상
|
||||||
- pnpm 10.4.1 (`packageManager` 기준, `corepack enable`로 활성화 권장)
|
- pnpm 10.4.1 (`packageManager` 기준, `corepack enable`로 활성화 권장)
|
||||||
- MongoDB 7.0, 일반 캐시/스트림용 Redis, 경매 실시간 상태 전용 Redis 8.4, NATS 실행 환경
|
- MongoDB 8.3, Redis 8.10, NATS 실행 환경
|
||||||
|
|
||||||
경매 Redis는 라이브 경매 상태의 source of truth로 사용되므로 일반 캐시/스트림 Redis와 물리적으로 분리해야 합니다. API는 `REDIS_URL`/`REDIS_ENDPOINT`와 별도로 `AUCTION_REDIS_URL`/`AUCTION_REDIS_ENDPOINT`를 읽고, 두 값이 같은 호스트/포트를 가리키면 부팅을 거부합니다.
|
일반 캐시/스트림, 경매 실시간 상태, WebSocket Pub/Sub는 `GARNET_ENDPOINT`로 지정한 하나의 Redis 인스턴스를 공유합니다. 값은 `host:port`, `redis://...`, `rediss://...` 형식을 지원합니다. 인증이 필요하면 기존 `GARNET_USERID`(또는 `GARNET_USERNAME`)와 `GARNET_PASSWORD`를 설정합니다. `REDIS_*`, `AUCTION_REDIS_*`, `GARNET_URL` 연결 설정은 사용하지 않습니다. 경매 점검 스크립트도 같은 설정을 사용합니다.
|
||||||
|
|
||||||
## 실행 방법
|
## 실행 방법
|
||||||
|
|
||||||
|
|||||||
@@ -2,10 +2,7 @@ import { Logger, Module } from '@nestjs/common';
|
|||||||
import { createClient } from 'redis';
|
import { createClient } from 'redis';
|
||||||
import { SecretModule } from '../../global/secret/secret.module';
|
import { SecretModule } from '../../global/secret/secret.module';
|
||||||
import { SecretService } from '../../global/secret/secret.service';
|
import { SecretService } from '../../global/secret/secret.service';
|
||||||
import {
|
import { loadRedisClientOptions } from '../../global/redis/redis.config';
|
||||||
assertDistinctRedisInstances,
|
|
||||||
resolveRedisClientOptions,
|
|
||||||
} from '../../global/redis/redis.config';
|
|
||||||
import { AUCTION_REDIS_CLIENT } from './auction-redis.constants';
|
import { AUCTION_REDIS_CLIENT } from './auction-redis.constants';
|
||||||
import {
|
import {
|
||||||
AuctionRedisService,
|
AuctionRedisService,
|
||||||
@@ -14,18 +11,6 @@ import {
|
|||||||
import { AuctionRedisCommandService } from './auction-redis-command.service';
|
import { AuctionRedisCommandService } from './auction-redis-command.service';
|
||||||
import { AuctionRedisStateReaderService } from './auction-redis-state-reader.service';
|
import { AuctionRedisStateReaderService } from './auction-redis-state-reader.service';
|
||||||
|
|
||||||
async function readFirstSecret(
|
|
||||||
secretService: SecretService,
|
|
||||||
keys: string[],
|
|
||||||
): Promise<string | undefined> {
|
|
||||||
for (const key of keys) {
|
|
||||||
const value = await secretService.get<string>(key, '');
|
|
||||||
const trimmed = value.trim();
|
|
||||||
if (trimmed) return trimmed;
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [SecretModule],
|
imports: [SecretModule],
|
||||||
providers: [
|
providers: [
|
||||||
@@ -35,45 +20,10 @@ async function readFirstSecret(
|
|||||||
useFactory: async (
|
useFactory: async (
|
||||||
secretService: SecretService,
|
secretService: SecretService,
|
||||||
): Promise<AuctionRedisClient> => {
|
): Promise<AuctionRedisClient> => {
|
||||||
await secretService.ready();
|
|
||||||
|
|
||||||
const [
|
|
||||||
redisUrl,
|
|
||||||
redisEndpoint,
|
|
||||||
auctionRedisUrl,
|
|
||||||
auctionRedisEndpoint,
|
|
||||||
auctionRedisUsername,
|
|
||||||
auctionRedisPassword,
|
|
||||||
] = await Promise.all([
|
|
||||||
readFirstSecret(secretService, ['REDIS_URL', 'GARNET_URL']),
|
|
||||||
readFirstSecret(secretService, ['REDIS_ENDPOINT', 'GARNET_ENDPOINT']),
|
|
||||||
readFirstSecret(secretService, ['AUCTION_REDIS_URL']),
|
|
||||||
readFirstSecret(secretService, ['AUCTION_REDIS_ENDPOINT']),
|
|
||||||
readFirstSecret(secretService, [
|
|
||||||
'AUCTION_REDIS_USERNAME',
|
|
||||||
'AUCTION_REDIS_USERID',
|
|
||||||
]),
|
|
||||||
readFirstSecret(secretService, ['AUCTION_REDIS_PASSWORD']),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const redisOptions = resolveRedisClientOptions({
|
|
||||||
endpoint: redisEndpoint,
|
|
||||||
endpointEnvName: 'REDIS_ENDPOINT',
|
|
||||||
url: redisUrl,
|
|
||||||
urlEnvName: 'REDIS_URL',
|
|
||||||
});
|
|
||||||
const auctionRedisOptions = resolveRedisClientOptions({
|
|
||||||
endpoint: auctionRedisEndpoint,
|
|
||||||
endpointEnvName: 'AUCTION_REDIS_ENDPOINT',
|
|
||||||
password: auctionRedisPassword,
|
|
||||||
url: auctionRedisUrl,
|
|
||||||
urlEnvName: 'AUCTION_REDIS_URL',
|
|
||||||
username: auctionRedisUsername,
|
|
||||||
});
|
|
||||||
assertDistinctRedisInstances(redisOptions.url, auctionRedisOptions.url);
|
|
||||||
|
|
||||||
const logger = new Logger('AuctionRedisClient');
|
const logger = new Logger('AuctionRedisClient');
|
||||||
const client = createClient(auctionRedisOptions);
|
const client = createClient(
|
||||||
|
await loadRedisClientOptions(secretService),
|
||||||
|
);
|
||||||
client.on('error', (error: unknown) => {
|
client.on('error', (error: unknown) => {
|
||||||
logger.error(
|
logger.error(
|
||||||
`Auction Redis client error: ${
|
`Auction Redis client error: ${
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
import {
|
import { normalizeRedisUrl, resolveRedisClientOptions } from './redis.config';
|
||||||
assertDistinctRedisInstances,
|
|
||||||
normalizeRedisUrl,
|
|
||||||
resolveRedisClientOptions,
|
|
||||||
} from './redis.config';
|
|
||||||
|
|
||||||
describe('redis.config', () => {
|
describe('redis.config', () => {
|
||||||
it('normalizes host:port endpoints into redis URLs', () => {
|
it('normalizes host:port endpoints into redis URLs', () => {
|
||||||
expect(normalizeRedisUrl('127.0.0.1:6379', 'REDIS_ENDPOINT')).toBe(
|
expect(normalizeRedisUrl(' 127.0.0.1:6379 ')).toBe(
|
||||||
'redis://127.0.0.1:6379',
|
'redis://127.0.0.1:6379',
|
||||||
);
|
);
|
||||||
expect(normalizeRedisUrl('rediss://cache.example.com:6380')).toBe(
|
expect(normalizeRedisUrl('rediss://cache.example.com:6380')).toBe(
|
||||||
@@ -15,8 +11,8 @@ describe('redis.config', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('throws a clear error for empty endpoints', () => {
|
it('throws a clear error for empty endpoints', () => {
|
||||||
expect(() => normalizeRedisUrl(' ', 'REDIS_ENDPOINT')).toThrow(
|
expect(() => normalizeRedisUrl(' ')).toThrow(
|
||||||
'REDIS_ENDPOINT cannot be empty',
|
'GARNET_ENDPOINT cannot be empty',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -26,34 +22,32 @@ describe('redis.config', () => {
|
|||||||
endpoint: 'redis.internal:6379',
|
endpoint: 'redis.internal:6379',
|
||||||
username: '',
|
username: '',
|
||||||
password: undefined,
|
password: undefined,
|
||||||
endpointEnvName: 'REDIS_ENDPOINT',
|
|
||||||
}),
|
}),
|
||||||
).toEqual({
|
).toEqual({
|
||||||
url: 'redis://redis.internal:6379',
|
url: 'redis://redis.internal:6379',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('rejects auction Redis when it points at the same physical Redis instance', () => {
|
it.each([undefined, '', ' '])(
|
||||||
expect(() =>
|
'rejects a missing or blank endpoint: %p',
|
||||||
assertDistinctRedisInstances(
|
(endpoint) => {
|
||||||
'redis://cache.internal:6379/0',
|
expect(() => resolveRedisClientOptions({ endpoint })).toThrow(
|
||||||
'redis://cache.internal:6379/1',
|
'GARNET_ENDPOINT cannot be empty',
|
||||||
),
|
);
|
||||||
).toThrow('AUCTION_REDIS must use a separate Redis instance');
|
},
|
||||||
});
|
);
|
||||||
|
|
||||||
it('allows different Redis hosts or ports', () => {
|
it('preserves TLS, database selection, and credentials', () => {
|
||||||
expect(() =>
|
expect(
|
||||||
assertDistinctRedisInstances(
|
resolveRedisClientOptions({
|
||||||
'redis://cache.internal:6379',
|
endpoint: ' rediss://garnet.internal:6380/1 ',
|
||||||
'redis://auction.internal:6379',
|
username: ' garnet-user ',
|
||||||
),
|
password: ' garnet-password ',
|
||||||
).not.toThrow();
|
}),
|
||||||
expect(() =>
|
).toEqual({
|
||||||
assertDistinctRedisInstances(
|
url: 'rediss://garnet.internal:6380/1',
|
||||||
'redis://cache.internal:6379',
|
username: 'garnet-user',
|
||||||
'redis://cache.internal:6380',
|
password: 'garnet-password',
|
||||||
),
|
});
|
||||||
).not.toThrow();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import type { SecretService } from '../secret/secret.service';
|
||||||
|
|
||||||
export interface RedisClientOptions {
|
export interface RedisClientOptions {
|
||||||
password?: string;
|
password?: string;
|
||||||
url: string;
|
url: string;
|
||||||
@@ -6,10 +8,7 @@ export interface RedisClientOptions {
|
|||||||
|
|
||||||
export interface ResolveRedisClientOptionsParams {
|
export interface ResolveRedisClientOptionsParams {
|
||||||
endpoint?: string;
|
endpoint?: string;
|
||||||
endpointEnvName?: string;
|
|
||||||
password?: string;
|
password?: string;
|
||||||
url?: string;
|
|
||||||
urlEnvName?: string;
|
|
||||||
username?: string;
|
username?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -18,13 +17,10 @@ function trimOptional(value: string | undefined): string | undefined {
|
|||||||
return trimmed ? trimmed : undefined;
|
return trimmed ? trimmed : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function normalizeRedisUrl(
|
export function normalizeRedisUrl(endpointOrUrl: string): string {
|
||||||
endpointOrUrl: string,
|
|
||||||
envName = 'REDIS_URL',
|
|
||||||
): string {
|
|
||||||
const trimmed = endpointOrUrl.trim();
|
const trimmed = endpointOrUrl.trim();
|
||||||
if (!trimmed) {
|
if (!trimmed) {
|
||||||
throw new Error(`${envName} cannot be empty`);
|
throw new Error('GARNET_ENDPOINT cannot be empty');
|
||||||
}
|
}
|
||||||
if (/^redis(s)?:\/\//iu.test(trimmed)) {
|
if (/^redis(s)?:\/\//iu.test(trimmed)) {
|
||||||
return trimmed;
|
return trimmed;
|
||||||
@@ -35,19 +31,8 @@ export function normalizeRedisUrl(
|
|||||||
export function resolveRedisClientOptions(
|
export function resolveRedisClientOptions(
|
||||||
params: ResolveRedisClientOptionsParams,
|
params: ResolveRedisClientOptionsParams,
|
||||||
): RedisClientOptions {
|
): RedisClientOptions {
|
||||||
const url = trimOptional(params.url);
|
|
||||||
const endpoint = trimOptional(params.endpoint);
|
|
||||||
const selectedEndpoint = url ?? endpoint;
|
|
||||||
const selectedEnvName = url
|
|
||||||
? (params.urlEnvName ?? 'REDIS_URL')
|
|
||||||
: (params.endpointEnvName ?? 'REDIS_ENDPOINT');
|
|
||||||
|
|
||||||
if (!selectedEndpoint) {
|
|
||||||
throw new Error(`${selectedEnvName} cannot be empty`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const options: RedisClientOptions = {
|
const options: RedisClientOptions = {
|
||||||
url: normalizeRedisUrl(selectedEndpoint, selectedEnvName),
|
url: normalizeRedisUrl(params.endpoint ?? ''),
|
||||||
};
|
};
|
||||||
const username = trimOptional(params.username);
|
const username = trimOptional(params.username);
|
||||||
const password = trimOptional(params.password);
|
const password = trimOptional(params.password);
|
||||||
@@ -56,22 +41,20 @@ export function resolveRedisClientOptions(
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseRedisInstanceIdentity(redisUrl: string): string {
|
export async function loadRedisClientOptions(
|
||||||
const url = new URL(normalizeRedisUrl(redisUrl));
|
secretService: Pick<SecretService, 'get' | 'ready'>,
|
||||||
const hostname = url.hostname.toLowerCase();
|
): Promise<RedisClientOptions> {
|
||||||
const port = url.port || '6379';
|
await secretService.ready();
|
||||||
return `${hostname}:${port}`;
|
const [endpoint, username, userId, password] = await Promise.all([
|
||||||
}
|
secretService.get<string>('GARNET_ENDPOINT', ''),
|
||||||
|
secretService.get<string>('GARNET_USERNAME', ''),
|
||||||
|
secretService.get<string>('GARNET_USERID', ''),
|
||||||
|
secretService.get<string>('GARNET_PASSWORD', ''),
|
||||||
|
]);
|
||||||
|
|
||||||
export function assertDistinctRedisInstances(
|
return resolveRedisClientOptions({
|
||||||
redisUrl: string,
|
endpoint,
|
||||||
auctionRedisUrl: string,
|
password,
|
||||||
): void {
|
username: trimOptional(username) ?? userId,
|
||||||
const redisIdentity = parseRedisInstanceIdentity(redisUrl);
|
});
|
||||||
const auctionRedisIdentity = parseRedisInstanceIdentity(auctionRedisUrl);
|
|
||||||
if (redisIdentity === auctionRedisIdentity) {
|
|
||||||
throw new Error(
|
|
||||||
'AUCTION_REDIS must use a separate Redis instance from REDIS because auction Redis is a live state source of truth.',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
import { Test } from '@nestjs/testing';
|
||||||
|
import { createClient } from 'redis';
|
||||||
|
import { AuctionRedisModule } from '../../auction/redis/auction-redis.module';
|
||||||
|
import { AuctionRedisService } from '../../auction/redis/auction-redis.service';
|
||||||
|
import { SecretService } from '../secret/secret.service';
|
||||||
|
import { RedisModule } from './redis.module';
|
||||||
|
import { RedisService, type RedisClient } from './redis.service';
|
||||||
|
|
||||||
|
jest.mock('redis', () => ({ createClient: jest.fn() }));
|
||||||
|
|
||||||
|
describe('shared Garnet instance', () => {
|
||||||
|
it.each([
|
||||||
|
{
|
||||||
|
name: 'only GARNET_ENDPOINT is configured',
|
||||||
|
secrets: { GARNET_ENDPOINT: 'garnet.internal:6379' },
|
||||||
|
options: { url: 'redis://garnet.internal:6379' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'existing Garnet credentials are configured',
|
||||||
|
secrets: {
|
||||||
|
GARNET_ENDPOINT: ' rediss://garnet.internal:6380/1 ',
|
||||||
|
GARNET_USERID: 'garnet-user',
|
||||||
|
GARNET_PASSWORD: 'garnet-password',
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
url: 'rediss://garnet.internal:6380/1',
|
||||||
|
username: 'garnet-user',
|
||||||
|
password: 'garnet-password',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'obsolete Redis settings are also configured',
|
||||||
|
secrets: {
|
||||||
|
GARNET_ENDPOINT: 'garnet.internal:6379',
|
||||||
|
GARNET_USERNAME: 'garnet-user',
|
||||||
|
GARNET_PASSWORD: 'garnet-password',
|
||||||
|
GARNET_URL: 'redis://obsolete-garnet.internal:6379',
|
||||||
|
REDIS_URL: 'redis://obsolete-common.internal:6379',
|
||||||
|
REDIS_ENDPOINT: 'obsolete-common.internal:6379',
|
||||||
|
REDIS_USERNAME: 'obsolete-common-user',
|
||||||
|
REDIS_PASSWORD: 'obsolete-common-password',
|
||||||
|
AUCTION_REDIS_URL: 'redis://obsolete-common.internal:6379',
|
||||||
|
AUCTION_REDIS_ENDPOINT: 'obsolete-common.internal:6379',
|
||||||
|
AUCTION_REDIS_USERNAME: 'obsolete-auction-user',
|
||||||
|
AUCTION_REDIS_PASSWORD: 'obsolete-auction-password',
|
||||||
|
},
|
||||||
|
options: {
|
||||||
|
url: 'redis://garnet.internal:6379',
|
||||||
|
username: 'garnet-user',
|
||||||
|
password: 'garnet-password',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
])('boots both Redis modules when $name', async ({ secrets, options }) => {
|
||||||
|
const values: Record<string, string | undefined> = secrets;
|
||||||
|
const secretService = {
|
||||||
|
ready: jest.fn().mockResolvedValue(undefined),
|
||||||
|
get: jest.fn((key: string, fallback: string) =>
|
||||||
|
Promise.resolve(values[key] ?? fallback),
|
||||||
|
),
|
||||||
|
};
|
||||||
|
const clients = Array.from({ length: 2 }, () => ({
|
||||||
|
connect: jest.fn().mockResolvedValue(undefined),
|
||||||
|
on: jest.fn(),
|
||||||
|
quit: jest.fn().mockResolvedValue(undefined),
|
||||||
|
destroy: jest.fn(),
|
||||||
|
}));
|
||||||
|
const createClientMock = jest.mocked(createClient);
|
||||||
|
createClientMock.mockReset();
|
||||||
|
for (const client of clients) {
|
||||||
|
createClientMock.mockReturnValueOnce(client as unknown as RedisClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
const module = await Test.createTestingModule({
|
||||||
|
imports: [RedisModule, AuctionRedisModule],
|
||||||
|
})
|
||||||
|
.overrideProvider(SecretService)
|
||||||
|
.useValue(secretService)
|
||||||
|
.compile();
|
||||||
|
|
||||||
|
try {
|
||||||
|
await module.init();
|
||||||
|
expect(createClientMock).toHaveBeenCalledTimes(2);
|
||||||
|
for (let index = 0; index < clients.length; index++) {
|
||||||
|
expect(createClientMock).toHaveBeenNthCalledWith(index + 1, options);
|
||||||
|
expect(clients[index].connect).toHaveBeenCalledTimes(1);
|
||||||
|
}
|
||||||
|
expect(clients).toContain(module.get(RedisService).raw());
|
||||||
|
expect(clients).toContain(module.get(AuctionRedisService).raw());
|
||||||
|
expect(module.get(RedisService).raw()).not.toBe(
|
||||||
|
module.get(AuctionRedisService).raw(),
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
await module.close();
|
||||||
|
}
|
||||||
|
for (const client of clients) {
|
||||||
|
expect(client.quit).toHaveBeenCalledTimes(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -3,22 +3,10 @@ import { createClient } from 'redis';
|
|||||||
import { SecretModule } from '../secret/secret.module';
|
import { SecretModule } from '../secret/secret.module';
|
||||||
import { SecretService } from '../secret/secret.service';
|
import { SecretService } from '../secret/secret.service';
|
||||||
import { REDIS_CLIENT } from './redis.constants';
|
import { REDIS_CLIENT } from './redis.constants';
|
||||||
import { resolveRedisClientOptions } from './redis.config';
|
import { loadRedisClientOptions } from './redis.config';
|
||||||
import { DistributedLockService } from './distributed-lock.service';
|
import { DistributedLockService } from './distributed-lock.service';
|
||||||
import { RedisService, type RedisClient } from './redis.service';
|
import { RedisService, type RedisClient } from './redis.service';
|
||||||
|
|
||||||
async function readFirstSecret(
|
|
||||||
secretService: SecretService,
|
|
||||||
keys: string[],
|
|
||||||
): Promise<string | undefined> {
|
|
||||||
for (const key of keys) {
|
|
||||||
const value = await secretService.get<string>(key, '');
|
|
||||||
const trimmed = value.trim();
|
|
||||||
if (trimmed) return trimmed;
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [SecretModule],
|
imports: [SecretModule],
|
||||||
providers: [
|
providers: [
|
||||||
@@ -28,30 +16,9 @@ async function readFirstSecret(
|
|||||||
useFactory: async (
|
useFactory: async (
|
||||||
secretService: SecretService,
|
secretService: SecretService,
|
||||||
): Promise<RedisClient> => {
|
): Promise<RedisClient> => {
|
||||||
await secretService.ready();
|
|
||||||
|
|
||||||
const [url, endpoint, username, password] = await Promise.all([
|
|
||||||
readFirstSecret(secretService, ['REDIS_URL', 'GARNET_URL']),
|
|
||||||
readFirstSecret(secretService, ['REDIS_ENDPOINT', 'GARNET_ENDPOINT']),
|
|
||||||
readFirstSecret(secretService, [
|
|
||||||
'REDIS_USERNAME',
|
|
||||||
'REDIS_USERID',
|
|
||||||
'GARNET_USERNAME',
|
|
||||||
'GARNET_USERID',
|
|
||||||
]),
|
|
||||||
readFirstSecret(secretService, ['REDIS_PASSWORD', 'GARNET_PASSWORD']),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const logger = new Logger('RedisClient');
|
const logger = new Logger('RedisClient');
|
||||||
const client = createClient(
|
const client = createClient(
|
||||||
resolveRedisClientOptions({
|
await loadRedisClientOptions(secretService),
|
||||||
endpoint,
|
|
||||||
endpointEnvName: 'REDIS_ENDPOINT',
|
|
||||||
password,
|
|
||||||
url,
|
|
||||||
urlEnvName: 'REDIS_URL',
|
|
||||||
username,
|
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
client.on('error', (error: unknown) => {
|
client.on('error', (error: unknown) => {
|
||||||
|
|||||||
@@ -15,10 +15,7 @@ import {
|
|||||||
ConnectionService,
|
ConnectionService,
|
||||||
resolveMongoDbName,
|
resolveMongoDbName,
|
||||||
} from '../modules/global/database/connection.service';
|
} from '../modules/global/database/connection.service';
|
||||||
import {
|
import { loadRedisClientOptions } from '../modules/global/redis/redis.config';
|
||||||
assertDistinctRedisInstances,
|
|
||||||
resolveRedisClientOptions,
|
|
||||||
} from '../modules/global/redis/redis.config';
|
|
||||||
import { SecretService } from '../modules/global/secret/secret.service';
|
import { SecretService } from '../modules/global/secret/secret.service';
|
||||||
|
|
||||||
type RedisClient = ReturnType<typeof createClient>;
|
type RedisClient = ReturnType<typeof createClient>;
|
||||||
@@ -59,18 +56,6 @@ function loadProjectEnv(): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function readFirstSecret(
|
|
||||||
secretService: SecretService,
|
|
||||||
keys: string[],
|
|
||||||
): Promise<string | undefined> {
|
|
||||||
for (const key of keys) {
|
|
||||||
const value = await secretService.get<string>(key, '');
|
|
||||||
const trimmed = value.trim();
|
|
||||||
if (trimmed) return trimmed;
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
function requireSafeNodeEnv(): void {
|
function requireSafeNodeEnv(): void {
|
||||||
if (
|
if (
|
||||||
process.env.NODE_ENV === 'prod' &&
|
process.env.NODE_ENV === 'prod' &&
|
||||||
@@ -90,37 +75,7 @@ async function createRedisClientFromSecrets(
|
|||||||
secretService: SecretService,
|
secretService: SecretService,
|
||||||
kind: 'common' | 'auction',
|
kind: 'common' | 'auction',
|
||||||
): Promise<RedisClient> {
|
): Promise<RedisClient> {
|
||||||
const prefix = kind === 'auction' ? 'AUCTION_REDIS' : 'REDIS';
|
const client = createClient(await loadRedisClientOptions(secretService));
|
||||||
const urlKeys =
|
|
||||||
kind === 'auction' ? [`${prefix}_URL`] : ['REDIS_URL', 'GARNET_URL'];
|
|
||||||
const endpointKeys =
|
|
||||||
kind === 'auction'
|
|
||||||
? [`${prefix}_ENDPOINT`]
|
|
||||||
: ['REDIS_ENDPOINT', 'GARNET_ENDPOINT'];
|
|
||||||
const usernameKeys =
|
|
||||||
kind === 'auction'
|
|
||||||
? [`${prefix}_USERNAME`, `${prefix}_USERID`]
|
|
||||||
: ['REDIS_USERNAME', 'REDIS_USERID', 'GARNET_USERNAME', 'GARNET_USERID'];
|
|
||||||
const passwordKeys =
|
|
||||||
kind === 'auction'
|
|
||||||
? [`${prefix}_PASSWORD`]
|
|
||||||
: ['REDIS_PASSWORD', 'GARNET_PASSWORD'];
|
|
||||||
const [url, endpoint, username, password] = await Promise.all([
|
|
||||||
readFirstSecret(secretService, urlKeys),
|
|
||||||
readFirstSecret(secretService, endpointKeys),
|
|
||||||
readFirstSecret(secretService, usernameKeys),
|
|
||||||
readFirstSecret(secretService, passwordKeys),
|
|
||||||
]);
|
|
||||||
const client = createClient(
|
|
||||||
resolveRedisClientOptions({
|
|
||||||
endpoint,
|
|
||||||
endpointEnvName: `${prefix}_ENDPOINT`,
|
|
||||||
password,
|
|
||||||
url,
|
|
||||||
urlEnvName: `${prefix}_URL`,
|
|
||||||
username,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
client.on('error', (error) => {
|
client.on('error', (error) => {
|
||||||
console.error(`[auction-rehearsal] ${kind} Redis error:`, error);
|
console.error(`[auction-rehearsal] ${kind} Redis error:`, error);
|
||||||
});
|
});
|
||||||
@@ -128,31 +83,10 @@ async function createRedisClientFromSecrets(
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function assertRedisTopology(secretService: SecretService): Promise<{
|
async function connectRedisClients(secretService: SecretService): Promise<{
|
||||||
auctionRedisClient: RedisClient;
|
auctionRedisClient: RedisClient;
|
||||||
commonRedisClient: RedisClient;
|
commonRedisClient: RedisClient;
|
||||||
}> {
|
}> {
|
||||||
const [redisUrl, redisEndpoint, auctionRedisUrl, auctionRedisEndpoint] =
|
|
||||||
await Promise.all([
|
|
||||||
readFirstSecret(secretService, ['REDIS_URL', 'GARNET_URL']),
|
|
||||||
readFirstSecret(secretService, ['REDIS_ENDPOINT', 'GARNET_ENDPOINT']),
|
|
||||||
readFirstSecret(secretService, ['AUCTION_REDIS_URL']),
|
|
||||||
readFirstSecret(secretService, ['AUCTION_REDIS_ENDPOINT']),
|
|
||||||
]);
|
|
||||||
const redisOptions = resolveRedisClientOptions({
|
|
||||||
endpoint: redisEndpoint,
|
|
||||||
endpointEnvName: 'REDIS_ENDPOINT',
|
|
||||||
url: redisUrl,
|
|
||||||
urlEnvName: 'REDIS_URL',
|
|
||||||
});
|
|
||||||
const auctionRedisOptions = resolveRedisClientOptions({
|
|
||||||
endpoint: auctionRedisEndpoint,
|
|
||||||
endpointEnvName: 'AUCTION_REDIS_ENDPOINT',
|
|
||||||
url: auctionRedisUrl,
|
|
||||||
urlEnvName: 'AUCTION_REDIS_URL',
|
|
||||||
});
|
|
||||||
assertDistinctRedisInstances(redisOptions.url, auctionRedisOptions.url);
|
|
||||||
|
|
||||||
const [commonRedisClient, auctionRedisClient] = await Promise.all([
|
const [commonRedisClient, auctionRedisClient] = await Promise.all([
|
||||||
createRedisClientFromSecrets(secretService, 'common'),
|
createRedisClientFromSecrets(secretService, 'common'),
|
||||||
createRedisClientFromSecrets(secretService, 'auction'),
|
createRedisClientFromSecrets(secretService, 'auction'),
|
||||||
@@ -409,7 +343,7 @@ async function main(): Promise<void> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const { auctionRedisClient, commonRedisClient } =
|
const { auctionRedisClient, commonRedisClient } =
|
||||||
await assertRedisTopology(secretService);
|
await connectRedisClients(secretService);
|
||||||
const connectionService = new ConnectionService(secretService);
|
const connectionService = new ConnectionService(secretService);
|
||||||
const auctionRedisService = new AuctionRedisService(auctionRedisClient);
|
const auctionRedisService = new AuctionRedisService(auctionRedisClient);
|
||||||
const auctionRedisCommandService = new AuctionRedisCommandService(
|
const auctionRedisCommandService = new AuctionRedisCommandService(
|
||||||
|
|||||||
Reference in new issue
Block a user