Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (60 loc) · 1.66 KB
/
Copy pathindex.js
File metadata and controls
68 lines (60 loc) · 1.66 KB
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
const { request, logErr } = require("distraught");
const DEFAULT_INTERVAL = 60000;
const MAX_ATTEMPTS = 2;
function uptime(config = {}) {
return {
request(payload, attempt = 1) {
if (!payload.url) {
throw new Error("URL is required for uptime monitoring requests");
}
if (!payload.method) {
payload.method = "GET";
}
payload.logErrors = false; // turn off default logging
return request(payload).catch(err => {
if (
attempt <
(config.hasOwnProperty("maxAttempts")
? config.maxAttempts
: MAX_ATTEMPTS)
) {
return this.request(payload, attempt + 1);
}
let statusOrMessage = err.message;
if (err.response && err.response.status) {
statusOrMessage = `status code ${err.response.status}`;
}
logErr(new Error(`${payload.url} responded with ${statusOrMessage}`), {
url: payload.url,
debug: payload.debug,
data: payload.data || ""
});
});
},
init() {
if (!config) {
throw new Error(
"Uptime config must be specified. See docs for details."
);
}
if (
!(
config.monitoring &&
config.monitoring.length &&
Array.isArray(config.monitoring)
)
) {
throw new Error(
"Uptime config monitoring must be specified. See docs for details."
);
}
config.monitoring.forEach(site => {
setInterval(
() => this.request(site.request),
site.interval || DEFAULT_INTERVAL
);
});
}
};
}
module.exports = uptime;