-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonlineServer.js
73 lines (67 loc) · 2.18 KB
/
onlineServer.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
const request = require('request');
//to find the lowest priority online server among various others
function findServer(servers){
return new Promise((resolve, reject)=>{
var onlineServers = getOnlineServers(servers);
onlineServers
.then(data =>{
if(data.length){
console.log('online servers are', data);
let sortedServers = getSortedServers(data)
resolve(sortedServers[0])
}else{
reject({message:"All servers are offline"})
}
})
})
}
// to get the all online servers
const getOnlineServers = (servers) => {
var count = 0;
var onlineServers = []
return new Promise((resolve, reject)=>{
for (let i = 0; i < servers.length; i++) {
let isOnlineServer = isOnline(servers[i].url);
isOnlineServer
.then(resp => {
onlineServers.push(servers[i])
count++;
if (count == servers.length) {
resolve(onlineServers)
}
})
.catch(err => {
count++;
if (count == servers.length) {
resolve(onlineServers)
}
})
}
})
}
// to check if a server given URL is online or not
function isOnline(link) {
return new Promise((resolve, reject) => {
let req = request(link, { json: true }, (err, res, body) => {
if (err) reject(err);
if (res) {
if(res.statusCode > 199 && res.statusCode < 300 ) resolve((true));
else reject(false);
}
})
//after 5 sec. kill the request
setTimeout(()=>{
req.abort();
reject({message:"req timeout"})
},5000)
})
}
const getSortedServers = (data)=>{
let sortServers = data.sort((a, b) => Number(a.priority) - Number(b.priority));
return sortServers;
}
module.exports = { //export the functions from module to access functions in other files
findServer,
getOnlineServers,
isOnline
}