-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.js
42 lines (40 loc) · 1.35 KB
/
cluster.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
function Cluster(clusterResult) {
var nodesString = clusterResult.split('\n');
var char = clusterResult.charCodeAt(clusterResult.length - 1);
this.nodes = [];
for (var index = 0; index < nodesString.length; index++) {
if(nodesString[index].length == 0) continue;
var nodeFields = nodesString[index].split(' ');
var node = {};
node.id = nodeFields[0];
node.name = nodeFields[1];
var hostport = node.name.split(':');
node.host = hostport[0];
node.port = +hostport[1];
parseFlags(nodeFields[2], node);
node.slaveOf = nodeFields[3];
node.configEpoch = +nodeFields[6];
node.slots = [];
for (var slotIndex = 8; slotIndex < nodeFields.length; slotIndex++) {
var range = nodeFields[slotIndex];
node.slots.push(range);
}
this.nodes.push(node);
this.nodes[node.id] = node;
}
}
module.exports.Cluster = Cluster;
function parseFlags(flags, node) {
var tmp = flags.split(',');
for (var index = 0; index < tmp.length; index++) {
var flag = tmp[index];
if (flag == 'master') {
node.isMaster = true;
} else if (flag == 'fail' || flag == 'fail?') {
node.failed = true;
} else if (flag == 'noaddr') {
node.hasNoAddr = true;
}
}
}