index.js
function ClusterManager(opts) {
  if (isFunction(opts)) {
    opts = { worker: opts };
  }
  this.options = opts || {};

  this.givenNumWorkers =
    exists(this.options.numWorkers) ||
    exists(process.env.CLUSTER_WORKERS);

  defaults(this.options, {
    debugScope: process.env.CLUSTER_DEBUG || 'cluster-man',
    master: noop,
    numWorkers: process.env.CLUSTER_WORKERS || os.cpus().length,
    killOnError: true,
    beforeExit: function (err, done) {
      done();
    }
  });

  this._addLogger('info', [this.options.debugScope, 'info'].join(':'));
  this._addLogger('warning', [this.options.debugScope, 'warning'].join(':'));
  this._addLogger('error', [this.options.debugScope, 'error'].join(':'));

  if (!this.options.worker || !isFunction(this.options.worker)) {
    throw new Error('Cluster must be provided with a worker closure.');
  }

  if (!isFunction(this.options.beforeExit)) {
    this.log.warning('Before exit callback is not a function, removing.');
    this.options.beforeExit = noop;
  }

  this.workers = [];

  // This is here to expose the cluster without having to re-require in the
  // script that uses cluster-man
  this.cluster = cluster;
}
function ClusterManager(opts) {
  if (isFunction(opts)) {
    opts = { worker: opts };
  }
  this.options = opts || {};

  this.givenNumWorkers =
    exists(this.options.numWorkers) ||
    exists(process.env.CLUSTER_WORKERS);

  defaults(this.options, {
    debugScope: process.env.CLUSTER_DEBUG || 'cluster-man',
    master: noop,
    numWorkers: process.env.CLUSTER_WORKERS || os.cpus().length,
    killOnError: true,
    beforeExit: function (err, done) {
      done();
    }
  });

  this._addLogger('info', [this.options.debugScope, 'info'].join(':'));
  this._addLogger('warning', [this.options.debugScope, 'warning'].join(':'));
  this._addLogger('error', [this.options.debugScope, 'error'].join(':'));

  if (!this.options.worker || !isFunction(this.options.worker)) {
    throw new Error('Cluster must be provided with a worker closure.');
  }

  if (!isFunction(this.options.beforeExit)) {
    this.log.warning('Before exit callback is not a function, removing.');
    this.options.beforeExit = noop;
  }

  this.workers = [];

  // This is here to expose the cluster without having to re-require in the
  // script that uses cluster-man
  this.cluster = cluster;
}
function ClusterManager ( opts ) { if ( isFunction ( opts ) ) { opts = { worker : opts } ; } this . options = opts || { } ; this . givenNumWorkers = exists ( this . options . numWorkers ) || exists ( process . env . CLUSTER_WORKERS ) ; defaults ( this . options , { debugScope : process . env . CLUSTER_DEBUG || 'cluster-man' , master : noop , numWorkers : process . env . CLUSTER_WORKERS || os . cpus ( ) . length , killOnError : true , beforeExit : function ( err , done ) { done ( ) ; } } ) ; this . _addLogger ( 'info' , [ this . options . debugScope , 'info' ] . join ( ':' ) ) ; this . _addLogger ( 'warning' , [ this . options . debugScope , 'warning' ] . join ( ':' ) ) ; this . _addLogger ( 'error' , [ this . options . debugScope , 'error' ] . join ( ':' ) ) ; if ( ! this . options . worker || ! isFunction ( this . options . worker ) ) { throw new Error ( 'Cluster must be provided with a worker closure.' ) ; } if ( ! isFunction ( this . options . beforeExit ) ) { this . log . warning ( 'Before exit callback is not a function, removing.' ) ; this . options . beforeExit = noop ; } this . workers = [ ] ; // This is here to expose the cluster without having to re-require in the // script that uses cluster-man this . cluster = cluster ; }
Utility class for creating new server clusters.

@example
var ClusterManager = require('cluster-man');
var server = require('./lib/server');

// Basic usage (if you only need to handle workers)
new ClusterManager(server.start).start();

@example
var ClusterManager = require('cluster-man');
var server = require('./lib/server');

// Create a new cluster manager using options, for handling master process
// and worker processes with a specific number of workers.
var serverCluster = new ClusterManager({
worker: server.start,
master: masterStart,
numWorkers: 4
});

function masterStart(clusterManager) {
// Any additional things you'd like to do after the cluster
// has started...
}

// Start the cluster
serverCluster.start();

@author Ryan Sandor Richards.
@class
@param {object|function} opts Options for the cluster or a worker function to
execute on worker processes.
@param {cluster-man~Callback} opt.worker Function to execute on the worker
processes.
@param {cluster-man~Callback} opt.master Function to execute on the master
process.
@param {Number} opt.numWorkers Number of workers to spawn. Defaults to the
value in `process.env.CLUSTER_WORKERS` if present, and if not then the
number of CPUs as reported by `os.cpus().length`.
@param {String} opt.debugScope Root scope for debug logging. Defaults to the
value in `process.env.CLUSTER_DEBUG` if present, and if not then defaults
to 'cluster-man'.
@param {Boolean} opt.killOnError=true Whether or not to kill the master
process on and unhandled error.
@param {cluster-man~BeforeExit} opt.beforeExit Callback to execute before the
master process exits in response to an error.
@throws Error If a opt.worker was not specified or was not a function.
Utility class for creating new server clusters .