forked from Elefrant/elefrant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
54 lines (43 loc) · 1.47 KB
/
api.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
'use strict';
// Module dependencies.
require('colors');
var cluster = require('cluster'),
datefmt = require('dateformat'),
numCPUs = require('os').cpus().length;
// Check if New relic is enable
if (require('./app/lib/config').getConfigValue('newrelic', 'enable')) {
require('newrelic');
}
// Show master info
console.log('[Cluster] Creating Master...');
// Create a master server
cluster.setupMaster({
exec: 'server.js'
});
//Show master info in console
console.log('[Cluster] Master created'.green);
console.log('- Pid: %d'.grey, process.pid);
console.log('- Time: %s'.grey, datefmt(new Date(), 'ddd, dd mmm yyyy hh:MM:ss Z'));
// Fork workers
console.error('[Cluster] Creating Workers...');
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
// Event when listening worker
cluster.on('listening', function (worker, address) {
console.log('[Cluster] A worker is now connected to %s:%d'.green, address.address, address.port);
});
// Event when exit worker
cluster.on('exit', function (worker, code, signal) {
console.log('[Cluster] worker %d died (%s). restarting...'.red, worker.process.pid, signal || code);
// Auto-restart worker
cluster.fork();
});
// Event when disconnect worker
cluster.on('disconnect', function (worker) {
console.log('[Cluster] The worker #%s has disconnected'.yellow, worker.id);
});
// Event when death worker
cluster.on('death', function (worker) {
console.log('[Cluster] The worker #%s is death'.red, worker.id);
});