Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions order/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,29 @@ class App {
}

async setupOrderConsumer() {
console.log("Connecting to RabbitMQ...");

const delay = config.rabbitMQConnectDelayMs;
setTimeout(async () => {

const scheduleRetry = () => {
console.log(`Retrying RabbitMQ connection in ${delay}ms...`);
setTimeout(connectToRabbitMQ, delay);
};

const connectToRabbitMQ = async () => {
console.log("Connecting to RabbitMQ...");

try {
const connection = await amqp.connect(config.rabbitMQURI);
console.log("Connected to RabbitMQ");

connection.on("close", () => {
console.warn("RabbitMQ connection closed");
scheduleRetry();
});

connection.on("error", (error) => {
console.error("RabbitMQ connection error:", error.message);
});

const channel = await connection.createChannel();
await channel.assertQueue(config.orderQueue, { durable: true });

Expand Down Expand Up @@ -65,8 +81,11 @@ class App {
});
} catch (err) {
console.error("Failed to connect to RabbitMQ:", err.message);
scheduleRetry();
}
}, delay);
};

setTimeout(connectToRabbitMQ, delay);
}

start() {
Expand Down
Loading