42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import Redis from "ioredis";
|
|
|
|
declare global {
|
|
// eslint-disable-next-line no-var
|
|
var __redisClient: Redis | undefined;
|
|
}
|
|
|
|
export function getRedis(): Redis {
|
|
if (!globalThis.__redisClient) {
|
|
globalThis.__redisClient = new Redis({
|
|
host: process.env.REDIS_HOST ?? "localhost",
|
|
port: parseInt(process.env.REDIS_PORT ?? "6379", 10),
|
|
password: process.env.REDIS_PASSWORD,
|
|
retryStrategy: (times) => Math.min(times * 200, 30_000),
|
|
lazyConnect: false,
|
|
enableOfflineQueue: true,
|
|
maxRetriesPerRequest: 3,
|
|
});
|
|
|
|
globalThis.__redisClient.on("error", (err) => {
|
|
console.error("[redis] connection error:", err.message);
|
|
});
|
|
}
|
|
|
|
return globalThis.__redisClient;
|
|
}
|
|
|
|
/**
|
|
* BullMQ requires a dedicated connection with maxRetriesPerRequest: null.
|
|
* Returns plain ConnectionOptions (not an ioredis instance) to avoid the
|
|
* dual-ioredis type conflict between the top-level ioredis and BullMQ's
|
|
* bundled copy.
|
|
*/
|
|
export function createBullMQConnection() {
|
|
return {
|
|
host: process.env.REDIS_HOST ?? "localhost",
|
|
port: parseInt(process.env.REDIS_PORT ?? "6379", 10),
|
|
password: process.env.REDIS_PASSWORD,
|
|
maxRetriesPerRequest: null as null,
|
|
enableOfflineQueue: true,
|
|
};
|
|
}
|