igloo.js
1.8 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
71
72
73
74
// # Igloo
var path = require('path')
var IoC = require('electrolyte')
var os = require('os')
var cluster = require('cluster')
IoC.loader(IoC.node(path.join(__dirname, 'lib')))
//
// Inspired by `bixby-express` and Jared Hanson
//
var logger = IoC.create('logger')
var settings = IoC.create('settings')
var app = IoC.create('app')
exports = module.exports = app
if (cluster.isMaster && settings.server.clusterEnabled) {
var clusterSize = settings.server.clusterSize || os.cpus().length
logger.info('creating cluster with %d workers', clusterSize)
for (var i=0; i<clusterSize; 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 {
app.boot(function(err) {
if (err) {
logger.error(err)
process.exit(0)
return
}
logger.info('app booted')
app.listen(settings.server.port, settings.server.host, function() {
var addr = this.address()
logger.info('app listening on %s:%d', addr.address, addr.port)
})
})
}