forked from linnovate/mean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (49 loc) · 1.69 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
/*
var cl = console.log;
console.log = function(){
console.trace();
cl.apply(console,arguments);
};
*/
process.env.NODE_CONFIG_DIR = './config/env';
// Requires meanio .
var mean = require('meanio');
var cluster = require('cluster');
var deferred = require('q').defer();
// Code to run if we're in the master process or if we are not in debug mode/ running tests
if ((cluster.isMaster) &&
(process.execArgv.indexOf('--debug') < 0) &&
(process.env.NODE_ENV!=='test') && (process.env.NODE_ENV!=='development') &&
(process.execArgv.indexOf('--singleProcess')<0)) {
//if (cluster.isMaster) {
console.log('for real!');
// Count the machine's CPUs
var cpuCount = process.env.CPU_COUNT || require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
console.log ('forking ',i);
cluster.fork();
}
// Listen for dying workers
cluster.on('exit', function (worker) {
// Replace the dead worker, we're not sentimental
console.log('Worker ' + worker.id + ' died :(');
cluster.fork();
});
// Code to run if we're in a worker process
} else {
var workerId = 0;
if (!cluster.isMaster)
{
workerId = cluster.worker.id;
}
// Creates and serves mean application
mean.serve({ workerid: workerId /* more options placeholder*/ }, function (app) {
var config = app.getConfig();
var port = config.https && config.https.port ? config.https.port : config.http.port;
console.log('Mean app started on port ' + port + ' (' + process.env.NODE_ENV + ') cluster.worker.id:', workerId);
deferred.resolve(app);
});
}
module.exports = deferred.promise;