This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
skynet-webportal/packages/health-check/src/checks/critical.js

112 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-04-27 16:30:51 +00:00
const got = require("got");
const FormData = require("form-data");
const { calculateElapsedTime, getResponseContent } = require("../utils");
2021-05-24 14:27:30 +00:00
const { SkynetClient } = require("skynet-js");
const skynetClient = new SkynetClient(process.env.SKYNET_PORTAL_API);
const exampleSkylink = "AACogzrAimYPG42tDOKhS3lXZD8YvlF8Q8R17afe95iV2Q";
2020-09-09 12:25:00 +00:00
// uploadCheck returns the result of uploading a sample file
async function uploadCheck(done) {
const time = process.hrtime();
2021-04-27 16:30:51 +00:00
const form = new FormData();
const payload = Buffer.from(new Date()); // current date to ensure data uniqueness
const data = { up: false };
2021-04-27 16:30:51 +00:00
form.append("file", payload, { filename: "time.txt", contentType: "text/plain" });
2021-04-27 16:30:51 +00:00
try {
const response = await got.post(`${process.env.SKYNET_PORTAL_API}/skynet/skyfile`, { body: form });
2021-04-27 16:30:51 +00:00
data.statusCode = response.statusCode;
data.up = true;
data.ip = response.ip;
2021-04-27 16:30:51 +00:00
} catch (error) {
data.statusCode = error.response?.statusCode || error.statusCode || error.status;
data.errorMessage = error.message;
data.errorResponseContent = getResponseContent(error.response);
data.ip = error?.response?.ip ?? null;
2021-04-27 16:30:51 +00:00
}
2021-05-24 14:27:30 +00:00
done({ name: "upload_file", time: calculateElapsedTime(time), ...data });
}
// websiteCheck checks whether the main website is working
async function websiteCheck(done) {
return genericAccessCheck("website", process.env.SKYNET_PORTAL_API, done);
2020-09-09 12:25:00 +00:00
}
// downloadCheck returns the result of downloading the hard coded link
async function downloadCheck(done) {
2021-05-24 14:27:30 +00:00
const url = await skynetClient.getSkylinkUrl(exampleSkylink);
return genericAccessCheck("skylink", url, done);
}
// skylinkSubdomainCheck returns the result of downloading the hard coded link via subdomain
async function skylinkSubdomainCheck(done) {
const url = await skynetClient.getSkylinkUrl(exampleSkylink, { subdomain: true });
2021-05-24 14:47:39 +00:00
return genericAccessCheck("skylink_via_subdomain", url, done);
2021-05-24 14:27:30 +00:00
}
// skylinkSubdomainCheck returns the result of downloading the hard coded link via subdomain
async function handshakeSubdomainCheck(done) {
const url = await skynetClient.getHnsUrl("note-to-self", { subdomain: true });
return genericAccessCheck("hns_via_subdomain", url, done);
}
async function accountHealthCheck(done) {
2020-09-09 12:25:00 +00:00
const time = process.hrtime();
const data = { up: false };
2020-09-09 12:25:00 +00:00
try {
2021-05-24 14:27:30 +00:00
const response = await got(`${process.env.SKYNET_DASHBOARD_URL}/health`, { responseType: "json" });
2020-09-09 12:25:00 +00:00
data.statusCode = response.statusCode;
2021-05-24 14:27:30 +00:00
data.response = response.body;
data.up = response.body.dbAlive === true;
data.ip = response.ip;
2020-09-09 12:25:00 +00:00
} catch (error) {
data.statusCode = error?.response?.statusCode || error.statusCode || error.status;
data.errorMessage = error.message;
data.errorResponseContent = getResponseContent(error.response);
data.ip = error?.response?.ip ?? null;
2020-09-09 12:25:00 +00:00
}
done({
2021-05-24 14:27:30 +00:00
name: "account_health",
2020-09-09 12:53:05 +00:00
time: calculateElapsedTime(time),
...data,
2020-09-09 12:25:00 +00:00
});
}
2021-05-24 14:27:30 +00:00
async function genericAccessCheck(name, url, done) {
const time = process.hrtime();
2021-05-24 14:27:30 +00:00
const data = { up: false, url };
try {
2021-05-24 14:27:30 +00:00
const response = await got(url, { headers: { cookie: "nocache=true" } });
data.statusCode = response.statusCode;
2021-05-24 14:27:30 +00:00
data.up = true;
data.ip = response.ip;
} catch (error) {
data.statusCode = error?.response?.statusCode || error.statusCode || error.status;
data.errorMessage = error.message;
data.errorResponseContent = getResponseContent(error.response);
data.ip = error?.response?.ip ?? null;
}
2021-05-24 14:27:30 +00:00
done({ name, time: calculateElapsedTime(time), ...data });
}
2021-05-24 14:27:30 +00:00
const checks = [uploadCheck, websiteCheck, downloadCheck, skylinkSubdomainCheck, handshakeSubdomainCheck];
if (process.env.ACCOUNTS_ENABLED) {
checks.push(accountHealthCheck);
}
module.exports = checks;