From 4e68d616e57e4cb330b12201d2a05f51b37e44c8 Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Wed, 21 Feb 2024 18:04:27 +0800 Subject: [PATCH 1/2] feat: implement graceful shutdown on sigterm --- Dockerfile | 2 +- package-lock.json | 11 +++++++++++ package.json | 1 + src/server/server.ts | 17 ++++++++++++++++- 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 33a243e8..5ef722b7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,7 @@ 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"] +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..7834bc20 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 ({ signal, err, manual }) => { + if (err) { + app.log.error(err) + } + await app.close() + }) + app.addHook('onClose', async (instance) => { + 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 }) }) From cdb97d42cf54de5cc9bb54a65768b805960b2b11 Mon Sep 17 00:00:00 2001 From: Qiao Han Date: Wed, 21 Feb 2024 18:41:43 +0800 Subject: [PATCH 2/2] chore: address pr comments --- Dockerfile | 1 + src/server/server.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5ef722b7..8756b7ac 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,7 @@ 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 +# `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 diff --git a/src/server/server.ts b/src/server/server.ts index 7834bc20..88f2fa50 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -126,13 +126,13 @@ if (EXPORT_DOCS) { }) ) } else { - const closeListeners = closeWithGrace(async ({ signal, err, manual }) => { + const closeListeners = closeWithGrace(async ({ err }) => { if (err) { app.log.error(err) } await app.close() }) - app.addHook('onClose', async (instance) => { + app.addHook('onClose', async () => { closeListeners.uninstall() })