Skip to content

Commit 842ce2e

Browse files
committed
Merge pull request #5 from rohiniwork/nodejs-highavailability
Adding example for using monitr, process-watcher and mod_statuspage
2 parents b4b8667 + d17c58c commit 842ce2e

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2013, Yahoo! Inc. All rights reserved.
3+
* Copyrights licensed under the New BSD License.
4+
* See the accompanying LICENSE file for terms.
5+
*/
6+
7+
// Reference: TODO put the blog link here
8+
9+
var cluster = require('cluster'),
10+
express = require('express'),
11+
http = require('http'),
12+
monitor = require('monitr'),
13+
status = require('mod_statuspage'),
14+
timers = require('timers');
15+
// Spawn numCPUs worker processes
16+
var numCPUs = require('os').cpus().length,
17+
clusterStartTime = Date.now(),
18+
newWorkerEnv = {};
19+
20+
// Master spawns worker processes
21+
if (cluster.isMaster) {
22+
newWorkerEnv.clusterStartTime = clusterStartTime;
23+
24+
// Fork workers
25+
for (var i = 0; i < numCPUs; i++) {
26+
console.log('Starting worker ' + i);
27+
cluster.fork(newWorkerEnv);
28+
}
29+
30+
// If any worker process dies, spawn a new one to bring back number of processes
31+
// to be back to numCPUs
32+
cluster.on('exit', function(worker, code, signal) {
33+
console.log('worker ' + worker.process.pid + ' died');
34+
cluster.fork(newWorkerEnv);
35+
});
36+
37+
// Logs to know what workers are active
38+
cluster.on('online', function (worker) {
39+
console.log('Worker ' + worker.process.pid + ' online');
40+
});
41+
42+
} else {
43+
// Worker process
44+
// Start monitoring
45+
monitor.start();
46+
47+
if (process.env.clusterStartTime) {
48+
process.clusterStartTime = new Date(parseInt(process.env.clusterStartTime,10));
49+
}
50+
51+
// Simple express app
52+
var app = express();
53+
// Server statuspage to get application metrics
54+
app.use(status({
55+
url: '/status',
56+
check: function(req) {
57+
if (req.something == false) {
58+
return false; //Don't show status
59+
}
60+
return true; //Show status
61+
},
62+
responseContentType : 'html'
63+
}));
64+
// An example of a request that makes CPU idle for some time
65+
app.get('/late-response', function(req, res) {
66+
var end = Date.now() + 5000
67+
// CPU cycles wasted for 5000ms
68+
// Blocking
69+
while (Date.now() < end) ;
70+
res.writeHead(200, {'Content-Type': 'text/plain'});
71+
res.end('I came after a delay\n');
72+
});
73+
// All other requests served normal
74+
app.get('*', function (req, res) {
75+
res.writeHead(200, {'Content-Type': 'text/plain'});
76+
res.end('I am being monitored\n');
77+
});
78+
79+
console.log('Go to: http://127.0.0.1:8000/status');
80+
app.listen(8000);
81+
82+
// Stop monitoring
83+
process.on('exit', function () {
84+
monitor.stop();
85+
});
86+
87+
// Graceful shutdown
88+
process.on('SIGINT', function () {
89+
process.exit();
90+
});
91+
} // end worker
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2013, Yahoo! Inc. All rights reserved.
3+
* Copyrights licensed under the New BSD License.
4+
* See the accompanying LICENSE file for terms.
5+
*/
6+
// Reference : Put the blog post here
7+
var watcher = require('process-watcher');
8+
9+
/*
10+
* Dummy metric monitoring object.
11+
*/
12+
var watcher_metric = {
13+
/**
14+
* Increments metric
15+
*/
16+
increment : function (name, v) {
17+
console.log('Increment ' + name + ' with ' + v);
18+
},
19+
/**
20+
* Set the metric or multiple metrics at the same time.
21+
*/
22+
set : function (name, v) {
23+
console.log('Setting the following metrics: ' + require('util').inspect(name));
24+
}
25+
};
26+
var dgpath = '/tmp/nodejs.mon',
27+
statusPath = '/tmp/watcher_status_path_test',
28+
watcher_config = { max_inactive : 0.001, monitor : 0.001, monPath: dgpath,
29+
timeout : 3, timeout_start : 60 };
30+
31+
//Instantiate watcher
32+
var watcher_instance = new watcher.Watcher({ metric : watcher_metric, config : watcher_config });

0 commit comments

Comments
 (0)