mirror of
https://github.com/formbricks/formbricks.git
synced 2026-01-03 17:09:58 -06:00
Co-authored-by: Harsh Bhat <90265455+harshsbhat@users.noreply.github.com> Co-authored-by: Johannes <johannes@formbricks.com> Co-authored-by: Piyush Gupta <56182734+gupta-piyush19@users.noreply.github.com> Co-authored-by: Aditya <162564995+Naidu-4444@users.noreply.github.com> Co-authored-by: Piyush Gupta <piyushguptaa2z123@gmail.com> Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com> Co-authored-by: Dhruwang Jariwala <67850763+Dhruwang@users.noreply.github.com> Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com> Co-authored-by: Jakob Schott <154420406+jakobsitory@users.noreply.github.com> Co-authored-by: Suraj <surajsuthar0067@gmail.com> Co-authored-by: Kshitij Sharma <63995641+kshitij-codes@users.noreply.github.com> Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com> Co-authored-by: Matti Nannt <mail@matthiasnannt.com>
70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import { createClient } from "redis";
|
|
import { logger } from "@formbricks/logger";
|
|
|
|
type RedisClient = ReturnType<typeof createClient>;
|
|
|
|
const REDIS_URL = process.env.REDIS_URL;
|
|
|
|
let client: RedisClient | null = null;
|
|
|
|
if (REDIS_URL) {
|
|
client = createClient({
|
|
url: REDIS_URL,
|
|
socket: {
|
|
reconnectStrategy: (retries) => {
|
|
logger.info(`Redis reconnection attempt ${retries}`);
|
|
|
|
// For the first 5 attempts, use exponential backoff with max 5 second delay
|
|
if (retries <= 5) {
|
|
return Math.min(retries * 1000, 5000);
|
|
}
|
|
|
|
// After 5 attempts, use a longer delay but never give up
|
|
// This ensures the client keeps trying to reconnect when Redis comes back online
|
|
logger.info("Redis reconnection using extended delay (30 seconds)");
|
|
return 30000; // 30 second delay for persistent reconnection attempts
|
|
},
|
|
},
|
|
});
|
|
|
|
client.on("error", (err) => {
|
|
logger.error("Redis client error:", err);
|
|
});
|
|
|
|
client.on("connect", () => {
|
|
logger.info("Redis client connected");
|
|
});
|
|
|
|
client.on("reconnecting", () => {
|
|
logger.info("Redis client reconnecting");
|
|
});
|
|
|
|
client.on("ready", () => {
|
|
logger.info("Redis client ready");
|
|
});
|
|
|
|
client.on("end", () => {
|
|
logger.info("Redis client disconnected");
|
|
});
|
|
|
|
// Connect immediately
|
|
client.connect().catch((err) => {
|
|
logger.error("Initial Redis connection failed:", err);
|
|
});
|
|
}
|
|
|
|
export const getRedisClient = (): RedisClient | null => {
|
|
if (!client?.isReady) {
|
|
logger.warn("Redis client not ready, operations will be skipped");
|
|
return null;
|
|
}
|
|
return client;
|
|
};
|
|
|
|
export const disconnectRedis = async (): Promise<void> => {
|
|
if (client) {
|
|
await client.disconnect();
|
|
client = null;
|
|
}
|
|
};
|