Skip to content

Commit b2b9546

Browse files
authored
feat: implement graceful shutdown on sigterm (#728)
* feat: implement graceful shutdown on sigterm * chore: address pr comments
1 parent 24d02d0 commit b2b9546

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ COPY --from=build /usr/src/app/node_modules node_modules
1616
COPY --from=build /usr/src/app/dist dist
1717
COPY package.json ./
1818
ENV PG_META_PORT=8080
19-
CMD ["npm", "run", "start"]
19+
# `npm run start` does not forward signals to child process
20+
CMD ["node", "dist/server/server.js"]
2021
EXPOSE 8080
2122
# --start-period defaults to 0s, but can't be set to 0s (to be explicit) by now
2223
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)})"

package-lock.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@fastify/swagger": "^8.2.1",
4141
"@fastify/type-provider-typebox": "^3.5.0",
4242
"@sinclair/typebox": "^0.31.25",
43+
"close-with-grace": "^1.3.0",
4344
"crypto-js": "^4.0.0",
4445
"fastify": "^4.24.3",
4546
"fastify-metrics": "^10.0.0",

src/server/server.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import closeWithGrace from 'close-with-grace'
12
import { pino } from 'pino'
23
import { PostgresMeta } from '../lib/index.js'
34
import { build as buildApp } from './app.js'
@@ -125,7 +126,21 @@ if (EXPORT_DOCS) {
125126
})
126127
)
127128
} else {
128-
app.listen({ port: PG_META_PORT, host: PG_META_HOST }, () => {
129+
const closeListeners = closeWithGrace(async ({ err }) => {
130+
if (err) {
131+
app.log.error(err)
132+
}
133+
await app.close()
134+
})
135+
app.addHook('onClose', async () => {
136+
closeListeners.uninstall()
137+
})
138+
139+
app.listen({ port: PG_META_PORT, host: PG_META_HOST }, (err) => {
140+
if (err) {
141+
app.log.error(err)
142+
process.exit(1)
143+
}
129144
const adminPort = PG_META_PORT + 1
130145
adminApp.listen({ port: adminPort, host: PG_META_HOST })
131146
})

0 commit comments

Comments
 (0)