safe-exit
v1.0.0
Published
Shutdown Node.js applications gracefully.
Downloads
11
Maintainers
Readme
Safe Exit
Safe Exit is a small utility to catch signals and execute tasks to ensure your application finishes properly. Is especially useful when your apps are deployed with Docker.
Installation
npm install safe-exit
Usage
safeExit(exitTask, gracePeriod)
exitTask
: Function, Task intended to stop gracefully the application.gracePeriod
: Number, Time (in milliseconds) given to gracefully exit and waited before force termination.
Example:
import http from 'http'
import safeExit from 'safe-exit'
// Start your app
const port = 3000
const server = http.createServer((request, response) => {
response.end('Hello Node.js Server!')
})
server.listen(port)
// Catch SIGINT, SIGTERM, SIGQUIT, SIGHUP, SIGABRT
safeExit(signal => {
console.log('%s detected, shutting down app', signal)
server.close()
}, 1000 * 5)
safeExit(tasksArray)
tasksArray
: Array, array oftasks
,task
is an object like:
{
// Signal to be caught
signal: 'SIGINT',
// Time (in milliseconds) waited before force exit
gracePeriod: 15 * 1000,
// Task intended to stop gracefully the application.
fn: signal => {
// App please exit
}
}
Example:
import http from 'http'
import safeExit from 'safe-exit'
// Start your app
const port = 3000
const server = http.createServer((request, response) => {
response.end('Hello Node.js Server!')
})
server.listen(port)
// Custom task for every signal
safeExit([
{
signal: 'SIGINT',
gracePeriod: 5 * 1000,
fn: signal => {
console.log('SIGNIT detected, shutting down app')
server.close()
}
},
{
signal: 'SIGTERM',
gracePeriod: 5 * 1000,
fn: signal => {
console.log('SIGTERM detected, shutting down app')
server.close()
}
}
])
LICENSE - "MIT"
Copyright (c) 2018 daniloaburto.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.