From 6a4c2e97c0afafe802759d0f4367d5a42feeb296 Mon Sep 17 00:00:00 2001 From: CodaBool <61724833+CodaBool@users.noreply.github.com> Date: Wed, 22 May 2024 11:54:29 -0500 Subject: [PATCH] feat: add docker healthchecks to server and ml (#9583) * add healthcheck * format, import, IMMICH_PORT, and eslint change * chore: clean up nodejs healthcheck * fix ruff formating * add healthcheck * format, import, IMMICH_PORT, and eslint change * chore: clean up nodejs healthcheck * fix ruff formating * add healthcheck to dockerfile * poetry run ruff check --fix * removed 2 of 3 console calls --------- Co-authored-by: Jason Rasmussen --- machine-learning/Dockerfile | 2 ++ machine-learning/app/healthcheck.py | 14 ++++++++++++++ server/Dockerfile | 2 ++ server/package.json | 1 + server/src/utils/healthcheck.ts | 29 +++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+) create mode 100644 machine-learning/app/healthcheck.py create mode 100644 server/src/utils/healthcheck.ts diff --git a/machine-learning/Dockerfile b/machine-learning/Dockerfile index afd793b033..21bb93af65 100644 --- a/machine-learning/Dockerfile +++ b/machine-learning/Dockerfile @@ -95,3 +95,5 @@ COPY start.sh log_conf.json ./ COPY app . ENTRYPOINT ["tini", "--"] CMD ["./start.sh"] + +HEALTHCHECK CMD python3 healthcheck.py diff --git a/machine-learning/app/healthcheck.py b/machine-learning/app/healthcheck.py new file mode 100644 index 0000000000..8a4ec897f0 --- /dev/null +++ b/machine-learning/app/healthcheck.py @@ -0,0 +1,14 @@ +import os +import sys + +import requests + +port = os.getenv("IMMICH_PORT", 3003) + +try: + response = requests.get(f"http://localhost:{port}/ping", timeout=2) + if response.status_code == 200: + sys.exit(0) + sys.exit(1) +except requests.RequestException: + sys.exit(1) diff --git a/server/Dockerfile b/server/Dockerfile index d9ad1665aa..ab5a730d37 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -62,3 +62,5 @@ VOLUME /usr/src/app/upload EXPOSE 3001 ENTRYPOINT ["tini", "--", "/bin/bash"] CMD ["start.sh"] + +HEALTHCHECK CMD npm run healthcheck diff --git a/server/package.json b/server/package.json index 9bf972a272..6265c6d928 100644 --- a/server/package.json +++ b/server/package.json @@ -18,6 +18,7 @@ "check": "tsc --noEmit", "check:code": "npm run format && npm run lint && npm run check", "check:all": "npm run check:code && npm run test:cov", + "healthcheck": "node ./dist/utils/healthcheck.js", "test": "vitest", "test:watch": "vitest --watch", "test:cov": "vitest --coverage", diff --git a/server/src/utils/healthcheck.ts b/server/src/utils/healthcheck.ts new file mode 100644 index 0000000000..df50636f45 --- /dev/null +++ b/server/src/utils/healthcheck.ts @@ -0,0 +1,29 @@ +#!/usr/bin/env node +const port = Number(process.env.IMMICH_PORT) || 3001; +const controller = new AbortController(); + +const main = async () => { + const timeout = setTimeout(() => controller.abort(), 2000); + try { + const response = await fetch(`http://localhost:${port}/api/server-info/ping`, { + signal: controller.signal, + }); + + if (response.ok) { + const body = await response.json(); + if (body.res === 'pong') { + process.exit(); + } + } + } catch (error) { + if (error instanceof DOMException === false) { + console.error(error); + } + } finally { + clearTimeout(timeout); + } + + process.exit(1); +}; + +void main();