Node Children

Concurrent tasks computation among nodejs child processes (vertical scaling)

Children

function
Children()

Option name Type Description
workerPath String File path to the module to fork
options Object,Undefined Option object

Manage child processes

function Children(workerPath, options){
  if(!(this instanceof Children)){
    return new Children(workerPath, options);
  }

  EventEmitter2.call(this, {wildcard: false});

  this.options            = _.defaults(options || {}, Children.defaults);
  this.options.workerPath = workerPath;

  if(!this.options.resetEnv){
    this.options.spawn_options.env = process.env;
  }

  // Children array
  this.childs        = [];
  this._shuttingDown = false;

  if(this.options.autoRestart){
    this.on('killed:child', this._autorespawn.bind(this));
  }
}

util.inherits(Children, EventEmitter2);

Children.prototype._autorespawn = function(child){
  if(this._shuttingDown){return;}
  _.defer(function(){
    this.emit('respawning:child', child, this);
    child.spawn(function(){
      this.emit('respawned:child', child, this);
    }.bind(this));
  }.bind(this));
};

start

method
Children.prototype.start()

Option name Type Description
fn Function(Children),Null Callback when done

Init children, emit "ready" when done

Children.prototype.start = function(fn){
  function initChild(id, fn){
    var child = new Child(this, this.options);
    child.spawn(fn.bind(null, null, child));
  }

  function done(err, childs){
    this.childs = childs;
    this.emit('ready', this);
    if(fn){_.defer(fn.bind(null, this));}
  }

  async.times(this.options.childs,
    initChild.bind(this),
    done.bind(this)
  );
  return this;
};

shutdown

method
Children.prototype.shutdown() ->Children

Option name Type Description
fn Function(Children),Undefined Callback when done

Shutdown all workers

Children.prototype.shutdown = function(fn){
  this._shuttingDown = true;

  var iter   = function(child, done){
    child.kill(done);
  };
  var onDone = function(){
    this.childs = [];
    this.emit("shutdown", this);
    if(fn){_.defer(fn.bind(null, this));}
  };

  async.each(this.childs, iter, onDone.bind(this));
  return this;
};


Children.prototype.send = function(obj){
  this.invoke("send", obj);
};

debug

method
Children.prototype.debug()

Option name Type Description
fn Function

Useful helper for debug

Children.prototype.debug = function(fn){
  function debugFn(ev, child){
    console.log(ev, child.pid);
  }

  ['read', 'shutdown','killing:child','killed:child','spawning:child', 'spawned:child'].forEach(function(ev){
    this.on(ev, (fn || debugFn).bind(null, ev));
  }.bind(this));

  return this;
};

defaults

property
Children.defaults

Children default options

Children.defaults = {

Number of childs to fork

childs: require('os').cpus().length,

Reset the environment when forking child_processes

resetEnv: false,

Auto-restart dead process

autoRestart:true,

Spawn option

spawn_options:{
    args:[],
    env:{}
  }
};