New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fastify-graceful-shutdown

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-graceful-shutdown - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

5

example.js
'use strict'
const fastify = require('../fastify')({
const fastify = require('fastify')({
logger: {

@@ -9,3 +9,4 @@ level: 'info'

fastify.register(require('./')).after(() => {
fastify.register(require('./')).after((err) => {
fastify.logger.error(err)
// Register custom clean up handler

@@ -12,0 +13,0 @@ fastify.gracefulShutdown((code, cb) => {

52

index.js

@@ -5,11 +5,27 @@ 'use strict'

const parallel = require('fastparallel')()
const handlers = []
function fastifyGracefulShutdown(fastify, opts, next) {
function completed(err, code) {
const logger = fastify.logger.child({ plugin: 'fastify-graceful-shutdown' })
const handlers = []
const timeout = opts.timeout || 10000
const signals = ['SIGINT', 'SIGTERM']
for (let i = 0; i < signals.length; i++) {
let signal = signals[i]
if (process.listenerCount(signal) > 0) {
next(
new Error(
`${signal} handler was already registered use fastify.gracefulShutdown`
)
)
return
}
}
function completed(err, signal) {
if (err) {
fastify.logger.error({ err: err, exitCode: code }, 'graceful shutdown')
logger.error({ err: err, signal: signal }, 'process terminated')
process.exit(1)
} else {
fastify.logger.info({ exitCode: code }, 'graceful shutdown')
logger.info({ signal: signal }, 'process terminated')
process.exit(0)

@@ -19,2 +35,12 @@ }

function terminateAfterTimeout(signal, timeout) {
setTimeout(() => {
logger.error(
{ signal: signal, timeout: timeout },
'terminate process after timeout'
)
process.exit(1)
}, timeout).unref()
}
function shutdown(signal) {

@@ -34,16 +60,16 @@ parallel(null, handlers, signal, err => completed(err, signal))

// shutdown fastify
addHandler((code, cb) => {
addHandler((signal, cb) => {
logger.info({ signal: signal }, 'triggering close hook')
fastify.close(cb)
})
// catch ctrl+c event and exit normally
process.on('SIGINT', function() {
shutdown('SIGINT')
// register handlers
signals.forEach(signal => {
process.once(signal, () => {
terminateAfterTimeout(signal, timeout)
logger.info({ signal: signal }, 'received signal')
shutdown(signal)
})
})
// is sent to a process to request its termination
process.on('SIGTERM', function() {
shutdown('SIGTERM')
})
next()

@@ -50,0 +76,0 @@ }

{
"name": "fastify-graceful-shutdown",
"version": "1.0.0",
"version": "1.1.0",
"description": "Gracefully shutdown fastify",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -1,2 +0,2 @@

# fastify-graceful-shutdown
# 🏹 fastify-graceful-shutdown
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](#badge)

@@ -19,5 +19,11 @@ [![NPM version](https://img.shields.io/npm/v/fastify-graceful-shutdown.svg?style=flat)](https://www.npmjs.com/package/fastify-graceful-shutdown)

```js
fastify.gracefulShutdown((exitCode, next) => {
fastify.gracefulShutdown((signal, next) => {
next()
})
```
## Caveats
- Don't register signal handlers otherwise except with this plugin.
- Use fastify `onClose` hook to release resources in your plugin.
- The process will be exited after a certain timeout (Default 10 seconds) to protect against stuck process.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc