You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Any recommended way for graceful shutdown of uncaughtException #26
With kue, the recommendation was to do something like this:
vardying=false;functionhandleException(err){console.error('uncaught exception or sigterm',err);if(!dying){queue.shutdown(function(err){if(err){console.error('Kue did not shutdown gracefully',err);}process.exit(1);});dying=true;}}process.on('SIGTERM',handleException);process.on('uncaughtException',handleException);
I suppose kue had some things to do to gracefully clean up and close the redis connection. I didn't see any mention of this in the docs, etc. so just curious if it was necessary at all with bee-queue?
Hm, this is definitely something I haven't thought of/documented. I haven't looked at exactly what queue.shutdown does for kue, but queue.close will gracefully close the Redis connection, so I'd start with that for now. I'll have to think about whether anything else should be done for a graceful shutdown; there could, for example, be cases with jobs where .save() has been called, but they haven't made it into Redis yet, and we might like to wait for that to happen (although maybe the redis driver does that?).
The recommended graceful shutdown behavior as of #64 is:
process.on('uncaughtException',()=>{// Queue#close is idempotent - no need to guard against duplicate calls.queue.close().catch((err)=>console.error('bee-queue failed to shut down gracefully',err)).then(()=>process.exit(1));});
The documentation does not normatively state that the event handler must be synchronous. It makes a recommendation that under "correct" usage it should be synchronous, and warns against one type of "incorrect" usage in the form of resuming normal operation.
I do find the wording's suggestion that synchronous cleanup is singularly correct, and therefore that it would be incorrect to even attempt to perform any asynchronous operations, to be a bit over-prescriptive and to gloss over an important subtlety, but in the strictest sense of program correctness the wording is not wrong because we do lose some guarantees along the way. The truly important takeaway is that normal operation must not be resumed. As long as the uncaughtException handler causes the process to move into a clean-up-and-shut-down phase and we can be sure that the process will not resume normal operation or otherwise continue to run indefinitely, we're otherwise free to do what we like as long as we understand what we're getting ourselves into.
I have significant experience implementing and testing uncaughtException handlers that perform asynchronous operations during the clean-up-and-shut-down phase of the process's lifecycle, and I can assure you that there is no hard requirement that the handler be synchronous. I can also assure you that it is reasonable to perform asynchronous cleanup operations (e.g. sending out any final logs, allowing in-flight requests or queries to finish, closing connections, etc) as long as 1) it is on a best-effort basis where we acknowledge that there are no guarantees that any asynchronous operations performed during this phase finish or work correctly, and 2) the (relatively imminent) shutdown of the process is still guaranteed.
For further reference, I gave a meetup/conference talk "Robust Error Handling in Node.js" last year that touched quite heavily on the clean-up-and-shutdown phase of the process's lifecycle and what uncaughtException handlers should and should not be used for.
Reacted by András Tóth, Doug Ayers and Francesco Virga
That aside, I would recommend against using any sort of "synchronous" hackaround library for almost any purpose; most are artifacts from the days of callbacks when everyone was squirming to get away from them. If you want your code to look synchronous, Node has native async/await.
Activity
LewisJEllis commentedon Dec 8, 2015
Hm, this is definitely something I haven't thought of/documented. I haven't looked at exactly what
queue.shutdowndoes for kue, butqueue.closewill gracefully close the Redis connection, so I'd start with that for now. I'll have to think about whether anything else should be done for a graceful shutdown; there could, for example, be cases with jobs where.save()has been called, but they haven't made it into Redis yet, and we might like to wait for that to happen (although maybe the redis driver does that?).shekyan commentedon Feb 23, 2016
See, if you thought about this case (incomplete jobs), we would definitely use this instead of bull ;)
skeggse commentedon Jul 20, 2017
The recommended graceful shutdown behavior as of #64 is:
skeggse commentedon Aug 9, 2017
Hm we should probably add that snippet to the documentation.
tothandras commentedon Feb 23, 2018
According to the Node.js documentation, the event handler has to be synchronous.
Does this mean that the async function must be wrapped with node-sync or a similar library?
LewisJEllis commentedon Feb 23, 2018
The documentation does not normatively state that the event handler must be synchronous. It makes a recommendation that under "correct" usage it should be synchronous, and warns against one type of "incorrect" usage in the form of resuming normal operation.
I do find the wording's suggestion that synchronous cleanup is singularly correct, and therefore that it would be incorrect to even attempt to perform any asynchronous operations, to be a bit over-prescriptive and to gloss over an important subtlety, but in the strictest sense of program correctness the wording is not wrong because we do lose some guarantees along the way. The truly important takeaway is that normal operation must not be resumed. As long as the
uncaughtExceptionhandler causes the process to move into a clean-up-and-shut-down phase and we can be sure that the process will not resume normal operation or otherwise continue to run indefinitely, we're otherwise free to do what we like as long as we understand what we're getting ourselves into.I have significant experience implementing and testing
uncaughtExceptionhandlers that perform asynchronous operations during the clean-up-and-shut-down phase of the process's lifecycle, and I can assure you that there is no hard requirement that the handler be synchronous. I can also assure you that it is reasonable to perform asynchronous cleanup operations (e.g. sending out any final logs, allowing in-flight requests or queries to finish, closing connections, etc) as long as 1) it is on a best-effort basis where we acknowledge that there are no guarantees that any asynchronous operations performed during this phase finish or work correctly, and 2) the (relatively imminent) shutdown of the process is still guaranteed.For further reference, I gave a meetup/conference talk "Robust Error Handling in Node.js" last year that touched quite heavily on the clean-up-and-shutdown phase of the process's lifecycle and what
uncaughtExceptionhandlers should and should not be used for.LewisJEllis commentedon Feb 23, 2018
That aside, I would recommend against using any sort of "synchronous" hackaround library for almost any purpose; most are artifacts from the days of callbacks when everyone was squirming to get away from them. If you want your code to look synchronous, Node has native async/await.
tothandras commentedon Feb 24, 2018
@LewisJEllis Thank you very much for the detailed explanation! I was always confused about the wording of this part of the Node documentation.