From 9048b5c3b179200416a6080e7747b38a3d5a0a14 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Fri, 5 Apr 2024 13:11:43 +0100 Subject: [PATCH] fix: fix healthcheck --- docker-compose.yml | 2 +- healthcheck.js | 16 ++++++++++------ src/server/index.ts | 3 +++ src/streamer/index.ts | 4 ++++ 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 79f5fd0..4c9d8b6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -81,7 +81,7 @@ services: healthcheck: test: - CMD - - mongo + - mongosh - --eval - "db.adminCommand('ping')" interval: 10s diff --git a/healthcheck.js b/healthcheck.js index d69921f..dde989e 100644 --- a/healthcheck.js +++ b/healthcheck.js @@ -1,22 +1,26 @@ +require("dotenv").config(); const http = require("http"); -const config = require("./config"); +const config = require("./build/config"); const options = { - host: "localhost", - port: config.PORT, + path: "/healthcheck", + method: "GET", + host: "127.0.0.1", + port: config.default.PORT, timeout: 2000, }; - const request = http.request(options, (res) => { - if (res.statusCode == 200) { + if (res.statusCode == 200 || res.statusCode == 404) { process.exit(0); } else { + const reqURL = `${res.req.protocol}://${res.req.host}:${options.port}${res.req.path}`; + console.log(reqURL, res.statusCode); process.exit(1); } }); request.on("error", (err) => { - console.log("ERROR"); + console.log("ERROR", err); process.exit(1); }); diff --git a/src/server/index.ts b/src/server/index.ts index bc7c9ff..75eb88f 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -159,6 +159,9 @@ export default async function start() { stat = {}; }, 1000 * 60 * 60); + apiRouter.get("/healthcheck", async (_, res) => { + res.json({ status: "ok" }); + }); apiRouter.get("/stat", async (_, res) => { if (stat.nbRepositories) { res.json(stat); diff --git a/src/streamer/index.ts b/src/streamer/index.ts index 255aa82..9235d31 100644 --- a/src/streamer/index.ts +++ b/src/streamer/index.ts @@ -16,6 +16,10 @@ app.use(compression()); app.use("/api", router); +app.get("/healthcheck", async (_, res) => { + res.json({ status: "ok" }); +}); + app.all("*", (req, res) => { handleError( new AnonymousError("file_not_found", {