Skip to content

Commit

Permalink
feat: modernize implementation, support promises
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Jul 18, 2024
1 parent 57d3130 commit f8afba6
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 292 deletions.
18 changes: 8 additions & 10 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

const fastify = require('fastify')({
logger: {
level: 'info',
level: 'debug',
},
})

const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

fastify.register(require('./')).after((err) => {
if (err) {
fastify.log.error(err)
}
// Register custom clean up handler
fastify.gracefulShutdown((signal, cb) => {
fastify.log?.info('Received signal to shutdown: %s', signal)
cb()
fastify.log?.info('Graceful shutdown complete')
fastify.gracefulShutdown(async (signal) => {
fastify.log?.info('received signal to shutdown: %s', signal)
await wait(3000)
fastify.log?.info('graceful shutdown complete')
})
})

Expand All @@ -34,11 +36,7 @@ const schema = {
}

fastify.get('/', schema, async function (req, reply) {
await new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, 5000)
})
await wait(3000)
reply.send({ hello: 'world' })
})

Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type FastifyGracefulShutdownPlugin =
declare module 'fastify' {
interface FastifyInstance {
gracefulShutdown(
handler: (signal: string, next: (err?: Error) => void) => void,
handler: (signal: string) => Promise<void> | void,
): void
}
}
Expand Down
30 changes: 14 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const fp = require('fastify-plugin')
const process = require('process')
const parallel = require('fastparallel')()

let registeredListeners = []

Expand Down Expand Up @@ -38,15 +37,12 @@ function fastifyGracefulShutdown(fastify, opts, next) {
function completed(err, signal) {
if (err) {
logger.error?.({ err: err, signal: signal }, 'process terminated')
// Avoid losing data
logger.flush?.()
handlerEventListener.exit(1)
} else {
logger.debug?.({ signal: signal }, 'process terminated')
// Avoid losing data
logger.flush?.()
handlerEventListener.exit(0)
}

logger.flush?.()
handlerEventListener.exit(err ? 1 : 0)
}

function terminateAfterTimeout(signal, timeout) {
Expand All @@ -59,8 +55,14 @@ function fastifyGracefulShutdown(fastify, opts, next) {
}, timeout).unref()
}

function shutdown(signal) {
parallel(null, handlers, signal, (err) => completed(err, signal))
async function shutdown(signal) {
await Promise.all(
handlers.map((handler) => {
return handler(signal)
}),
)
logger.debug?.({ signal: signal }, 'closing fastify server')
await fastify.close()
}

function addHandler(handler) {
Expand All @@ -72,18 +74,14 @@ function fastifyGracefulShutdown(fastify, opts, next) {

fastify.decorate('gracefulShutdown', addHandler)

// shutdown fastify
addHandler((signal, cb) => {
logger.debug?.({ signal: signal }, 'triggering close hook')
fastify.close(cb)
})

// register handlers
signals.forEach((signal) => {
const listener = () => {
terminateAfterTimeout(signal, timeout)
logger.debug?.({ signal: signal }, 'received signal')
terminateAfterTimeout(signal, timeout)
shutdown(signal)
.then(() => completed(null, signal))
.catch((err) => completed(err, signal))
}
registeredListeners.push({ signal, listener })
handlerEventListener.once(signal, listener)
Expand Down
3 changes: 2 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ app.register(plugin, {
app.register(plugin, { timeout: 1 })
app.register(plugin)

app.gracefulShutdown((signal: string, next: () => void) => {})
app.gracefulShutdown((signal: string) => {})
app.gracefulShutdown(async (signal: string) => {})
Loading

0 comments on commit f8afba6

Please sign in to comment.