File size: 1,289 Bytes
a966957
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99f6bca
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Redis from 'ioredis';
import { logger } from '../logger';

function makeRedis(bullmq = false) {
    const client = process.env.REDIS_URL
        ? new Redis(process.env.REDIS_URL, { maxRetriesPerRequest: bullmq ? null : 3 })
        : new Redis({
            host:     process.env.REDIS_HOST || 'localhost',
            port:     parseInt(process.env.REDIS_PORT || '6379'),
            username: process.env.REDIS_USERNAME || 'default',
            password: process.env.REDIS_PASSWORD || undefined,
            tls:      process.env.REDIS_TLS === 'true' ? {} : undefined,
            maxRetriesPerRequest: bullmq ? null : 3,
        });
    client.on('error', (err) => logger.error({ err }, `[REDIS] ${bullmq ? 'BullMQ' : 'Cache'} connection error`));
    return client;
}

/** Standard caching client (organization, normalization, meta-status) */
export const redis = makeRedis(false);

/** BullMQ-compatible client — maxRetriesPerRequest: null required by BullMQ */
export const redisForBullMQ = makeRedis(true);

/** Dedicated subscriber for SSE — each sub needs its own connection */
export function createSubscriber() {
    return makeRedis(false);
}

/** Channel name for org-scoped real-time events */
export const orgChannel = (orgId: string) => `org:${orgId}:events`;