diff --git a/Dockerfile b/Dockerfile index 4c2f202..5c202e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -42,6 +42,8 @@ RUN node -v && npm -v COPY package.json package-lock.json ./ COPY moleculer.config.ts ./ COPY tsconfig.json ./ +COPY scripts ./scripts +RUN chmod +x ./scripts/* # Install application dependencies RUN npm install @@ -50,9 +52,8 @@ RUN npm install -g typescript ts-node # Copy the rest of the application files COPY . . -# Build and clean up -RUN npm run build \ - && npm prune +# clean up +RUN npm prune # Expose the application's port EXPOSE 3000 diff --git a/config/redis.config.ts b/config/redis.config.ts index 3d6cf0b..26a4cb8 100644 --- a/config/redis.config.ts +++ b/config/redis.config.ts @@ -1,10 +1,47 @@ import { createClient } from "redis"; -const redisURL = new URL(process.env.REDIS_URI); +import { URL } from "url"; -const pubClient = createClient({ url: `redis://${redisURL.hostname}:6379` }); -(async () => { - await pubClient.connect(); -})(); +// Ensure that the REDIS_URI environment variable is set +const redisURL = process.env.REDIS_URI ? new URL(process.env.REDIS_URI) : null; +if (!redisURL) { + throw new Error("REDIS_URI environment variable is not set."); +} + +// Function to create a Redis client +const createRedisClient = (url) => { + const client = createClient({ url }); + + client.on("error", (err) => { + console.error("Redis Client Error", err); + }); + + client.on("connect", () => { + console.log("Connected to Redis:", url); + }); + + client.on("reconnecting", () => { + 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; +}; + +// Create publisher and subscriber clients +const pubClient = createRedisClient(process.env.REDIS_URI); const subClient = pubClient.duplicate(); +// Ensure subscriber client handles connection and errors +subClient.on("error", (err) => { + console.error("Redis Subscriber Client Error", err); +}); + +subClient.connect().catch((err) => { + console.error("Failed to connect Redis Subscriber:", err); +}); + export { subClient, pubClient }; diff --git a/moleculer.config.ts b/moleculer.config.ts index 69e2ce2..b6e15d1 100644 --- a/moleculer.config.ts +++ b/moleculer.config.ts @@ -90,7 +90,7 @@ const brokerConfig: BrokerOptions = { // More info: https://moleculer.services/docs/0.14/networking.html // Note: During the development, you don't need to define it because all services will be loaded locally. // In production you can set it via `TRANSPORTER=nats://localhost:4222` environment variable. - transporter: process.env.REDIS_URI || "redis://localhost:6379", + transporter: process.env.REDIS_URI || "redis://127.0.0.1:6379", // Define a cacher. // More info: https://moleculer.services/docs/0.14/caching.html