diff --git a/Dockerfile b/Dockerfile index 7ab5a07..ab8aa1b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,11 @@ FROM node:lts-slim +# Install wget for healthcheck +RUN apt-get update && \ + apt-get install -y wget && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* + WORKDIR /app COPY app/ ./ @@ -21,6 +27,6 @@ ENV NODE_ENV=production # Type checking is done in the repo before building the image. RUN npx tsc --noCheck -HEALTHCHECK --interval=30s --start-period=10s --timeout=5s CMD node /app/healthcheck.js || exit 1 +HEALTHCHECK --interval=30s --start-period=10s --timeout=5s CMD wget -q http://localhost:3000/healthcheck || exit 1 CMD ["pm2-runtime", "dist/index.js" ] diff --git a/app/healthcheck.js b/app/healthcheck.js deleted file mode 100644 index 3629b96..0000000 --- a/app/healthcheck.js +++ /dev/null @@ -1,9 +0,0 @@ -(async () => { - try { - const res = await fetch('http://localhost:3000/healthcheck') - if (await res.text() === 'ok') { - process.exit(0) - } - } catch (e) { } - process.exit(1) -})() diff --git a/app/src/immich.ts b/app/src/immich.ts index cb7972c..9c523d6 100644 --- a/app/src/immich.ts +++ b/app/src/immich.ts @@ -11,17 +11,21 @@ class Immich { * the possible attack surface of this app. */ async request (endpoint: string) { - const res = await fetch(process.env.IMMICH_URL + '/api' + endpoint) - if (res.status === 200) { - const contentType = res.headers.get('Content-Type') || '' - if (contentType.includes('application/json')) { - return res.json() + try { + const res = await fetch(process.env.IMMICH_URL + '/api' + endpoint) + if (res.status === 200) { + const contentType = res.headers.get('Content-Type') || '' + if (contentType.includes('application/json')) { + return res.json() + } else { + return res + } } else { - return res + log('Immich API status ' + res.status) + console.log(await res.text()) } - } else { - log('Immich API status ' + res.status) - console.log(await res.text()) + } catch (e) { + log('Unable to reach Immich on ' + process.env.IMMICH_URL) } } @@ -229,6 +233,10 @@ class Immich { expires: dayjs().add(1, 'hour').format() })) } + + async accessible () { + return !!(await immich.request('/server/ping')) + } } const immich = new Immich() diff --git a/app/src/index.ts b/app/src/index.ts index 63aab74..cf4c60c 100644 --- a/app/src/index.ts +++ b/app/src/index.ts @@ -69,8 +69,12 @@ app.get('/:type(photo|video)/:key/:id', async (req, res) => { }) // Healthcheck -app.get('/healthcheck', (_req, res) => { - res.send('ok') +app.get('/healthcheck', async (_req, res) => { + if (await immich.accessible()) { + res.send('ok') + } else { + res.status(502).send() + } }) // Send a 404 for all other routes diff --git a/docker-compose.yml b/docker-compose.yml index 4bb35f0..dc3f6ac 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,3 +6,7 @@ services: ports: - ${PORT}:3000 env_file: .env + healthcheck: + test: wget -q http://localhost:3000/healthcheck || exit 1 + start_period: 10s + timeout: 5s