Node Children

Concurrent tasks computation among nodejs child processes (vertical scaling)

send

method
Child.prototype.send()

Option name Type Description
message Mixed message to send

Send a message to the child_process

Child.prototype.send = function(message){
  if(this.isDead()){return;}
  this._process.send(message);
};

spawn

method
Child.prototype.spawn()

Spawn a child

Child.prototype.spawn = function(fn){
  // Kill the child if it was already launched
  this.kill();
  this.em.emit('spawning:child', this, this.em);
  this._process = fork(this.options.workerPath, this.options.args, this.options.spawn_options);
  this._process.on('message', this.em.emit.bind(this.em, "message"));
  this.em.emit('spawned:child', this, this.em);
  this.pid      = this._process.pid;
  if(fn){_.defer(fn);}
  this._process.on('exit', function(exitCode, signalCode){
    this.em.emit('killed:child', this, this.em, exitCode, signalCode);
  }.bind(this));

  return this;
};

Child.prototype.isDead = function(){
  return !this._process || (this._process && (this._process.killed || !this._process.connected));
};

kill

method
Child.prototype.kill()

Option name Type Description
fnDone Function,Undefined

Kill the child (it will never be re-used again)

Child.prototype.kill = function(fn){
  if(this.isDead()){
    if(fn){_.defer(fn);}
    return this;
  }

  this.em.emit('killing:child', this, this.em);
  this._process.removeAllListeners();
  if(fn){
    // fn(err, val, exitCode, signalCode)
    this._process.on('exit', function(){
      _.defer(fn);
    });
  }
  this._process.kill();
  return this;
};

module.exports = Child;