Skip to content

Commit

Permalink
add Matteo's close-with-grace and update app.js
Browse files Browse the repository at this point in the history
  • Loading branch information
tryb3l committed Aug 24, 2024
1 parent d6c0740 commit 85812c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
21 changes: 9 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const path = require('node:path')
const AutoLoad = require('@fastify/autoload')
const closeWithGrace = require('close-with-grace')

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

// Graceful shutdown handler
const shutdown = async () => {
try {
await fastify.close()
fastify.log.info('Server closed successfully')
process.exit(0)
} catch (err) {
fastify.log.error('Error during server close %o', err)
process.exit(1)
// eslint-disable-next-line no-unused-vars
closeWithGrace(async function ({ signal, err, manual }) {
if (err) {
fastify.log.error({ err }, 'server closing with error')
} else {
fastify.log.info(`${signal} received, server closing`)
}
}

process.on('SIGINT', shutdown)
process.on('SIGTERM', shutdown)
await fastify.close()
})
}

module.exports.options = options
13 changes: 10 additions & 3 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 @@ -53,6 +53,7 @@
"@fastify/swagger-ui": "4.1.0",
"@fastify/under-pressure": "^8.5.1",
"@fastify/websocket": "^10.0.1",
"close-with-grace": "^2.1.0",
"csv-parse": "^5.5.6",
"csv-stringify": "^6.5.1",
"fast-json-stringify": "^6.0.0",
Expand Down

0 comments on commit 85812c9

Please sign in to comment.