-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
122 lines (114 loc) · 4.91 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const http = require('http');
let EventEmitter = require('events').EventEmitter;
const { replacePortNumber } = require("../utils");
class HealthCheckerUtility extends EventEmitter {
inMemoryConfig;
isProxyAlive = false;
isApiServerAlive = false;
isWebserverAlive = false;
pingTime = 2000;
constructor(inMemoryConfigObj) {
super();
if(inMemoryConfigObj) {
this.inMemoryConfig = inMemoryConfigObj;
}
// set up interval timers to check status
this.apiServerAliveTimer = setInterval(this.checkIfApiServerAlive.bind(this), this.pingTime);
this.webServerAliveTimer = setInterval(this.checkIfWebServerAlive.bind(this), this.pingTime);
this.proxyAliveTimer = setInterval(this.checkIfProxyServerAlive.bind(this), this.pingTime);
}
get status() {
return {
"isProxyAlive": this.isProxyAlive,
"isApiServerAlive": this.isApiServerAlive,
"isWebserverAlive": this.isWebserverAlive
}
}
get config() {
return this.inMemoryConfig.config;
}
checkIfWebServerAlive() {
let reqUrl = this.config.web.url;
let req = http.get(reqUrl, (res => {
//console.log('checkIfWebServerAlive.response: ', res.statusCode);
//res.on('end',(() => {
if(res.statusCode === 200) {
if(this.isWebserverAlive !== true) {
this.isWebserverAlive = true;
this.emit('statusChange', this.status);
}
//console.log('Response ended: \n', _dataRes);
} else if(this.isWebserverAlive == true) {
this.isWebserverAlive = false;
this.emit('statusChange', this.status);
}
//}).bind(this));
}).bind(this)).on('error', (error => {
//console.log('checkIfWebServerAlive: '+ error.code +' | ['+ reqUrl +']');
if(this.isWebserverAlive == true) {
this.isWebserverAlive = false;
this.emit('statusChange', this.status);
}
this.isWebserverAlive = false;
//console.log(error)
}).bind(this))
}
checkIfApiServerAlive() {
let reqUrl = this.config.web.apiServerUrl+'/server-info';
let req = http.get(reqUrl, (res => {
//console.log('checkIfApiServerAlive.response: ', res.statusCode);
//res.on('end',(() => {
if(res.statusCode === 200) {
if(this.isApiServerAlive !== true) {
this.isApiServerAlive = true;
this.emit('statusChange', this.status);
}
//console.log('Response ended: \n', _dataRes);
} else if(this.isApiServerAlive == true) {
this.isApiServerAlive = false;
this.emit('statusChange', this.status);
}
//}).bind(this));
}).bind(this)).on('error', (error => {
//console.log('checkIfApiServerAlive: '+ error.code +' | ['+ reqUrl +']');
if(this.isApiServerAlive == true) {
this.isApiServerAlive = false;
this.emit('statusChange', this.status);
}
this.isApiServerAlive = false;
//console.log(error)
}).bind(this))
}
checkIfProxyServerAlive() {
let reqUrl = this.config.web.url + '/health/proxy';
//let reqUrl = replacePortNumber(this.config.configServer.port, (this.config.web.url + '/health/proxy'));
//console.log('checkIfProxyServerAlive: port', this.config.configServer.port);
//console.log('checkIfProxyServerAlive: url', reqUrl);
//console.log(this.config);
let req = http.get(reqUrl, (res => {
//console.log('checkIfProxyServerAlive.response: ', res.statusCode);
//res.on('end',(() => {
if(res.statusCode === 200) {
//console.log('\t isProxyAlive('+ this.isProxyAlive +')', (this.isProxyAlive !== true));
if(this.isProxyAlive !== true) {
this.isProxyAlive = true;
this.emit('statusChange', this.status);
}
//console.log('Response ended: \n', _dataRes);
} else if(this.isProxyAlive == true) {
this.isProxyAlive = false;
this.emit('statusChange', this.status);
}
//}).bind(this));
}).bind(this)).on('error', (error => {
//console.log('checkIfProxyServerAlive: '+ error.code +' | ['+ reqUrl +']');
if(this.isProxyAlive == true) {
this.isProxyAlive = false;
this.emit('statusChange', this.status);
}
this.isProxyAlive = false;
//console.log(error)
}).bind(this))
}
}
module.exports = HealthCheckerUtility;