Skip to content

Commit 85812c9

Browse files
committed
add Matteo's close-with-grace and update app.js
1 parent d6c0740 commit 85812c9

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

app.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const path = require('node:path')
44
const AutoLoad = require('@fastify/autoload')
5+
const closeWithGrace = require('close-with-grace')
56

67
// Pass --options via CLI arguments in command to enable these options.
78
const options = require('./configs/server-options.js')
@@ -43,19 +44,15 @@ module.exports = async function (fastify, opts) {
4344
})
4445

4546
// Graceful shutdown handler
46-
const shutdown = async () => {
47-
try {
48-
await fastify.close()
49-
fastify.log.info('Server closed successfully')
50-
process.exit(0)
51-
} catch (err) {
52-
fastify.log.error('Error during server close %o', err)
53-
process.exit(1)
47+
// eslint-disable-next-line no-unused-vars
48+
closeWithGrace(async function ({ signal, err, manual }) {
49+
if (err) {
50+
fastify.log.error({ err }, 'server closing with error')
51+
} else {
52+
fastify.log.info(`${signal} received, server closing`)
5453
}
55-
}
56-
57-
process.on('SIGINT', shutdown)
58-
process.on('SIGTERM', shutdown)
54+
await fastify.close()
55+
})
5956
}
6057

6158
module.exports.options = options

package-lock.json

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@fastify/swagger-ui": "4.1.0",
5454
"@fastify/under-pressure": "^8.5.1",
5555
"@fastify/websocket": "^10.0.1",
56+
"close-with-grace": "^2.1.0",
5657
"csv-parse": "^5.5.6",
5758
"csv-stringify": "^6.5.1",
5859
"fast-json-stringify": "^6.0.0",

0 commit comments

Comments
 (0)