src/server/master.js
function Master(options) {
  /*jshint maxcomplexity: 12 */

  options = options || {};

  this._workers = {};
  this.shuttingDown = false;

  this.spinTimeout = options.spinTimeout || 10 * 1000;
  util.verifyType('number', this, 'spinTimeout');

  this.delayStart = options.delayStart || 60 * 1000;
  util.verifyType('number', this, 'delayStart');

  this.pollInterval = options.pollInterval || 500;
  util.verifyType('number', this, 'pollInterval');

  this.killTimeout = options.killTimeout || 7000;
  util.verifyType('number', this, 'killTimeout');

  this.numberWorkers = options.numberWorkers ||
    parseInt(process.env.THEHELP_NUMBER_WORKERS, 10) || 1;
  util.verifyType('number', this, 'numberWorkers');

  this.setGraceful(options.graceful || Graceful.instance);

  this.log = options.log || logShim('thehelp-cluster:master');
  util.verifyLog(options.log);

  this._cluster = options._cluster || cluster;
  this._cluster.on('disconnect', this._restartWorker.bind(this));

  if (Master.instance) {
    this.log.warn('More than one Master instance created in this process. ' +
      'You\'ll have more worker processes than you signed up for!');
  }
  Master.instance = this;
}
function Master(options) {
  /*jshint maxcomplexity: 12 */

  options = options || {};

  this._workers = {};
  this.shuttingDown = false;

  this.spinTimeout = options.spinTimeout || 10 * 1000;
  util.verifyType('number', this, 'spinTimeout');

  this.delayStart = options.delayStart || 60 * 1000;
  util.verifyType('number', this, 'delayStart');

  this.pollInterval = options.pollInterval || 500;
  util.verifyType('number', this, 'pollInterval');

  this.killTimeout = options.killTimeout || 7000;
  util.verifyType('number', this, 'killTimeout');

  this.numberWorkers = options.numberWorkers ||
    parseInt(process.env.THEHELP_NUMBER_WORKERS, 10) || 1;
  util.verifyType('number', this, 'numberWorkers');

  this.setGraceful(options.graceful || Graceful.instance);

  this.log = options.log || logShim('thehelp-cluster:master');
  util.verifyLog(options.log);

  this._cluster = options._cluster || cluster;
  this._cluster.on('disconnect', this._restartWorker.bind(this));

  if (Master.instance) {
    this.log.warn('More than one Master instance created in this process. ' +
      'You\'ll have more worker processes than you signed up for!');
  }
  Master.instance = this;
}
/*
The `constructor` has no required parameters. Optional parameters:

+ `graceful` - required to enable full process shutdown scenarios. Can be set either on
construction or later with `setGraceful()`. Default is `Graceful.instance`, so if you've
created an instance in this process already, it will be found automatically.
+ `spinTimeout` - how long a process needs to stay up after launch for a crash to be
considered 'normal.'
+ `delayStart` - if a process crashes too fast, how long to wait before starting another
+ `pollInterval` - when gracefully shutting down, how frequently to check for zero
remaining worker processes
+ `killTimeout` - how long to wait after sending 'SIGTERM` to follow up it with a 'SIGINT'
for a more decisive kill
+ `numberWorkers` - worker processes to start and maintain. defaults to `1` or
`process.env.THEHELP_NUMBER_WORDERS`
+ `log` - a logger object: `info`, `warn` and `error` keys with the signature
`function(string)`. By default, whatever `thehelp-log-shim` gives us.