This repository was archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (40 loc) · 1.32 KB
/
index.js
File metadata and controls
46 lines (40 loc) · 1.32 KB
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
//Include the cluster module
var cluster = require('cluster');
var config = require('./config');
// Code to run if we're in the master process
if (cluster.isMaster) {
var MiniOps = require('miniops');
var restify = require('restify');
var miniOps = new MiniOps(24);
var stats = restify.createServer();
stats.use(restify.jsonp());
stats.get('/ops', miniOps.dataHub());
stats.listen(9133);
// Count the machine's CPU cores
var cpuCount = require('os').cpus().length - 1;
if (cpuCount < 2) {
cpuCount = 2;
}
// Create a worker for each cpu core - 1
for (var i = 0, il=cpuCount; i < il; i++) {
var worker = cluster.fork();
worker.on('message', function (msg) {
miniOps.recorder()(msg.req, msg.res, msg.route, msg.error);
});
}
// Listen for dying workers
cluster.on('exit', function (worker) {
// Replace the dead worker, we're not sentimental
console.log('Worker ' + worker.id + ' died');
var worker = cluster.fork();
worker.on('message', function (msg) {
miniOps.recorder()(msg.req, msg.res, msg.route, msg.error);
});
});
} else {
require('./server')(config.database.host + config.database.name, function (callback) {
//Start webserver
callback.listen(config.port, config.address);
});
console.log('Worker ' + cluster.worker.id + ' running!');
}