forked from linnovate/mean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
60 lines (49 loc) · 1.6 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
'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()
var debug = require('debug')('cluster')
// 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) {
debug(`Production Environment`)
// 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) {
debug(`forking ${i}`)
cluster.fork()
}
// Listen for dying workers
cluster.on('exit', function (worker) {
// Replace the dead worker, we're not sentimental
debug(`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 }, function (app) {
var config = app.getConfig()
var port = config.https && config.https.port ? config.https.port : config.http.port
debug(`MEAN app started on port ${port} (${process.env.NODE_ENV}) with cluster worker id ${workerId}`)
deferred.resolve(app)
})
}
module.exports = deferred.promise