Skip to content

Commit

Permalink
feat: implement graceful shutdown on sigterm (#728)
Browse files Browse the repository at this point in the history
* feat: implement graceful shutdown on sigterm

* chore: address pr comments
  • Loading branch information
sweatybridge authored Feb 21, 2024
1 parent 24d02d0 commit b2b9546
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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)})"
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 16 additions & 1 deletion src/server/server.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 })
})
Expand Down

0 comments on commit b2b9546

Please sign in to comment.