This repository has been archived by the owner on Jul 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
118 lines (88 loc) · 2.37 KB
/
main.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
const sleep = require('sleep-promise');
// Include all checks
const checks = {
telegraph: require('./checks/telegraph.js'),
telescope: require('./checks/telescope.js')
};
const dcs = require('./checks/dc.js')
// Shared status storage
let status = {dcs: [], services: {}};
// Initialize status layout
for (let i in checks) {
status.services[i] = {
ping: {},
functionality: []
};
}
status.dcs = dcs.dcs.map(e=>{
return {
ping: { },
functionality: [],
location: e.location
};
});
// Loop all pings
async function doPings() {
const minDelay = 15 * 1000;
while (true) {
const start = Date.now();
// Ping services
for (let i in checks) {
try {
// Perform functionality tests
status.services[i].ping = await checks[i].ping();
} catch (checkError) {
console.error('Runner failed to ping', i)
}
}
// Ping dcs
for (let i in dcs.dcs) {
try {
// Perform functionality tests
status.dcs[i].ping = await dcs.ping(dcs.dcs[i].ipv4);
} catch (checkError) {
console.error('Runner failed to ping dc', i+1)
}
}
const end = Date.now() - start;
await sleep(end > minDelay ? 0 : minDelay - end);
}
} setImmediate(doPings);
// Loop all functionality checks
async function doChecks() {
const minDelay = 60*1000;
while(true) {
const start = Date.now();
for (let i in checks) {
try {
// Perform functionality tests
status.services[i].functionality = await checks[i].test();
} catch (checkError) {
console.error('Runner failed to perform functionality tests for',i)
}
}
const end = Date.now() - start;
await sleep(end > minDelay ? 0 : minDelay - end);
}
} setImmediate(doChecks);
// HTTP API
const package = require('./package.json');
const express = require('express');
const app = express();
app.get('/', async (req, res) => {
res.redirect(package.homepage);
})
app.get('/api/v1/all', async (req, res) => {
res.setHeader('Content-Type', 'application/json')
res.json(status)
})
app.get('/api/v1/dcs', async (req, res) => {
res.setHeader('Content-Type', 'application/json')
res.json(status.dcs)
})
app.get('/api/v1/services', async (req, res) => {
res.setHeader('Content-Type','application/json')
res.json(status.services)
})
app.listen(8080);
console.log('Finished initializing...');