-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
36 lines (34 loc) · 1.17 KB
/
index.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
(() => {
'use strict';
let cluster = require('cluster');
// Condition that checks if we are on the master process,
// before creating child processes.
if (cluster.isMaster) {
// Fork all the workers.
const numCPUs = require('os').cpus().length;
// / (parseInt(process.env.CLUSTER_DIVIDER, 10) || 1);
console.log('Process Master', process.pid);
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
Object.keys(cluster.workers).forEach(id => {
console.log('Running with process ID: ', cluster.workers[id].process.pid);
});
// arguments are worker, code, signal
cluster.on('exit', worker => {
const RESTART_DELAY = parseInt(process.env.RESTART_DELAY, 10) || 30000;
console.log('Process ID: ' + worker.process.pid +
' died, creating new worker in ' +
(RESTART_DELAY / 1000) + ' seconds');
setTimeout(cluster.fork, RESTART_DELAY);
});
} else {
const PORT = process.env.PORT || 5555;
require('./server/app')(process.cwd(), app => {
app.listen(PORT, err => {
console.log(err || 'Server running on ', PORT,
' Process ID: ' + process.pid);
});
});
}
})();