From e823e9f373776872e9758ee3353dc66fad3e2ddd Mon Sep 17 00:00:00 2001 From: Matthew Sevey Date: Fri, 15 Apr 2022 10:15:41 -0400 Subject: [PATCH] Add skylink health check --- packages/health-check/Dockerfile | 2 +- packages/health-check/src/checks/critical.js | 32 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/health-check/Dockerfile b/packages/health-check/Dockerfile index 15ef0086..f21394dc 100644 --- a/packages/health-check/Dockerfile +++ b/packages/health-check/Dockerfile @@ -1,6 +1,6 @@ FROM node:16.14.2-alpine -RUN apk add --no-cache dnsmasq=2.86-r0 +RUN apk add --no-cache dnsmasq=2.86-r1 WORKDIR /usr/app diff --git a/packages/health-check/src/checks/critical.js b/packages/health-check/src/checks/critical.js index 127ebd8a..17fc24a4 100644 --- a/packages/health-check/src/checks/critical.js +++ b/packages/health-check/src/checks/critical.js @@ -45,6 +45,7 @@ async function uploadCheck(done) { form.append("file", payload, { filename: "time.txt", contentType: "text/plain" }); try { + // Upload file const response = await got.post(`https://${process.env.PORTAL_DOMAIN}/skynet/skyfile`, { body: form, headers: { cookie: authCookie }, @@ -53,6 +54,18 @@ async function uploadCheck(done) { data.statusCode = response.statusCode; data.up = true; data.ip = response.ip; + + // Check file health + const responseContent = getResponseContent(response); + const skylink = responseContent.skylink; + try { + await skylinkHealthCheck(skylink, 30, authCookie); + } catch (error) { + data.up = false; + data.statusCode = error.response?.statusCode || error.statusCode || error.status; + data.errorMessage = error.message || error; + data.errorResponseContent = getResponseContent(error.response) || error; + } } catch (error) { data.statusCode = error.response?.statusCode || error.statusCode || error.status; data.errorMessage = error.message; @@ -63,6 +76,25 @@ async function uploadCheck(done) { done({ name: "upload_file", time: calculateElapsedTime(time), ...data }); } +// skylinkHealthCheck checks if the skylink has reached full redundancy +async function skylinkHealthCheck(skylink, numRetries, authCookie) { + try { + const response = await got(`https://${process.env.PORTAL_DOMAIN}/skynet/health/skylink/${skylink}`, { + headers: { cookie: authCookie }, + }); + const healthData = getResponseContent(response); + if (healthData.basesectorredundancy !== 10 && numRetries > 0) { + return skylinkHealthCheck(skylink, numRetries - 1); + } + if (healthData.basesectorredundancy !== 10 && numRetries === 0) { + throw "Skylink did not reach full redundancy"; + } + return response; + } catch (error) { + throw error; + } +} + // websiteCheck checks whether the main website is working async function websiteCheck(done) { return done(await genericAccessCheck("website", `https://${process.env.PORTAL_DOMAIN}`));