-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (28 loc) · 1018 Bytes
/
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
const cluster = require('cluster'),
os = require('os');
var express = require('express'),
winston = require('winston'),
chalk = require('chalk'),
logLevel = process.env.environment === 'development' ? 'debug' : 'info';
winston.level = logLevel;
var worker = require('./worker');
const port = process.env.INFRAL_PORT || 4555;
if ( cluster.isMaster ) {
var workers = [];
os.cpus().forEach((cpu, i) => {
let boot = num => {
workers[num] = cluster.fork();
workers[num].on('exit', () => {
winston.error(chalk.bgRed('Worker died. :( RIP Worker', num, 'on core #' + i + '. Rebooting...'));
boot(num);
});
};
boot(i);
});
} else {
winston.info(chalk.dim('[', cluster.worker.id, '] Starting infral worker...'));
process.title = 'INFRAL Balance Worker - ' + cluster.worker.id + ' - Node.js';
worker.init(express()).listen(port, () => {
winston.info(chalk.dim('[', cluster.worker.id, '] Worker listening on port:', port));
});
}