diff --git a/Dockerfile b/Dockerfile index 33a243e8..8756b7ac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,8 @@ COPY --from=build /usr/src/app/node_modules node_modules COPY --from=build /usr/src/app/dist dist COPY package.json ./ ENV PG_META_PORT=8080 -CMD ["npm", "run", "start"] +# `npm run start` does not forward signals to child process +CMD ["node", "dist/server/server.js"] EXPOSE 8080 # --start-period defaults to 0s, but can't be set to 0s (to be explicit) by now HEALTHCHECK --interval=5s --timeout=5s --retries=3 CMD node -e "fetch('http://localhost:8080/health').then((r) => {if (r.status !== 200) throw new Error(r.status)})" diff --git a/package-lock.json b/package-lock.json index 3474fd7b..fec64565 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@fastify/swagger": "^8.2.1", "@fastify/type-provider-typebox": "^3.5.0", "@sinclair/typebox": "^0.31.25", + "close-with-grace": "^1.3.0", "crypto-js": "^4.0.0", "fastify": "^4.24.3", "fastify-metrics": "^10.0.0", @@ -1587,6 +1588,11 @@ "node": ">=6" } }, + "node_modules/close-with-grace": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/close-with-grace/-/close-with-grace-1.3.0.tgz", + "integrity": "sha512-lvm0rmLIR5bNz4CRKW6YvCfn9Wg5Wb9A8PJ3Bb+hjyikgC1RO1W3J4z9rBXQYw97mAte7dNSQI8BmUsxdlXQyw==" + }, "node_modules/color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", @@ -6926,6 +6932,11 @@ "resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-2.2.0.tgz", "integrity": "sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==" }, + "close-with-grace": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/close-with-grace/-/close-with-grace-1.3.0.tgz", + "integrity": "sha512-lvm0rmLIR5bNz4CRKW6YvCfn9Wg5Wb9A8PJ3Bb+hjyikgC1RO1W3J4z9rBXQYw97mAte7dNSQI8BmUsxdlXQyw==" + }, "color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", diff --git a/package.json b/package.json index cf7af94b..bfb71eb5 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "@fastify/swagger": "^8.2.1", "@fastify/type-provider-typebox": "^3.5.0", "@sinclair/typebox": "^0.31.25", + "close-with-grace": "^1.3.0", "crypto-js": "^4.0.0", "fastify": "^4.24.3", "fastify-metrics": "^10.0.0", diff --git a/src/server/server.ts b/src/server/server.ts index e9c6f5b6..88f2fa50 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -1,3 +1,4 @@ +import closeWithGrace from 'close-with-grace' import { pino } from 'pino' import { PostgresMeta } from '../lib/index.js' import { build as buildApp } from './app.js' @@ -125,7 +126,21 @@ if (EXPORT_DOCS) { }) ) } else { - app.listen({ port: PG_META_PORT, host: PG_META_HOST }, () => { + const closeListeners = closeWithGrace(async ({ err }) => { + if (err) { + app.log.error(err) + } + await app.close() + }) + app.addHook('onClose', async () => { + closeListeners.uninstall() + }) + + app.listen({ port: PG_META_PORT, host: PG_META_HOST }, (err) => { + if (err) { + app.log.error(err) + process.exit(1) + } const adminPort = PG_META_PORT + 1 adminApp.listen({ port: adminPort, host: PG_META_HOST }) })