Skip to content

Broadcast exit event to listener before the actual exit handler is called

License

Notifications You must be signed in to change notification settings

stanleyxu2005/node-exit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node-Exit

npm version

A simple process exit hook to make sure you can run your clean up code before node process exits.

Code Example

The application uses pm2 to manage processes. When user presses CTRL+C to kill application, the signal will be sent to child processes as well. pm2 will try to recover these processes. With this module, we can tell pm2 no longer monit terminated processes.

In app.js

const shutdown = require('node-exit')

shutdown.registerExitHandler((isExpectedExit, error) => {
  if (!isExpectedExit) {
    console.fatal('Unexpected exit', error)
  }
  // Put your clean up code here
})

If you have more than one exit handling, here is an example:

const shutdown = require('node-exit')
const pm2 = require('pm2')

// Start PM2
const detached = false
pm2.connect(detached)

// Hook for stop
shutdown.on('will-exit', (isExpectedExit) => {
  console.debug('Will exit soon...')
  pm2.killDaemon()
})

About

Broadcast exit event to listener before the actual exit handler is called

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published