-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
65 lines (60 loc) · 2 KB
/
index.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
-"use strict";
/*
PRE-REQUISITES
*/
const log = require ('./logger.js');
const dns = require('node:dns').promises;
const Redis = require ('ioredis');
const net = require ('node:net');
/*
REQUIREMENTS
*/
var keydb, discovery = null;
keydb = new Redis (process.env.KRAUSE_KEYDB_SOCKET);
keydb.once ('connect', () => {
discovery = setInterval (discover, process.env.KRAUSE_DISCOVERY_INTERVAL);
})
process.on ('SIGTERM', () => {
if (keydb) keydb.disconnect ();
if (discovery) clearInterval (discovery);
});
// docker swarm endpoint for resolution
const endpoint = 'tasks.' + process.env.KRAUSE_KEYDB_SERVICE + '.';
// automatic discovery
function discover () {
log.debug ('Hitting endpoint ' + endpoint + ' ...');
dns.resolve (endpoint).then (async function main (discovered) {
log.debug (`Got tasks ${discovered}`);
// get existing peers
log.debug ('Checking role...');
let role = await keydb.role ();
let peers = [];
for (let peer of role) {
// role returns an array of arrays where
// index 1 of the subarray is the IP
if (net.isIPv4 (peer[1])) {
peers.push (peer[1]);
}
}
log.debug (`Got peers ${peers}`);
// add new tasks
for (let taskAddress of discovered) {
if (!peers.includes (taskAddress)) {
log.info (`Setting REPLICAOF ${taskAddress} ${process.env.KRAUSE_KEYDB_PORT}`);
keydb.replicaof (taskAddress, process.env.KRAUSE_KEYDB_PORT);
}
}
// remove old peers
for (let peer of peers) {
if (!discovered.includes (peer)) {
log.info (`Removing REPLICAOF ${peer} ${process.env.KRAUSE_KEYDB_PORT}`);
keydb.replicaof ('REMOVE', peer, process.end.KRAUSE_KEYDB_PORT);
}
}
}).catch ((error) => {
// do not throw if 0 tasks are discovered
if (error.code != 'ENOTFOUND') {
throw error;
}
});
}