Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom cleanup in workers before graceful shutdown. #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ctavan
Copy link

@ctavan ctavan commented Nov 1, 2011

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:

var cluster = require('cluster');

var proc = cluster('app.js').listen(3000);

if (proc.isWorker) {
  proc.on('worker closing', function() {
    console.log('closing worker now');

    var server = this.server;

    // Increase connection count, so cluster waits before shutting
    // down this worker
    server.connections++;

    // Perform cleanup
    cleanupStuff(function() {
      server.connections--;
    });
  });
}

I'm thankful for any feedback and hints on that.

Cheers,
Christoph

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.
smtlaissezfaire added a commit to smtlaissezfaire/cluster that referenced this pull request Nov 4, 2011
* delay execution of process.exit with a callback when listening on 'worker close'

Closes LearnBoost#167
smtlaissezfaire added a commit to smtlaissezfaire/cluster that referenced this pull request Nov 4, 2011
* delay execution of process.exit with a callback when listening on 'worker close'

Closes LearnBoost#167
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant