46 lines
1.1 KiB
JavaScript
46 lines
1.1 KiB
JavaScript
const logger = require("./logger.js");
|
|
const timediff = require("./timediff.js");
|
|
|
|
async function isOnline() {
|
|
const url = global.config.draytek;
|
|
let online = false;
|
|
logger.info("checking if draytek is responding on url '" + url + "'...");
|
|
const timestamp = Date.now();
|
|
try {
|
|
const controller = new AbortController();
|
|
const timeoutId = setTimeout(async function () {
|
|
logger.debug(
|
|
"request to draytek timed out after " +
|
|
timediff.inSeconds(timestamp) +
|
|
" seconds!"
|
|
);
|
|
controller.abort();
|
|
}, global.config.timeouts.request);
|
|
|
|
const result = await fetch(url, {
|
|
signal: controller.signal,
|
|
});
|
|
clearTimeout(timeoutId);
|
|
logger.info(
|
|
"draytek responded with status code '" +
|
|
result.status +
|
|
"' after " +
|
|
timediff.inSeconds(timestamp) +
|
|
" seconds!"
|
|
);
|
|
online = result.status === 200;
|
|
} catch (err) {
|
|
if (err.name !== "AbortError") {
|
|
logger.error(
|
|
"encountered an error checking if draytek is responding (" + err + ")"
|
|
);
|
|
}
|
|
}
|
|
return online;
|
|
}
|
|
|
|
// exports
|
|
module.exports = {
|
|
isOnline,
|
|
};
|