{"name":"async-exit-hook","version":"1.0.2","description":"Run some code when the process exits (supports async hooks andpm2 clustering)","license":"MIT","repository":{"type":"git","url":"git+https://github.com/tapppi/async-exit-hook.git"},"author":{"name":"Tapani Moilanen","email":"moilanen.tapani@gmail.com","url":"https://github.com/tapppi"},"contributors":[{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"http://sindresorhus.com"}],"engines":{"node":">=0.12.0"},"scripts":{"test":"nyc ava"},"files":["index.js"],"keywords":["exit","quit","process","hook","graceful","handler","shutdown","sigterm","sigint","sighup","pm2","cluster","child","reload","async","terminate","kill","stop","event"],"devDependencies":{"ava":"0.13.0","coveralls":"^2.11.8","nyc":"^6.0.0"},"ava":{"files":["test/*.js","!tests/cases/*"]},"gitHead":"1eb042e519df9905aa6a71ff1728f6994d0541b9","bugs":{"url":"https://github.com/tapppi/async-exit-hook/issues"},"homepage":"https://github.com/tapppi/async-exit-hook#readme","_id":"async-exit-hook@1.0.2","_shasum":"c3bd72a3d39e446482469f9a90ad3963c783c955","_from":".","_npmVersion":"3.6.0","_nodeVersion":"4.2.2","_npmUser":{"name":"tapppi","email":"moilanen.tapani@gmail.com"},"dist":{"shasum":"c3bd72a3d39e446482469f9a90ad3963c783c955","tarball":"http://registry-npm.ywwl.com/async-exit-hook/download/async-exit-hook-1.0.2.tgz","integrity":"sha512-AGty6PLfNiTamhGgLqCV9ms9WO3nhwsJ7YTGPEP9RHKn2pLF86NQcs9F7ZKPKQa5vJHG0uMcq9g3lul+niXGKw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICtwX2bvnxgZjSjryfxK+c/nwuQwlt6ng2I4SPtkceM2AiBJ6Tvj8kDtoSxKnS1aoLZB6QhPId7QYGRQyS/lAh4WQg=="}],"size":3979,"noattachment":false,"key":"/async-exit-hook/-/async-exit-hook-1.0.2.tgz"},"maintainers":[{"name":"tapppi","email":"moilanen.tapani@gmail.com"}],"_npmOperationalInternal":{"host":"packages-13-west.internal.npmjs.com","tmp":"tmp/async-exit-hook-1.0.2.tgz_1457798524159_0.8370754467323422"},"directories":{},"readme":"# async-exit-hook\n[![Build Status](https://api.travis-ci.org/Tapppi/async-exit-hook.svg)](https://travis-ci.org/Tapppi/async-exit-hook)\n[![Coverage Status](https://coveralls.io/repos/github/Tapppi/async-exit-hook/badge.svg?branch=master)](https://coveralls.io/github/Tapppi/async-exit-hook?branch=master)\n\n> Run some code when the process exits\n\nThe `process.on('exit')` event doesn't catch all the ways a process can exit. This module catches:\n\n* process SIGINT, SIGTERM and SIGHUP, SIGBREAK signals  \n* process beforeExit and exit events  \n* PM2 clustering process shutdown message ([PM2 graceful reload](http://pm2.keymetrics.io/docs/usage/cluster-mode/#graceful-reload))  \n\nUseful for cleaning up. You can also include async handlers, and add custom events to hook and exit on.\n\nForked and pretty much rewritten from [exit-hook](https://npmjs.com/package/exit-hook).\n\n\n## Install\n\n```\n$ npm install --save async-exit-hook\n```\n\n## Usage\n\n### Considerations and warning\n#### On `process.exit()` and asynchronous code\n**If you use asynchronous exit hooks, DO NOT use `process.exit()` to exit.\nThe `exit` event DOES NOT support asynchronous code.**\n>['beforeExit' is not emitted for conditions causing explicit termination, such as process.exit()]\n(https://nodejs.org/api/process.html#process_event_beforeexit)\n\n#### Windows and `process.kill(signal)`\nOn windows `process.kill(signal)` immediately kills the process, and does not fire signal events, \nand as such, cannot be used to gracefully exit. See *Clustering and child processes* for a\nworkaround when killing child processes. I'm planning to support gracefully exiting \nwith async support on windows soon.\n\n### Clustering and child processes\nIf you use custom clustering / child processes, you can gracefully shutdown your child process\nby sending a shutdown message (`childProc.send('shutdown')`).\n\n### Example\n```js\nconst exitHook = require('async-exit-hook');\n\nexitHook(() => {\n    console.log('exiting');\n});\n\n// you can add multiple hooks, even across files\nexitHook(() => {\n    console.log('exiting 2');\n});\n\n// you can add async hooks by accepting a callback\nexitHook(callback => {\n    setTimeout(() => {\n        console.log('exiting 3');\n        callback();\n    }, 1000);\n});\n\n// You can hook uncaught errors with uncaughtExceptionHandler(), consequently adding \n// async support to uncaught errors (normally uncaught errors result in a synchronous exit).\nexitHook.uncaughtExceptionHandler(err => {\n    console.error(err);\n});\n\n// You can hook unhandled rejections with unhandledRejectionHandler()\nexitHook.unhandledRejectionHandler(err => {\n    console.error(err);\n});\n\n// You can add multiple uncaught error handlers\n// Add the second parameter (callback) to indicate async hooks\nexitHook.uncaughtExceptionHandler((err, callback) => {\n    sendErrorToCloudOrWhatever(err) // Returns promise\n        .then(() => { \n             console.log('Sent err to cloud'); \n         });\n        .catch(sendError => {\n             console.error('Error sending to cloud: ', err.stack));\n        })\n        .then(() => callback);\n    });\n});\n\n// Add exit hooks for a signal or custom message:\n\n// Custom signal\n// Arguments are `signal, exitCode` (SIGBREAK is already handled, this is an example)\nexitHook.hookEvent('SIGBREAK', 21);\n\n// process event: `message` with a filter\n// filter gets all arguments passed to *handler*: `process.on(message, *handler*)`\n// Exits on process event `message` with msg `customShutdownMessage` only\nexitHook.hookEvent('message', 0, msg => msg !== 'customShutdownMessage');\n\n// All async hooks will work with uncaught errors when you have specified an uncaughtExceptionHandler\nthrow new Error('awesome');\n\n//=> // Sync uncaughtExcpetion hooks called and retun\n//=> '[Error: awesome]'\n//=> // Sync hooks called and retun\n//=> 'exiting'\n//=> 'exiting 2'\n//=> // Async uncaughtException hooks return\n//=> 'Sent error to cloud'\n//=> // Sync uncaughtException hooks return\n//=> 'exiting 3'\n```\n\n\n## License\n\nMIT © Tapani Moilanen  \nMIT © [Sindre Sorhus](http://sindresorhus.com)\n","publish_time":1457798526377,"_cnpm_publish_time":1457798526377,"dist-tags":{"latest":"2.0.1"}}