This repository has been archived by the owner on Jun 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.js
54 lines (43 loc) · 1.73 KB
/
app.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
// app.js is the main entry point into your application, this in turn creates the
// http server. You can create more entry points for tools and tests as long as
// you add the same global services to the serviceLocator.
var cluster = require('cluster')
, cpus = require('os').cpus()
, serviceLocator = require('service-locator').createServiceLocator()
, properties = require('./properties')()
, nodemailer = require('nodemailer')
function log() {
var output = Array.prototype.slice.apply(arguments)
output.unshift((new Date()).toISOString())
console.log.apply(null, output)
}
// All application need: properties, logger and most likely a mailer for sending
// emails. saveFactory is also created here for defining data storage. The default
// bundles need all of these.
serviceLocator
.register('properties', properties)
.register('logger', { info: log, debug: log, warn: log, error: log })
.register('mailer', nodemailer.createTransport('sendmail'))
.register('saveFactory', {})
// Cluster is used in all but the development environment
if ((properties.env !== 'development') && (cluster.isMaster)) {
serviceLocator.logger.info('Forking ' + cpus.length +
' cluster process, one per CPU')
// Create one instance of the app (i.e. one process) per CPU
cpus.map(function () {
cluster.fork()
})
// Report child process death
cluster.on('death', function (worker) {
serviceLocator.logger.error('Worker ' + worker.pid + ' died', worker)
if (worker.signalCode === null) {
cluster.fork()
} else {
serviceLocator.logger.error('Not forking new process because ' +
worker.signalCode + ' was given')
}
})
} else {
// Now we create the web server
require('./server')(serviceLocator)
}