-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats-collector.js
62 lines (53 loc) · 1.49 KB
/
stats-collector.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
let url = require('url');
let socketClusterClient = require('socketcluster-client');
let trailingPortNumberRegex = /:[0-9]+$/;
function StatsCollector(sccInstanceClusterDetailsList) {
this.clients = [];
this.sccInstanceClusterDetailsList = sccInstanceClusterDetailsList;
let targets = this.sccInstanceClusterDetailsList
.filter((instanceDetails) => {
return instanceDetails.type === 'worker';
});
targets.forEach((instanceDetails) => {
let clientOptions = {
hostname: '127.0.0.1',
port: instanceDetails.port
};
let socket = socketClusterClient.create(clientOptions);
socket.instanceName = instanceDetails.name;
(async () => {
for await (let {error} of socket.listener('error')) {
console.error(error);
}
})();
this.clients.push(socket);
});
}
StatsCollector.prototype.collectStats = function () {
let collectStatsPromises = [];
this.clients.forEach((socket) => {
collectStatsPromises.push(
(async () => {
let stats = await socket.invoke('getStats');
return {
instanceName: socket.instanceName,
stats
};
})()
);
});
return Promise.all(collectStatsPromises)
.then((results) => {
let stats = {};
results.forEach((result) => {
stats[result.instanceName] = result.stats;
});
return stats;
});
};
StatsCollector.prototype.destroy = function () {
this.clients.forEach((socket) => {
socket.disconnect();
});
};
module.exports = StatsCollector;