-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from serguun42/add-cloud-healthcheck
Add cloud health check container
- Loading branch information
Showing
9 changed files
with
217 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.vscode | ||
/node_modules/ | ||
|
||
/data/ | ||
/out/ | ||
/local-stuff/ | ||
tsconfig.json | ||
*.bat | ||
*.example.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM node:lts-alpine | ||
|
||
ARG TESTING | ||
|
||
WORKDIR /usr/src/app | ||
COPY . /usr/src/app | ||
|
||
ENV NODE_ENV=production | ||
RUN npm ci --omit=dev | ||
|
||
CMD [ "npm", "run", "production" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const http = require("http"); | ||
const fetch = require("node-fetch").default; | ||
const { AbortError } = require("node-fetch"); | ||
const IS_DEV = require("./util/is-dev.js"); | ||
|
||
const CONFIG = IS_DEV ? require("../../DEV_CONFIGS/healthcheck.config.json") : require("./healthcheck.config.json"); | ||
|
||
const CHECKING_ORIGIN = process.env.CHECKING_ORIGIN || CONFIG.CHECKING_ORIGIN; | ||
const API_VERSION = process.env.API_VERSION || CONFIG.API_VERSION; | ||
|
||
const PONG_API_METHOD = new URL(`/api/v${API_VERSION}/stats`, CHECKING_ORIGIN); | ||
const STATS_API_METHOD = new URL(`/api/v${API_VERSION}/stats`, CHECKING_ORIGIN); | ||
|
||
class CustomError extends Error { | ||
constructor(message) { | ||
super(message); | ||
} | ||
} | ||
|
||
http | ||
.createServer((req, res) => { | ||
if (req.method !== "GET") { | ||
res.statusCode = 405; | ||
res.end(); | ||
return; | ||
} | ||
|
||
if (req.url !== "/" && req.url !== "") { | ||
res.statusCode = 404; | ||
res.end(); | ||
return; | ||
} | ||
|
||
const pingSignaling = new AbortController(); | ||
const pingSignalTimeout = setTimeout(() => pingSignaling.abort(), 5000); | ||
|
||
fetch(PONG_API_METHOD, { signal: pingSignaling.signal }) | ||
.then((res) => { | ||
clearTimeout(pingSignalTimeout); | ||
|
||
if (!res.ok) return Promise.reject(new CustomError("Cannot ping service")); | ||
|
||
const statsSignaling = new AbortController(); | ||
const statsSignalTimeout = setTimeout(() => pingSignaling.abort(), 5000); | ||
|
||
return fetch(STATS_API_METHOD, { signal: statsSignaling.signal }).then( | ||
/** @returns {Promise<{ groupsCount: number; scrapperUpdatedDate: string }>} */ (res) => { | ||
clearTimeout(statsSignalTimeout); | ||
|
||
if (!res.ok) return Promise.reject(new CustomError("Cannot get stats from service")); | ||
|
||
return res.json(); | ||
} | ||
); | ||
}) | ||
.then((stats) => { | ||
res.statusCode = 200; | ||
res.setHeader("Content-Type", "application/json; charset=UTF-8"); | ||
res.end(JSON.stringify({ ok: true, stats })); | ||
}) | ||
.catch((reason) => { | ||
res.statusCode = 500; | ||
res.setHeader("Content-Type", "application/json; charset=UTF-8"); | ||
res.end( | ||
JSON.stringify({ | ||
ok: false, | ||
reason: | ||
reason instanceof CustomError | ||
? reason.message | ||
: reason instanceof AbortError | ||
? "Connection timed out" | ||
: typeof reason === "string" | ||
? reason | ||
: "Server is not OK" | ||
}) | ||
); | ||
}); | ||
}) | ||
.listen(80); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"CHECKING_ORIGIN": "https://mirea.xyz", | ||
"API_VERSION": "1.3" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "healthcheck", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "healthcheck-server.js", | ||
"scripts": { | ||
"production": "node healthcheck-server.js" | ||
}, | ||
"author": "serguun42", | ||
"license": "BSL-1.0", | ||
"dependencies": { | ||
"node-fetch": "^2.7.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const IS_DEV = process.env.NODE_ENV !== "production"; | ||
|
||
module.exports = IS_DEV; |