#!/usr/bin/env node
/**
 * A very basic cluster-based server runner. Restarts failed workers, but does
 * not much else right now.
 */

var cluster = require('cluster');

if (cluster.isMaster) {
	// Start a few more workers than there are cpus visible to the OS, so that we
	// get some degree of parallelism even on single-core systems. A single
	// long-running request would otherwise hold up all concurrent short requests.
	var numCPUs = require('os').cpus().length + 3;
	// Fork workers.
	for (var i = 0; i < numCPUs; i++) {
		cluster.fork();
	}

	cluster.on('exit', function(worker) {
		if (!worker.suicide) {
			var exitCode = worker.process.exitCode;
			console.log('worker', worker.process.pid,
									'died ('+exitCode+'), restarting.');
			cluster.fork();
		}
	});

	process.on('SIGTERM', function() {
		console.log('master shutting down, killing workers');
		var workers = cluster.workers;
		Object.keys(workers).forEach(function(id) {
				console.log('Killing worker ' + id);
				workers[id].destroy();
		});
		console.log('Done killing workers, bye');
		process.exit(1);
	} );
} else {
	var mathoidWorker = require('./mathoid-worker.js');
	process.on('SIGTERM', function() {
		console.log('Worker shutting down');
		process.exit(1);
	});
	// when running on appfog.com the listen port for the app
	// is passed in an environment variable.  Most users can ignore this!
	mathoidWorker.listen(process.env.VCAP_APP_PORT || 8010);
}

@@ -16,14 +16,12 @@
 node
-%0D
 %0A/**
-%0D
 %0A * 
@@ -95,17 +95,16 @@
 but does
-%0D
 %0A * not 
@@ -127,16 +127,13 @@
 now.
-%0D
 %0A */
-%0D%0A%0D
+%0A
 %0Avar
@@ -162,19 +162,17 @@
 uster');
-%0D%0A%0D
+%0A
 %0Aif (clu
@@ -187,17 +187,16 @@
 aster) %7B
-%0D
 %0A%09// Sta
@@ -266,17 +266,16 @@
  that we
-%0D
 %0A%09// get
@@ -339,17 +339,16 @@
 A single
-%0D
 %0A%09// lon
@@ -419,17 +419,16 @@
 equests.
-%0D
 %0A%09var nu
@@ -467,17 +467,16 @@
 gth + 3;
-%0D
 %0A%09// For
@@ -485,17 +485,16 @@
 workers.
-%0D
 %0A%09for (v
@@ -522,17 +522,16 @@
 ; i++) %7B
-%0D
 %0A%09%09clust
@@ -544,15 +544,12 @@
 k();
-%0D
 %0A%09%7D
-%0D%0A%0D
+%0A
 %0A%09cl
@@ -583,17 +583,16 @@
 orker) %7B
-%0D
 %0A%09%09if (!
@@ -608,17 +608,16 @@
 icide) %7B
-%0D
 %0A%09%09%09var 
@@ -651,17 +651,16 @@
 xitCode;
-%0D
 %0A%09%09%09cons
@@ -696,17 +696,16 @@
 ess.pid,
-%0D
 %0A%09%09%09%09%09%09%09
@@ -742,17 +742,16 @@
 ting.');
-%0D
 %0A%09%09%09clus
@@ -765,22 +765,18 @@
 k();
-%0D
 %0A%09%09%7D
-%0D
 %0A%09%7D);
-%0D%0A%0D
+%0A
 %0A%09pr
@@ -799,33 +799,32 @@
 M', function() %7B
-%0D
 %0A%09%09console.log('
@@ -863,17 +863,16 @@
 rkers');
-%0D
 %0A%09%09var w
@@ -896,17 +896,16 @@
 workers;
-%0D
 %0A%09%09Objec
@@ -942,17 +942,16 @@
 on(id) %7B
-%0D
 %0A%09%09%09%09con
@@ -983,17 +983,16 @@
 ' + id);
-%0D
 %0A%09%09%09%09wor
@@ -1014,16 +1014,14 @@
 y();
-%0D
 %0A%09%09%7D);
-%0D
 %0A%09%09c
@@ -1060,17 +1060,16 @@
 , bye');
-%0D
 %0A%09%09proce
@@ -1083,16 +1083,14 @@
 (1);
-%0D
 %0A%09%7D );
-%0D
 %0A%7D e
@@ -1094,17 +1094,16 @@
 %7D else %7B
-%0D
 %0A%09var ma
@@ -1147,17 +1147,16 @@
 er.js');
-%0D
 %0A%09proces
@@ -1183,17 +1183,16 @@
 tion() %7B
-%0D
 %0A%09%09conso
@@ -1222,17 +1222,16 @@
  down');
-%0D
 %0A%09%09proce
@@ -1245,15 +1245,13 @@
 (1);
-%0D
 %0A%09%7D);
-%0D
 %0A%09//
@@ -1305,17 +1305,16 @@
  the app
-%0D
 %0A%09// is 
@@ -1376,17 +1376,16 @@
 re this!
-%0D
 %0A%09mathoi
@@ -1415,16 +1415,15 @@
 env.
-VCAP_APP
+MATHOID
 _POR
@@ -1431,15 +1431,14 @@
  %7C%7C 
-80
 10
+042
 );
-%0D
 %0A%7D
-%0D
 %0A

Change default port