tapppi / async-exit-hook Goto Github PK
View Code? Open in Web Editor NEWThis project forked from sindresorhus/exit-hook
Run some code when the process exits
License: MIT License
This project forked from sindresorhus/exit-hook
Run some code when the process exits
License: MIT License
run nodejs app as:
pm2 start app.js
it run successfully. Then I tried to stop app:
pm2 stop id
But it can not trigger exitHook()
Hello,
it looks like you can bind to both. Is there any particoular reason?
Also, would you expose the exit function so that one can eventually calll it manually?
Let me illustrate the problem with the following code:
const cluster = require("cluster");
const exitHook = require("async-exit-hook");
function addHook(delay) {
exitHook(cb => {
console.log("Start " + delay);
setTimeout(() => {
console.log("Finish " + delay);
cb()
}, delay);
});
}
if (cluster.isMaster) {
cluster.fork();
} else {
addHook(0);
addHook(1);
addHook(10);
addHook(100);
}
If I start this script and kill it with ctrl+c, then I get:
$ node exitHook.js
^C
$ Start 0
Start 1
Start 10
Start 100
Finish 0
Finish 1
As you can see, only the very fast exit hooks are executed. This problem only happens in workers. Is this a known issue?
Versions:
The runHook
method has no error handling:
if (err) {
// Pass error, calling uncaught exception handlers
return hook(err, stepTowardExit);
}
return hook(stepTowardExit);
If a hook throws, then the exit procedure stops and exit is never actually achieved. (I found this by hitting #9 which threw in an exit routine.)
This could be fixed by wrapping in a try/catch
and calling stepTowardsExit
on error.
This would be useful to control the order of exit hooks execution where required. Use case: if I am using it for shutting down dependant processes gracefully, I am probably expecting to have logging being shut down as last.
Please, could you provide an example of how use NodeMailer to send an email on Node exit? I can't figure it out how to integrate callback with nodemailer's promise. Thanks.
callback is always called
https://github.com/Tapppi/async-exit-hook/blame/80e692c88e62a88cf5750460ff02c28298e5b09b/readme.md#L60
callback is not always provided
Line 39 in 80e692c
should be
exitHook(callback => {
setTimeout(() => {
console.log('exiting 3');
if(callback) callback();
}, 1000);
});
When calling exit hook, like so:
exitHook(cb => { /* do something on exit */ })
cb
may not be a function depending on how the async-exit-hook
routine is entered. If it's entered with an uncaught error, then it's called with 2 params and the second is the callback.
if (exit && hook.length > syncArgCount) {
// Hook is async, expects a finish callback
waitingFor++;
if (err) {
// Pass error, calling uncaught exception handlers
return hook(err, stepTowardExit); // call called with 2 params
}
return hook(stepTowardExit); // <-- called with 1 param
}
This would be easy to fix by simply changing the second to:
return hook(null, stepTowardExit);
Because this is a breaking change to the API, it probably warrants a major version upgrade.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.