Custom cleanup in workers before graceful shutdown. #167
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The criterion for graceful shutdown of a worker is that there are no more open server-connections: https://github.com/LearnBoost/cluster/blob/master/lib/worker.js#L162-181
I've got a use case where I perform deferred database operations even after the tcp-connection has been closed. This could potentially lead to problems when a worker is killed on graceful shutdown before these db operations have finished.
Since I don't want just the http-server to shutdown gracefully but also the db operations, I thus need some way to perform cleanup within the worker instance during a graceful worker shutdown.
I've come up with a solution that kinda works, but I'm not really happy with it:
When a worker instance is gracefully closed in Worker.close() a 'worker closing' event is emitted. This event can be listened on on the current worker's master instance where a reference to the actual workers this.server object is present. Increasing server.connections by one gives some time (until timeout) to perform cleanup tasks like gracefully finishing deferred db operations before the worker gets killed.
Here's how you would use it:
I'm thankful for any feedback and hints on that.
Cheers,
Christoph