var cluster = require('cluster');
var q = require('q');
var amqplib = require('amqplib/callback_api');
var _ = require('lodash');
 
var queueName = 'events';
 
var nbrOfRequestsServed = 0;
 
 
function openMQChannel(options) {
  var deferred = q.defer();
 
  amqplib.connect(options.mqServerAddress, function(err, conn) {
    if (err != null) throw err;
    conn.createChannel(function(err, channel) {
      if (err != null) throw err;
      deferred.resolve(channel);
    });
  });
 
  return deferred.promise;
}
 
function startCluster(options) {
  if (cluster.isMaster) {
  	startWorkers();
  	startServerStatsReport();
  } else {
  	startWorker(options);
  }
}
 
function startWorkers() {
  var cpuCount = require('os').cpus().length;
 
  for (var i = 0; i < cpuCount; i += 1) {
    var worker = cluster.fork();
    worker.on('message', function(nbr) {
    	nbrOfRequestsServed += nbr;
    });
  }
  console.log('microservice ready!');
}
 
function startWorker(options) {
  	openMQChannel(options.mqServerAddress).
    then(_.partial(startWebServer, options));
 
    startWorkerStatsReport();
}
 
function startWebServer(options, messageChannel) {
  console.log('starting web server on port ' + options.serverPort);
  var http = require('http');
  var queueName = options.queueName;
 
  var server = http.createServer(function(req, res) {
    var msg = req.url.substring(1);
    messageChannel.assertQueue(queueName);
    messageChannel.sendToQueue(queueName, new Buffer(msg));
 
    res.writeHead(200);
    res.end();
 
    nbrOfRequestsServed++;
  });
  server.listen(options.serverPort);
}
 
cluster.on('exit', function(worker) {
  console.log('Worker ' + worker.id + ' died :(');
  cluster.fork();
});
 
function startWorkerStatsReport() {
	//Report stats every  minute
	setInterval(reportWorkerStats, 60000);
}
 
function startServerStatsReport() {
	//Output stats to the console every 5 minutes
	setInterval(function() {
		console.log('number of requests served: ' + nbrOfRequestsServed);
	}, 30000)
}
 
function reportWorkerStats() {
	process.send(nbrOfRequestsServed);
	nbrOfRequestsServed = 0;
}

module.exports.startCluster = startCluster;
@@ -123,17 +123,16 @@
 dash');%0A
- 
 %0Avar que
@@ -150,17 +150,16 @@
 vents';%0A
- 
 %0Avar nbr
@@ -180,19 +180,17 @@
 ed = 0;%0A
- %0A 
+%0A
 %0Afunctio
@@ -244,17 +244,16 @@
 efer();%0A
- 
 %0A  amqpl
@@ -471,17 +471,16 @@
 ;%0A  %7D);%0A
- 
 %0A  retur
@@ -497,25 +497,24 @@
 .promise;%0A%7D%0A
- 
 %0Afunction st
@@ -559,25 +559,26 @@
 Master) %7B%0A  
-%09
+  
 startWorkers
@@ -583,17 +583,18 @@
 rs();%0A  
-%09
+  
 startSer
@@ -624,17 +624,18 @@
 lse %7B%0A  
-%09
+  
 startWor
@@ -654,17 +654,149 @@
 ;%0A  %7D%0A%7D%0A
- 
+%0Afunction forkThat() %7B%0A  var worker = cluster.fork();%0A  worker.on('message', function(nbr) %7B%0A    nbrOfRequestsServed += nbr;%0A  %7D);%0A%7D%0A
 %0Afunctio
@@ -860,17 +860,16 @@
 length;%0A
- 
 %0A  for (
@@ -911,116 +911,17 @@
     
-var worker = cluster.fork();%0A    worker.on('message', function(nbr) %7B%0A    %09nbrOfRequestsServed += nbr;%0A    %7D
+forkThat(
 );%0A 
@@ -959,25 +959,24 @@
 ready!');%0A%7D%0A
- 
 %0Afunction st
@@ -998,17 +998,16 @@
 ns) %7B%0A  
-%09
 openMQCh
@@ -1038,18 +1038,16 @@
 dress).%0A
-  
   then(_
@@ -1082,20 +1082,17 @@
 ions));%0A
- 
 %0A
-  
   startW
@@ -1109,25 +1109,24 @@
 Report();%0A%7D%0A
- 
 %0Afunction st
@@ -1300,17 +1300,16 @@
 ueName;%0A
- 
 %0A  var s
@@ -1494,17 +1494,16 @@
 (msg));%0A
- 
 %0A    res
@@ -1534,17 +1534,16 @@
 .end();%0A
- 
 %0A    nbr
@@ -1607,17 +1607,16 @@
 ort);%0A%7D%0A
- 
 %0Acluster
@@ -1703,28 +1703,24 @@
 ;%0A  
-cluster.
 fork
+That
 ();%0A%7D);%0A
  %0Afu
@@ -1715,17 +1715,16 @@
 ();%0A%7D);%0A
- 
 %0Afunctio
@@ -1752,17 +1752,18 @@
 ort() %7B%0A
-%09
+  
 //Report
@@ -1783,17 +1783,18 @@
  minute%0A
-%09
+  
 setInter
@@ -1826,17 +1826,16 @@
 000);%0A%7D%0A
- 
 %0Afunctio
@@ -1863,17 +1863,18 @@
 ort() %7B%0A
-%09
+  
 //Output
@@ -1911,17 +1911,18 @@
 minutes%0A
-%09
+  
 setInter
@@ -1942,10 +1942,12 @@
 ) %7B%0A
-%09%09
+    
 cons
@@ -2008,17 +2008,18 @@
 erved);%0A
-%09
+  
 %7D, 30000
@@ -2022,17 +2022,16 @@
 0000)%0A%7D%0A
- 
 %0Afunctio
@@ -2054,17 +2054,18 @@
 ats() %7B%0A
-%09
+  
 process.
@@ -2095,9 +2095,10 @@
 d);%0A
-%09
+  
 nbrO
@@ -2164,8 +2164,9 @@
 Cluster;
+%0A

FIX - 'respawned' workers now contribute to stats