Skip to content
This repository

Support Worker Initialization Functions #113

Open
wants to merge 2 commits into from

2 participants

Joshua Rubin TJ Holowaychuk
Joshua Rubin

Some workers may require an initialization sequence before they should
connect to the socket.

In order to utilize this, simply add:
.set('worker init', 'method name')
to your cluster instance.

'method name' must be a method of the server that takes one callback
argument as follows:

function server.worker_init(callback) {

if (error) {
return callback(<error>)
}
// no error
return callback(null);
}

When running the callback, one argument should be supplied. If the
is "truthy", then the worker will consider it to have failed. If
it is "falsy", then the worker will connect to the master.

Signed-off-by: Joshua Rubin joshua@rubixconsulting.com

Joshua Rubin joshuarubin Add support for worker initialization functions
Some workers may require an initialization sequence before they should
connect to the socket.

In order to utilize this, simply add:
  .set('worker init', 'method name')
to your cluster instance.

'method name' must be a method of the server that takes one callback
argument as follows:

function server.worker_init(callback) {
  <initialization code goes here>
  if (error) {
    return callback(<error>)
  }
  // no error
  return callback(null);
}

When running the callback, one argument should be supplied. If the
<error> is "truthy", then the worker will consider it to have failed. If
it is "falsy", then the worker will connect to the master.

Signed-off-by: Joshua Rubin <joshua@rubixconsulting.com>
02db2d3
TJ Holowaychuk

what's the use-case for deferring the connection?

Joshua Rubin

I have some workers that need to fetch data from remote sites and/or do some disk loads before they will return acceptable results. I am using cluster for a very high volume web api and I am primarily concerned that the requests that come in from the time the worker is added, until initialized, will fail.

Joshua Rubin joshuarubin ensure that the callback is run in the scope of the worker instance
Signed-off-by: Joshua Rubin <joshua@rubixconsulting.com>
9644c3b
TJ Holowaychuk

I think I'd rather tackle this from a more abstract point of view, so that plugins could defer listening as well, then the logic you want to defer could just be a plugin, which happens to be specific to your app, but still just a plugin

Joshua Rubin

That would work well for me too. I am happy to help.

TJ Holowaychuk

actually never mind that, the api for exposing a plugin to the workers is pretty lame right now so that's not a great solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Jun 27, 2011
Joshua Rubin joshuarubin Add support for worker initialization functions
Some workers may require an initialization sequence before they should
connect to the socket.

In order to utilize this, simply add:
  .set('worker init', 'method name')
to your cluster instance.

'method name' must be a method of the server that takes one callback
argument as follows:

function server.worker_init(callback) {
  <initialization code goes here>
  if (error) {
    return callback(<error>)
  }
  // no error
  return callback(null);
}

When running the callback, one argument should be supplied. If the
<error> is "truthy", then the worker will consider it to have failed. If
it is "falsy", then the worker will connect to the master.

Signed-off-by: Joshua Rubin <joshua@rubixconsulting.com>
02db2d3
Joshua Rubin joshuarubin ensure that the callback is run in the scope of the worker instance
Signed-off-by: Joshua Rubin <joshua@rubixconsulting.com>
9644c3b
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 21 additions and 0 deletions. Show diff stats Hide diff stats

  1. +21 0 lib/worker.js
21 lib/worker.js
@@ -124,6 +124,8 @@ Worker.prototype.start = function(){
124 124 */
125 125
126 126 Worker.prototype.connect = function(id, options){
  127 + var self = this;
  128 +
127 129 this.options = options;
128 130
129 131 // worker id
@@ -135,6 +137,25 @@ Worker.prototype.connect = function(id, options){
135 137 // title
136 138 process.title = options['worker title'].replace('{n}', id);
137 139
  140 + var init_name = options['worker init'];
  141 +
  142 + var init;
  143 + if (this.server && init_name && this.server[init_name] && (typeof this.server[init_name] == "function")) {
  144 + init = this.server[init_name].bind(this);
  145 + }
  146 +
  147 + if (init) {
  148 + init(function(err) {
  149 + if (err) {
  150 + console.error('error initializing worker '+id+': '+err);
  151 + self.close();
  152 + } else {
  153 + self.master.call('connect');
  154 + }
  155 + });
  156 + return;
  157 + }
  158 +
138 159 // notify master of connection
139 160 this.master.call('connect');
140 161 };

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.