🔧 Fixed REDIS_URI
This commit is contained in:
@@ -1,47 +1,24 @@
|
|||||||
import { createClient } from "redis";
|
import { createClient } from "redis";
|
||||||
import { URL } from "url";
|
|
||||||
|
|
||||||
// Ensure that the REDIS_URI environment variable is set
|
|
||||||
const redisURL = process.env.REDIS_URI;
|
const redisURL = process.env.REDIS_URI;
|
||||||
if (!redisURL) {
|
if (!redisURL) throw new Error("REDIS_URI environment variable is not set.");
|
||||||
throw new Error("REDIS_URI environment variable is not set.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Function to create a Redis client
|
|
||||||
const createRedisClient = (url) => {
|
const createRedisClient = (url) => {
|
||||||
const client = createClient({ url });
|
const client = createClient({ url });
|
||||||
console.log(client)
|
|
||||||
client.on("error", (err) => {
|
|
||||||
console.error("Redis Client Error", err);
|
|
||||||
});
|
|
||||||
|
|
||||||
client.on("connect", () => {
|
client.on("error", (err) => console.error("Redis Client Error", err));
|
||||||
console.log("Connected to Redis:", url);
|
client.on("connect", () => console.log("Connected to Redis:", url));
|
||||||
});
|
client.on("reconnecting", () => console.log("Reconnecting to Redis..."));
|
||||||
|
|
||||||
client.on("reconnecting", () => {
|
client.connect().catch((err) => console.error("Failed to connect to Redis:", err));
|
||||||
console.log("Reconnecting to Redis...");
|
|
||||||
});
|
|
||||||
|
|
||||||
// Attempt to connect with error handling
|
|
||||||
client.connect().catch((err) => {
|
|
||||||
console.error("Failed to connect to Redis:", err);
|
|
||||||
});
|
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create publisher and subscriber clients
|
const pubClient = createRedisClient(redisURL);
|
||||||
const pubClient = createRedisClient(process.env.REDIS_URI);
|
|
||||||
const subClient = pubClient.duplicate();
|
const subClient = pubClient.duplicate();
|
||||||
|
|
||||||
// Ensure subscriber client handles connection and errors
|
subClient.on("error", (err) => console.error("Redis Subscriber Client Error", err));
|
||||||
subClient.on("error", (err) => {
|
subClient.connect().catch((err) => console.error("Failed to connect Redis Subscriber:", err));
|
||||||
console.error("Redis Subscriber Client Error", err);
|
|
||||||
});
|
|
||||||
|
|
||||||
subClient.connect().catch((err) => {
|
|
||||||
console.error("Failed to connect Redis Subscriber:", err);
|
|
||||||
});
|
|
||||||
|
|
||||||
export { subClient, pubClient };
|
export { subClient, pubClient };
|
||||||
|
|||||||
Reference in New Issue
Block a user