|
| 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 |
0 commit comments