server.js
2.0 KB
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
64
65
66
67
68
69
70
// # boot - server
// Inspired by `bixby-server` by Jared Hanson
var https = require('https')
var http = require('http')
var cluster = require('cluster')
var os = require('os')
var path = require('path')
exports = module.exports = function(logger, settings) {
return function(done) {
if (cluster.isMaster && settings.server.cluster) {
var size = settings.server.cluster.size || os.cpus().length
logger.info('creating cluster with %d workers', size)
for (var i=0; i<size; i++) {
logger.info('spawning worker #%d', i + 1)
cluster.fork()
}
cluster.on('fork', function(worker) {
logger.info('worker #%s with pid %d spawned', worker.id, worker.process.pid)
})
cluster.on('online', function(worker) {
logger.info('worker #%s with pid %d online', worker.id, worker.process.pid)
})
cluster.on('listening', function(worker, addr) {
logger.info('worker #%s with pid %d listening on %s:%d', worker.id, worker.process.pid, addr.address, addr.port)
})
cluster.on('disconnect', function(worker) {
logger.info('worker #%s with pid %d disconnected', worker.id, worker.process.pid)
})
cluster.on('exit', function(worker, code, signal) {
logger.error('worker #%s with pid %d exited with code/signal', worker.id, worker.process.pid, signal, code)
if (worker.suicide) return
logger.info('worker #%s restarting', worker.id)
cluster.fork()
})
} else {
if (settings.server.ssl.enabled)
this.server = https.createServer(settings.server.ssl.options, this)
else
this.server = http.createServer(this)
this.server.listen(settings.server.port, settings.server.host, function() {
var addr = this.address()
logger.info('app listening on %s:%d', addr.address, addr.port)
done()
})
}
}
}
exports['@singleton'] = true
exports['@require'] = [ 'igloo/logger', 'igloo/settings' ]