-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (33 loc) · 854 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
37
38
39
40
41
var fs = require('fs');
var cluster = require('cluster');
var config = require('./config');
var app = require('./app');
var workers = [];
if (cluster.isMaster) {
var nconfBroadcast = function(msg) {
for (var i in workers) {
var worker = workers[i];
worker.send(msg);
}
}
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i++) {
const worker = cluster.fork();
worker.on('message', (msg) => {
if(msg.cmd === 'nconf') {
nconfBroadcast(msg);
}
});
// Add the worker to an array of known workers
workers.push(worker);
}
} else {
// Bind to a port
app.listen(3000);
console.log('Application running!');
process.on('message', (msg) => {
config.set(msg.key, msg.value);
});
}