-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (61 loc) · 1.98 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const amqp = require("amqplib/callback_api");
const cron = require("node-cron");
const os = require("os");
//Connect to the IP address of the rabbitmq container
const url = "amqp://guest:[email protected]";
const sendContainerIdToOthers = () => {
/**
* method for sending containerId to other nodes
* @param {null}
* @returns {null}
*
*/
console.log(`My id is ${os.hostname()}`);
amqp.connect(url, (error0, connection) => {
if (error0) throw error0;
connection.createChannel((error1, channel) => {
if (error1) throw error1;
const exchange = "logs";
const msg = `My id is ${os.hostname()}`;
channel.assertExchange(exchange, "fanout", { durable: false });
channel.publish(exchange, "", Buffer.from(msg));
});
});
};
amqp.connect(url, (error0, connection) => {
if (error0) throw error0;
connection.createChannel((error1, channel) => {
if (error1) throw error1;
const exchange = "logs";
channel.assertExchange(exchange, "fanout", { durable: false });
channel.assertQueue("", { exclusive: true }, (error2, q) => {
if (error2) throw error2;
console.log(`Waiting for messages in ${q.queue}`);
channel.bindQueue(q.queue, exchange, "");
let resultSet = new Set();
//Clear the set every 15 seconds
setInterval(()=>{resultSet = new Set()}, 15000);
channel.consume(
q.queue,
msg => {
if (msg.content) {
console.log(`received: ${msg.content.toString()}`);
const id = msg.content
.toString()
.split("is")[1]
.trim();
resultSet.add(id);
console.log("Container id's", resultSet);
const findMaster = Array.from(resultSet).sort();
console.log(`Our Master Node is ${findMaster[0]}`);
}
},
{
noAck: true
}
);
});
});
});
//Send message every 10 seconds
cron.schedule("10 * * * * *", () => sendContainerIdToOthers());