-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (52 loc) · 1.92 KB
/
index.js
File metadata and controls
60 lines (52 loc) · 1.92 KB
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
/**
* Created by vd on 20.11.2017.
*/
const config = require('./config'),
ENV = process.env.NODE_ENV || "dev";
const redis = require("redis");
const client = redis.createClient(config.redisPort[ENV]);
const errorHandler = require("./errorHandler");
const CommonClient = require("./commonClient");
// if we running getErrors argument
let getErrors = false;
process.argv.forEach(function (val, index, array) {
if (val === "getErrors") {
getErrors = true;
CommonClient.getErrors(function (resp) {
console.log(resp);
process.exit();
})
}
});
if(getErrors) return;
const generatorMode = require("./generatorMode"),
WorkerMode = require("./workerMode");
const commonClient = new CommonClient();
const CLIENT_ID = commonClient.clientNumber;
// Send keepalive messages to know, what client's is online now
commonClient.sendKeepalive();
let timerId = setTimeout(function tick() {
commonClient.sendKeepalive();
timerId = setTimeout(tick, config.keepAliveTimeout[ENV]);
}, config.keepAliveTimeout[ENV]);
let worker = new WorkerMode(onNoMessages, CLIENT_ID);
// If we not receive any message before times out
function onNoMessages(timeout) {
console.log(`No message's from generator after ${timeout}ms`);
// Get all client's online to choose new generator
commonClient.getAllClientsOnline((reply) => {
const newGeneratorId = commonClient.getNewGenerator(reply);
console.log(`New generator will be #${newGeneratorId}`);
// Make new generator from the elder client
// Make listener's from other client's
if (newGeneratorId === CLIENT_ID) {
generatorMode.start(CLIENT_ID, () => {
worker = new WorkerMode(onNoMessages, CLIENT_ID);
});
} else {
worker = new WorkerMode(onNoMessages, CLIENT_ID);
}
});
}
client.subscribe(config.channelName[ENV]);
client.on("error", errorHandler);