Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

feat(redis): build every Redis connection from one helper, add REDIS_TLS

Yudhi Armyndharise6a1231a41e8
diff --git a/.env.example b/.env.example
index 5ee260eae..a5f23732b 100644
--- a/.env.example
+++ b/.env.example
@@ -369,6 +369,9 @@ DATABASE_SYNCHRONIZE=false # WARNING: Set false in production! (data DB
# REDIS_CONNECT_TIMEOUT_MS=5000
# REDIS_USERNAME=
# REDIS_PASSWORD=
+# Connect to Redis over TLS (a managed Redis that requires it). Uses Node's default CA store; add a
+# private CA with NODE_EXTRA_CA_CERTS. Not for the built-in Redis container, which serves plain TCP.
+# REDIS_TLS=false
# Redis logical database for the cache (the queue uses the default database).
# REDIS_CACHE_DB=1
diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml
index ac1a1b605..901d372d8 100644
--- a/docker-compose.dev.yml
+++ b/docker-compose.dev.yml
@@ -189,6 +189,8 @@ services:
- REDIS_USERNAME=${REDIS_USERNAME:-}
- REDIS_PASSWORD=${REDIS_PASSWORD:-}
- REDIS_CONNECT_TIMEOUT_MS=${REDIS_CONNECT_TIMEOUT_MS:-}
+ - REDIS_TLS=${REDIS_TLS:-}
+ - REDIS_CACHE_DB=${REDIS_CACHE_DB:-}
# Fetch statuses on ready (off unless the exact string 'true', matching docker-compose.yml).
- STATUS_SEED_ON_READY=${STATUS_SEED_ON_READY:-false}
- AUTOMATION_MAX_PER_SESSION=${AUTOMATION_MAX_PER_SESSION:-}
diff --git a/docker-compose.yml b/docker-compose.yml
index 070e1f1b9..5619755b6 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -337,6 +337,8 @@ services:
- REDIS_USERNAME=${REDIS_USERNAME:-}
- REDIS_PASSWORD=${REDIS_PASSWORD:-}
- REDIS_CONNECT_TIMEOUT_MS=${REDIS_CONNECT_TIMEOUT_MS:-}
+ - REDIS_TLS=${REDIS_TLS:-}
+ - REDIS_CACHE_DB=${REDIS_CACHE_DB:-}
# Engine / session tuning.
- MAX_CONCURRENT_SESSIONS=${MAX_CONCURRENT_SESSIONS:-}
- BAILEYS_AUTH_DIR=${BAILEYS_AUTH_DIR:-}
diff --git a/docs/10-devops-infrastructure.md b/docs/10-devops-infrastructure.md
index ca13938ee..b0e370da6 100644
--- a/docs/10-devops-infrastructure.md
+++ b/docs/10-devops-infrastructure.md
@@ -454,6 +454,9 @@ LOG_FORMAT=json
# REDIS_ENABLED=false
# REDIS_HOST=localhost
# REDIS_PORT=6379
+# REDIS_TLS=false # true for a Redis that requires TLS (not the built-in container); every client
+# # (cache, rate limits, queue, WebSocket fan-out) uses it. Private CA: NODE_EXTRA_CA_CERTS
+# REDIS_CACHE_DB=1 # logical database for the cache
# Redis-backed caching switches on when REDIS_ENABLED=true OR CACHE_ENABLED=true — enabling Redis
# for the queue alone therefore also enables the cache.
# CACHE_ENABLED=true
diff --git a/src/app.module.ts b/src/app.module.ts
index ceb947b6c..f02e8122a 100644
--- a/src/app.module.ts
+++ b/src/app.module.ts
@@ -273,9 +273,7 @@ if (dashboardServingEnabled && dashboardBuildPresent) {
// API. The client is built fail-fast (see throttler-redis.client.ts) so that fail-open
// engages immediately instead of after a queue/timeout stall per request.
const redisStorage =
- process.env.REDIS_ENABLED === 'true'
- ? new RedisThrottlerStorage(createThrottlerRedisClient(configService))
- : undefined;
+ process.env.REDIS_ENABLED === 'true' ? new RedisThrottlerStorage(createThrottlerRedisClient()) : undefined;
return { throttlers, ...(redisStorage ? { storage: redisStorage } : {}) };
},
}),
diff --git a/src/common/cache/cache.service.spec.ts b/src/common/cache/cache.service.spec.ts
index 107faf564..774cb2940 100644
--- a/src/common/cache/cache.service.spec.ts
+++ b/src/common/cache/cache.service.spec.ts
@@ -153,6 +153,54 @@ describe('CacheService Redis-outage resilience', () => {
expect(opts.enableOfflineQueue).toBe(false);
});
+ it('uses the shared connection options and a numeric REDIS_CACHE_DB', async () => {
+ useClient(jest.fn().mockResolvedValue('PONG'));
+ const saved = { tls: process.env.REDIS_TLS, db: process.env.REDIS_CACHE_DB };
+ process.env.REDIS_TLS = 'true';
+ process.env.REDIS_CACHE_DB = '2';
+ try {
+ await new CacheService(enabledConfig()).isAvailable();
+ delete process.env.REDIS_CACHE_DB;
+ await new CacheService(enabledConfig()).isAvailable();
+ } finally {
+ for (const [key, value] of [
+ ['REDIS_TLS', saved.tls],
+ ['REDIS_CACHE_DB', saved.db],
+ ] as const) {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+ }
+ }
+
+ const calls = RedisMock.mock.calls as Array<[{ tls?: unknown; db?: unknown }]>;
+ expect(calls[0][0]).toMatchObject({ tls: {}, db: 2 });
+ expect(calls[1][0].db).toBe(1);
+ });
+
+ it('parses a cached JSON value and reads a miss as null', async () => {
+ const get = jest.fn().mockResolvedValueOnce('["a","b"]').mockResolvedValueOnce(null);
+ RedisMock.mockImplementation(() => ({ ...fakeClient(jest.fn().mockResolvedValue('PONG')), get }));
+ const service = new CacheService(enabledConfig());
+
+ expect(await service.getSessionsList()).toEqual(['a', 'b']);
+ expect(await service.getSessionsList()).toBeNull();
+ });
+
+ it('falls back to the source of truth when a cache command fails', async () => {
+ const fail = jest.fn().mockRejectedValue(new Error('READONLY'));
+ RedisMock.mockImplementation(() => ({
+ ...fakeClient(jest.fn().mockResolvedValue('PONG')),
+ get: fail,
+ setex: fail,
+ }));
+ const service = new CacheService(enabledConfig());
+
+ expect(await service.getSessionInfo('s1')).toBeNull();
+ expect(await service.getSessionsStats()).toBeNull();
+ await expect(service.setSessionInfo('s1', { id: 's1' } as never)).resolves.toBeUndefined();
+ await expect(service.setSessionsStats({} as never)).resolves.toBeUndefined();
+ });
+
it('does not construct a client when the cache is disabled', async () => {
const disabled = { get: (_key: string, def?: unknown) => def } as unknown as ConfigService;
const service = new CacheService(disabled);
diff --git a/src/common/cache/cache.service.ts b/src/common/cache/cache.service.ts
index 4d63c2646..dc4e4a71b 100644
--- a/src/common/cache/cache.service.ts
+++ b/src/common/cache/cache.service.ts
@@ -2,6 +2,8 @@ import { Injectable, OnModuleDestroy } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import Redis from 'ioredis';
import { createLogger } from '../services/logger.service';
+import { resolveNonNegativeIntEnv } from '../../config/configuration';
+import { redisConnectionOptions } from '../../config/redis-options';
export interface SessionInfo {
id: string;
@@ -57,23 +59,17 @@ export class CacheService implements OnModuleDestroy {
private ensureClient(): void {
if (this.redis) return;
- const host = process.env.REDIS_HOST || this.configService.get<string>('REDIS_HOST', 'localhost');
- const port = parseInt(process.env.REDIS_PORT || '', 10) || this.configService.get<number>('REDIS_PORT', 6379);
-
- this.logger.log(`Connecting to Redis at ${host}:${port}`);
+ const connection = redisConnectionOptions();
+ this.logger.log(`Connecting to Redis at ${connection.host}:${connection.port}`);
const redis = new Redis({
- host,
- port,
- username: this.configService.get<string>('REDIS_USERNAME'),
- password: this.configService.get<string>('REDIS_PASSWORD'),
- db: this.configService.get<number>('REDIS_CACHE_DB', 1),
+ ...connection,
+ db: resolveNonNegativeIntEnv(process.env.REDIS_CACHE_DB, 1),
lazyConnect: true,
// Cache is best-effort: a command issued while disconnected fails fast (the caller falls back to
// the source of truth) instead of being queued and stalling the request until reconnect.
enableOfflineQueue: false,
maxRetriesPerRequest: 3,
- connectTimeout: this.configService.get<number>('redis.connectTimeoutMs', 5000),
// Reconnect forever with bounded backoff. Returning null (the previous behavior after 3 tries)
// makes ioredis abandon reconnection permanently, which is exactly what left the cache dead
// across a Redis restart.
diff --git a/src/common/throttler/throttler-redis.client.spec.ts b/src/common/throttler/throttler-redis.client.spec.ts
index 9565e7956..992353557 100644
--- a/src/common/throttler/throttler-redis.client.spec.ts
+++ b/src/common/throttler/throttler-redis.client.spec.ts
@@ -1,42 +1,50 @@
-import { ConfigService } from '@nestjs/config';
import {
buildThrottlerRedisOptions,
createThrottlerRedisClient,
THROTTLER_REDIS_COMMAND_TIMEOUT_MS,
} from './throttler-redis.client';
-const makeConfig = (values: Record<string, unknown>): ConfigService =>
- ({
- get: jest.fn((key: string, fallback?: unknown) => values[key] ?? fallback),
- }) as unknown as ConfigService;
+const KEYS = ['REDIS_HOST', 'REDIS_PORT', 'REDIS_USERNAME', 'REDIS_PASSWORD', 'REDIS_CONNECT_TIMEOUT_MS', 'REDIS_TLS'];
+const saved: Record<string, string | undefined> = {};
+beforeEach(() => {
+ for (const key of KEYS) {
+ saved[key] = process.env[key];
+ delete process.env[key];
+ }
+});
+afterEach(() => {
+ for (const key of KEYS) {
+ if (saved[key] === undefined) delete process.env[key];
+ else process.env[key] = saved[key];
+ }
+});
describe('buildThrottlerRedisOptions', () => {
- it('maps the redis.* config keys onto the client', () => {
- const options = buildThrottlerRedisOptions(
- makeConfig({
- 'redis.host': 'redis.internal',
- 'redis.port': 6380,
- 'redis.username': 'throttler',
- 'redis.password': 'secret',
- 'redis.connectTimeoutMs': 3000,
- }),
- );
- expect(options).toMatchObject({
+ it('maps the REDIS_* connection env onto the client', () => {
+ Object.assign(process.env, {
+ REDIS_HOST: 'redis.internal',
+ REDIS_PORT: '6380',
+ REDIS_USERNAME: 'throttler',
+ REDIS_PASSWORD: 'secret',
+ REDIS_CONNECT_TIMEOUT_MS: '3000',
+ REDIS_TLS: 'true',
+ });
+ expect(buildThrottlerRedisOptions()).toMatchObject({
host: 'redis.internal',
port: 6380,
username: 'throttler',
password: 'secret',
connectTimeout: 3000,
+ tls: {},
});
});
it('falls back to localhost defaults', () => {
- const options = buildThrottlerRedisOptions(makeConfig({}));
- expect(options).toMatchObject({ host: 'localhost', port: 6379, connectTimeout: 5000 });
+ expect(buildThrottlerRedisOptions()).toMatchObject({ host: 'localhost', port: 6379, connectTimeout: 5000 });
});
it('fails fast instead of queueing or replaying commands across an outage', () => {
- const options = buildThrottlerRedisOptions(makeConfig({}));
+ const options = buildThrottlerRedisOptions();
// Commands issued while disconnected reject immediately so the storage's fail-open path
// engages per request — no offline-queue stall, no post-recovery replay of queued evals.
expect(options.enableOfflineQueue).toBe(false);
@@ -52,7 +60,8 @@ describe('buildThrottlerRedisOptions', () => {
describe('createThrottlerRedisClient', () => {
it('passes the fail-fast options through to the ioredis constructor', () => {
- const client = createThrottlerRedisClient(makeConfig({ 'redis.host': '127.0.0.1' }));
+ process.env.REDIS_HOST = '127.0.0.1';
+ const client = createThrottlerRedisClient();
try {
expect(client.options.enableOfflineQueue).toBe(false);
expect(client.options.autoResendUnfulfilledCommands).toBe(false);
diff --git a/src/common/throttler/throttler-redis.client.ts b/src/common/throttler/throttler-redis.client.ts
index 3989502f9..c7a1fe73b 100644
--- a/src/common/throttler/throttler-redis.client.ts
+++ b/src/common/throttler/throttler-redis.client.ts
@@ -1,5 +1,5 @@
-import { ConfigService } from '@nestjs/config';
import Redis, { RedisOptions } from 'ioredis';
+import { redisConnectionOptions } from '../../config/redis-options';
/**
* Upper bound for one throttler command. The storage fails OPEN on command error, so a bounded
@@ -26,13 +26,9 @@ export const THROTTLER_REDIS_COMMAND_TIMEOUT_MS = 2000;
* offline queue disabled there is nothing to flush, and per-command latency is bounded by
* commandTimeout instead of a retry counter.
*/
-export function buildThrottlerRedisOptions(configService: ConfigService): RedisOptions {
+export function buildThrottlerRedisOptions(): RedisOptions {
return {
- host: configService.get<string>('redis.host', 'localhost'),
- port: configService.get<number>('redis.port', 6379),
- username: configService.get<string>('redis.username'),
- password: configService.get<string>('redis.password'),
- connectTimeout: configService.get<number>('redis.connectTimeoutMs', 5000),
+ ...redisConnectionOptions(),
enableOfflineQueue: false,
autoResendUnfulfilledCommands: false,
commandTimeout: THROTTLER_REDIS_COMMAND_TIMEOUT_MS,
@@ -41,6 +37,6 @@ export function buildThrottlerRedisOptions(configService: ConfigService): RedisO
}
/** Dedicated client for throttler hit-count storage — separate from cache/queue clients. */
-export function createThrottlerRedisClient(configService: ConfigService): Redis {
- return new Redis(buildThrottlerRedisOptions(configService));
+export function createThrottlerRedisClient(): Redis {
+ return new Redis(buildThrottlerRedisOptions());
}
diff --git a/src/config/env-precedence.ts b/src/config/env-precedence.ts
index 8a8cf45bb..847cddabe 100644
--- a/src/config/env-precedence.ts
+++ b/src/config/env-precedence.ts
@@ -199,6 +199,8 @@ export const BLANK_SHADOWED_ENV_KEYS: string[] = [
'REDIS_USERNAME',
'REDIS_PASSWORD',
'REDIS_CONNECT_TIMEOUT_MS',
+ 'REDIS_TLS',
+ 'REDIS_CACHE_DB',
'MAX_CONCURRENT_SESSIONS',
'BAILEYS_AUTH_DIR',
'BAILEYS_BROWSER_NAME',
diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.ts
index 0d94c5cd0..c1d25918d 100644
--- a/src/config/env.validation.spec.ts
+++ b/src/config/env.validation.spec.ts
@@ -554,6 +554,13 @@ describe('validateEnv', () => {
},
);
+ it('accepts only an exact true or false for REDIS_TLS', () => {
+ expect(() => validateEnv({ REDIS_TLS: 'yes' })).toThrow(/REDIS_TLS must be "true" or "false"/);
+ expect(() => validateEnv({ REDIS_TLS: 'true ' })).toThrow(/REDIS_TLS/);
+ expect(() => validateEnv({ REDIS_TLS: 'true' })).not.toThrow();
+ expect(() => validateEnv({ REDIS_TLS: '' })).not.toThrow();
+ });
+
it.each(['INGRESS_RETRY_DELAY_MS', 'REDIS_CACHE_DB'])('rejects a negative or non-integer %s and keeps 0', key => {
for (const bad of ['-1', '1.5', 'abc']) {
expect(() => validateEnv({ [key]: bad })).toThrow(new RegExp(`${key} must be a non-negative integer`));
diff --git a/src/config/env.validation.ts b/src/config/env.validation.ts
index 1fd781a6d..6eafd47d4 100644
--- a/src/config/env.validation.ts
+++ b/src/config/env.validation.ts
@@ -443,6 +443,9 @@ export function validateEnv(config: EnvConfig): EnvConfig {
// Read at boot by the throttler factory (app.module.ts) and CacheService with `=== 'true'`: a
// typo like `ture` silently downgrades rate-limit storage + cache to per-process in-memory.
'REDIS_ENABLED',
+ // `=== 'true'` in redis-options.ts: a typo silently connects in plaintext to a Redis the operator
+ // expected to reach over TLS, which a TLS-only managed Redis then refuses.
+ 'REDIS_TLS',
// Read by the SSRF guard's redirect loop with `=== 'true'`: a typo silently keeps the secure
// default, but an accidental 'true'-ish string is not the flag the operator meant to audit.
'PLUGIN_DOWNLOAD_ALLOW_INSECURE_REDIRECTS',
diff --git a/src/config/redis-options.spec.ts b/src/config/redis-options.spec.ts
new file mode 100644
index 000000000..78dc5973b
--- /dev/null
+++ b/src/config/redis-options.spec.ts
@@ -0,0 +1,59 @@
+import { redisConnectionOptions } from './redis-options';
+
+describe('redisConnectionOptions', () => {
+ const KEYS = [
+ 'REDIS_HOST',
+ 'REDIS_PORT',
+ 'REDIS_USERNAME',
+ 'REDIS_PASSWORD',
+ 'REDIS_CONNECT_TIMEOUT_MS',
+ 'REDIS_TLS',
+ ];
+ const saved: Record<string, string | undefined> = {};
+ beforeEach(() => {
+ for (const key of KEYS) {
+ saved[key] = process.env[key];
+ delete process.env[key];
+ }
+ });
+ afterEach(() => {
+ for (const key of KEYS) {
+ if (saved[key] === undefined) delete process.env[key];
+ else process.env[key] = saved[key];
+ }
+ });
+
+ it('defaults to a plain connection on localhost:6379', () => {
+ expect(redisConnectionOptions()).toEqual({
+ host: 'localhost',
+ port: 6379,
+ username: undefined,
+ password: undefined,
+ connectTimeout: 5000,
+ });
+ });
+
+ it('maps the REDIS_* connection env', () => {
+ Object.assign(process.env, {
+ REDIS_HOST: 'redis.internal',
+ REDIS_PORT: '6380',
+ REDIS_USERNAME: 'u',
+ REDIS_PASSWORD: 'p',
+ REDIS_CONNECT_TIMEOUT_MS: '1234',
+ });
+ expect(redisConnectionOptions()).toEqual({
+ host: 'redis.internal',
+ port: 6380,
+ username: 'u',
+ password: 'p',
+ connectTimeout: 1234,
+ });
+ });
+
+ it('connects over TLS only when REDIS_TLS=true', () => {
+ process.env.REDIS_TLS = 'true';
+ expect(redisConnectionOptions().tls).toEqual({});
+ process.env.REDIS_TLS = 'false';
+ expect(redisConnectionOptions()).not.toHaveProperty('tls');
+ });
+});
diff --git a/src/config/redis-options.ts b/src/config/redis-options.ts
new file mode 100644
index 000000000..482137591
--- /dev/null
+++ b/src/config/redis-options.ts
@@ -0,0 +1,35 @@
+import type { ConnectionOptions as TlsOptions } from 'node:tls';
+
+/**
+ * A plain shape rather than ioredis's RedisOptions: BullMQ types its connection against its own
+ * ioredis copy, and this object has to satisfy both.
+ */
+export interface RedisConnectionOptions {
+ host: string;
+ port: number;
+ username?: string;
+ password?: string;
+ connectTimeout: number;
+ tls?: TlsOptions;
+}
+
+/**
+ * The connection fields every Redis client shares: the cache, the throttler store, the BullMQ
+ * producer and workers, and the WebSocket fan-out pair. Each caller spreads this and adds only its
+ * role-specific behaviour (offline queue, retry strategy, database index).
+ *
+ * Reads process.env directly, not ConfigService: the @Processor decorators and the WebSocket adapter
+ * evaluate it before the DI container exists. load-env has already merged .env and
+ * data/.env.generated by then. REDIS_TLS=true connects over TLS with Node's default CA store (add a
+ * private CA with NODE_EXTRA_CA_CERTS).
+ */
+export function redisConnectionOptions(): RedisConnectionOptions {
+ return {
+ host: process.env.REDIS_HOST || 'localhost',
+ port: parseInt(process.env.REDIS_PORT || '6379', 10),
+ username: process.env.REDIS_USERNAME,
+ password: process.env.REDIS_PASSWORD,
+ connectTimeout: parseInt(process.env.REDIS_CONNECT_TIMEOUT_MS || '5000', 10),
+ ...(process.env.REDIS_TLS === 'true' ? { tls: {} } : {}),
+ };
+}
diff --git a/src/modules/events/redis-io.adapter.spec.ts b/src/modules/events/redis-io.adapter.spec.ts
index 72f1c022c..99243d732 100644
--- a/src/modules/events/redis-io.adapter.spec.ts
+++ b/src/modules/events/redis-io.adapter.spec.ts
@@ -85,6 +85,15 @@ describe('RedisIoAdapter', () => {
expect(typeof opts.retryStrategy).toBe('function');
expect((opts.retryStrategy as (n: number) => number)(100)).toBe(5000);
});
+
+ it('connects over TLS when REDIS_TLS=true', () => {
+ process.env.REDIS_TLS = 'true';
+ try {
+ expect(wsRedisOptions().tls).toEqual({});
+ } finally {
+ delete process.env.REDIS_TLS;
+ }
+ });
});
describe('createIOServer', () => {
diff --git a/src/modules/events/redis-io.adapter.ts b/src/modules/events/redis-io.adapter.ts
index 499a225be..70bdcaec6 100644
--- a/src/modules/events/redis-io.adapter.ts
+++ b/src/modules/events/redis-io.adapter.ts
@@ -4,6 +4,7 @@ import { createAdapter } from '@socket.io/redis-adapter';
import Redis, { type RedisOptions } from 'ioredis';
import type { Server, ServerOptions } from 'socket.io';
import { createLogger } from '../../common/services/logger.service';
+import { redisConnectionOptions } from '../../config/redis-options';
const logger = createLogger('RedisIoAdapter');
@@ -22,11 +23,7 @@ export function isWsRedisEnabled(): boolean {
/** ioredis options for the pub/sub pair, mirroring the throttler/cache connection env exactly. */
export function wsRedisOptions(): RedisOptions {
return {
- host: process.env.REDIS_HOST || 'localhost',
- port: parseInt(process.env.REDIS_PORT || '6379', 10),
- username: process.env.REDIS_USERNAME,
- password: process.env.REDIS_PASSWORD,
- connectTimeout: parseInt(process.env.REDIS_CONNECT_TIMEOUT_MS || '5000', 10),
+ ...redisConnectionOptions(),
// The adapter's SUBSCRIBE connection cannot issue ordinary commands, so a bounded retry that
// gives up (returning null) would strand fan-out permanently after one blip. Reconnect forever
// with capped backoff, matching the cache client.
diff --git a/src/modules/queue/queue.module.ts b/src/modules/queue/queue.module.ts
index 6a3795734..b365d2888 100644
--- a/src/modules/queue/queue.module.ts
+++ b/src/modules/queue/queue.module.ts
@@ -1,5 +1,4 @@
import { Module } from '@nestjs/common';
-import { ConfigModule, ConfigService } from '@nestjs/config';
import { BullModule } from '@nestjs/bullmq';
import { BullBoardModule } from '@bull-board/nestjs';
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter';
@@ -8,6 +7,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { WebhookProcessor } from './processors/webhook.processor';
import { IngressProcessor } from './processors/ingress.processor';
import { QUEUE_NAMES } from './queue-names';
+import { queueConnectionOptions } from './redis-connection';
import { Webhook } from '../webhook/entities/webhook.entity';
import { WebhookDeliveryFailure } from '../webhook/entities/webhook-delivery-failure.entity';
import { IntegrationDeliveryFailure } from '../integration/entities/integration-delivery-failure.entity';
@@ -39,18 +39,7 @@ export const WEBHOOK_QUEUE_JOB_OPTIONS = {
// explicitly for clarity, matching HooksModule above).
PluginsModule,
BullModule.forRootAsync({
- imports: [ConfigModule],
- inject: [ConfigService],
- useFactory: (configService: ConfigService) => ({
- connection: {
- host: configService.get<string>('redis.host', 'localhost'),
- port: configService.get<number>('redis.port', 6379),
- username: configService.get<string>('redis.username'),
- password: configService.get<string>('redis.password'),
- connectTimeout: configService.get<number>('redis.connectTimeoutMs', 5000),
- enableOfflineQueue: false,
- },
- }),
+ useFactory: () => ({ connection: queueConnectionOptions() }),
}),
BullModule.registerQueue({
name: QUEUE_NAMES.WEBHOOK,
diff --git a/src/modules/queue/redis-connection.spec.ts b/src/modules/queue/redis-connection.spec.ts
index 2c0096478..f1ae0b642 100644
--- a/src/modules/queue/redis-connection.spec.ts
+++ b/src/modules/queue/redis-connection.spec.ts
@@ -1,4 +1,9 @@
-import { ingressWorkerConcurrency, workerConnectionOptions, webhookWorkerConcurrency } from './redis-connection';
+import {
+ ingressWorkerConcurrency,
+ queueConnectionOptions,
+ workerConnectionOptions,
+ webhookWorkerConcurrency,
+} from './redis-connection';
describe('workerConnectionOptions (webhook Worker connection)', () => {
const ORIGINAL_ENV = process.env;
@@ -43,6 +48,12 @@ describe('workerConnectionOptions (webhook Worker connection)', () => {
connectTimeout: 1234,
});
});
+
+ it('connects the Worker and the producer over TLS when REDIS_TLS=true', () => {
+ process.env = { ...ORIGINAL_ENV, REDIS_TLS: 'true' };
+ expect(workerConnectionOptions().tls).toEqual({});
+ expect(queueConnectionOptions()).toMatchObject({ tls: {}, enableOfflineQueue: false });
+ });
});
describe('webhookWorkerConcurrency', () => {
diff --git a/src/modules/queue/redis-connection.ts b/src/modules/queue/redis-connection.ts
index f0357f32b..cd216a6bc 100644
--- a/src/modules/queue/redis-connection.ts
+++ b/src/modules/queue/redis-connection.ts
@@ -1,36 +1,25 @@
import { resolveNonNegativeIntEnv } from '../../config/configuration';
+import { redisConnectionOptions, type RedisConnectionOptions } from '../../config/redis-options';
/**
- * BullMQ Redis connection options for the WEBHOOK queue's Worker.
- *
- * The webhook PRODUCER (the Queue) is configured on the shared `BullModule.forRootAsync` connection
- * with `enableOfflineQueue: false`, so `queue.add()` fails fast when Redis is unreachable and the
- * dispatch path can fall back to direct delivery instead of buffering forever.
- *
- * The WORKER must NOT inherit that producer-only fast-fail. Its blocking/internal commands
- * (moveToActive, lock renewal) need to tolerate a brief reconnect during a Redis blip/failover;
- * BullMQ explicitly recommends leaving the offline queue enabled for Worker connections. Because
- * @nestjs/bullmq otherwise builds the Worker from the same shared connection, the WebhookProcessor
- * overrides its connection with these options — host/port/username/password/timeout identical to the producer
- * (same env vars, same defaults as configuration.ts), but with the offline queue left at ioredis's
- * default of `true` so a transient outage no longer throws "Stream isn't writeable" and stalls jobs.
+ * BullMQ connection for the shared producer (`BullModule.forRootAsync` in queue.module.ts): the common
+ * Redis fields plus `enableOfflineQueue: false`, so `queue.add()` fails fast when Redis is unreachable
+ * and the dispatch path can fall back to direct delivery instead of buffering forever.
*/
-export interface WorkerConnectionOptions {
- host: string;
- port: number;
- username?: string;
- password?: string;
- connectTimeout: number;
+export function queueConnectionOptions(): RedisConnectionOptions & { enableOfflineQueue: false } {
+ return { ...redisConnectionOptions(), enableOfflineQueue: false };
}
-export function workerConnectionOptions(): WorkerConnectionOptions {
- return {
- host: process.env.REDIS_HOST || 'localhost',
- port: parseInt(process.env.REDIS_PORT || '6379', 10),
- username: process.env.REDIS_USERNAME,
- password: process.env.REDIS_PASSWORD,
- connectTimeout: parseInt(process.env.REDIS_CONNECT_TIMEOUT_MS || '5000', 10),
- };
+/**
+ * BullMQ connection for the webhook and ingress Workers. It must NOT inherit the producer-only
+ * fast-fail above: the Worker's blocking/internal commands (moveToActive, lock renewal) need to
+ * tolerate a brief reconnect during a Redis blip/failover, and BullMQ recommends leaving the offline
+ * queue enabled for Worker connections. @nestjs/bullmq would otherwise build the Worker from the shared
+ * connection, so each processor passes these options instead; without the override a transient outage
+ * throws "Stream isn't writeable" and stalls jobs.
+ */
+export function workerConnectionOptions(): RedisConnectionOptions {
+ return redisConnectionOptions();
}
/** Default number of webhook deliveries the Worker processes in parallel. */