From 12083465989bb512e006ff8ae99373d2dd57a41a Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 28 Nov 2023 09:41:52 +0000 Subject: [PATCH] ci: [#263] add healthcheck for Index GUI container --- .eslintignore | 1 + Containerfile | 8 ++-- .../container/e2e/sqlite/run-e2e-tests.sh | 4 +- package.json | 4 +- project-words.txt | 2 + share/container/health_check.js | 40 +++++++++++++++++++ 6 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 .eslintignore create mode 100644 share/container/health_check.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..3fcdfade --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +/share/container/health_check.js \ No newline at end of file diff --git a/Containerfile b/Containerfile index b7919635..0e738d40 100644 --- a/Containerfile +++ b/Containerfile @@ -47,6 +47,7 @@ RUN mkdir -p /var/log/torrust/tracker ENV ENV=/etc/profile COPY --chmod=0555 ./share/container/entry_script_sh /usr/local/bin/entry.sh +COPY --chmod=0555 ./share/container/health_check.js /usr/local/bin/health_check.js VOLUME ["/var/log/torrust/index-gui"] @@ -58,7 +59,6 @@ ENTRYPOINT ["/usr/local/bin/entry.sh"] FROM runtime as release ENV RUNTIME="release" COPY --from=test /app/.output /app/.output -#HEALTHCHECK --interval=5s --timeout=5s --start-period=3s --retries=3 \ -# CMD /usr/bin/http_health_check http://localhost:${HEALTH_CHECK_API_PORT}/health_check \ -# || exit 1 -CMD [ "/nodejs/bin/node", "/app/.output/server/index.mjs" ] \ No newline at end of file +HEALTHCHECK --interval=5s --timeout=5s --start-period=3s --retries=3 \ + CMD /nodejs/bin/node /usr/local/bin/health_check.js ${INDEX_GUI_PORT} || exit 1 +CMD [ "/nodejs/bin/node", "/app/.output/server/index.mjs" ] diff --git a/contrib/dev-tools/container/e2e/sqlite/run-e2e-tests.sh b/contrib/dev-tools/container/e2e/sqlite/run-e2e-tests.sh index c8a38593..79c7656d 100755 --- a/contrib/dev-tools/container/e2e/sqlite/run-e2e-tests.sh +++ b/contrib/dev-tools/container/e2e/sqlite/run-e2e-tests.sh @@ -24,9 +24,7 @@ cp .env.local .env || exit 1 ./contrib/dev-tools/container/functions/wait_for_container_to_be_healthy.sh torrust-mysql-1 10 3 || exit 1 ./contrib/dev-tools/container/functions/wait_for_container_to_be_healthy.sh torrust-tracker-1 10 3 || exit 1 ./contrib/dev-tools/container/functions/wait_for_container_to_be_healthy.sh torrust-index-1 10 3 || exit 1 - -# Wait for the Index GUI to be ready -sleep 10 +./contrib/dev-tools/container/functions/wait_for_container_to_be_healthy.sh torrust-index-gui-1 10 3 || exit 1 # Just to make sure that everything is up and running docker ps diff --git a/package.json b/package.json index 5138b98a..9bb680f9 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "generate": "nuxt generate", "preview": "nuxt preview", "postinstall": "nuxt prepare", - "lint": "eslint --ext \".ts,.js,.vue\" --ignore-path .gitignore .", - "lintfix": "eslint --fix --ext \".ts,.js,.vue\" --ignore-path .gitignore .", + "lint": "eslint --ext \".ts,.js,.vue\" .", + "lintfix": "eslint --fix --ext \".ts,.js,.vue\" .", "cypress:open": "cypress open", "cypress:run": "cypress run" }, diff --git a/project-words.txt b/project-words.txt index 56888217..0127079a 100644 --- a/project-words.txt +++ b/project-words.txt @@ -1,9 +1,11 @@ composables +Containerfile daisyui dinamically dompurify filedrag filelist +HEALTHCHECK heroicons infohash lintfix diff --git a/share/container/health_check.js b/share/container/health_check.js new file mode 100644 index 00000000..2b17e3c2 --- /dev/null +++ b/share/container/health_check.js @@ -0,0 +1,40 @@ +/* + +This application is used to create a `HEALTHCHECK` instruction in the +`Dockerfile` or `Containerfile`. + +Usage: + +node health_check.js 3000 +node health_check.js PORT + +*/ + +const http = require("http"); + +// Retrieve the port number from the command-line arguments. +// Default to 3000 if no argument is provided +const port = process.argv[2] || "3000"; + +const options = { + host: "localhost", + port, + timeout: 2000 +}; + +const request = http.request(options, (res) => { + console.log(`STATUS: ${res.statusCode}`); + if (res.statusCode === 200) { + process.exit(0); + } + else { + process.exit(1); + } +}); + +request.on("error", function (err) { + console.log("ERROR"); + process.exit(1); +}); + +request.end();