var cluster = require('cluster');
var fs = require('fs');

if (cluster.isMaster) {
  console.log('Starting cluster with pid: ' + process.pid);

  // ensure workers exit cleanly
  process.on('SIGINT', () => {
    console.log('Cluster shutting down..');

    // send kill cmd to all workers
    for (var id in cluster.workers) {
      console.log('Worker (' + id + ') shutting down...');
      process.kill(cluster.workers[id]['process']['pid'], 'SIGINT');
    }

    function waitForWorkerShutdown () {
      if (Object.keys(cluster.workers).length > 0) {
        // continue waiting since worker threads are still open
        setTimeout(waitForWorkerShutdown, 100);
      } else {
        // all worker threads have closed
        // now exit the master process
        process.exit(0);
      }
    }

    waitForWorkerShutdown();
  });

  // count the machine's CPUs
  var cpuCount = require('os').cpus().length;

  // create a worker for each CPU
  for (var i = 0; i < cpuCount; i += 1)
    cluster.fork();

  // listen for dying workers
  cluster.on('exit', function (worker, code, signal) {
    if (worker['process']['exitCode'] === 0) {
      console.log('Worker (' + worker['id'] + ') shutdown complete');
    } else if ((signal != 'SIGINT') && (worker['process']['exitCode'] !== 0) && (worker.exitedAfterDisconnect !== true)) {
      console.log('Cluster restarting...');
      cluster.fork();
    }
  });
} else
  require('./instance');