Advertisement
Guest User

Untitled

a guest
May 29th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var cluster = require('cluster');
  2. var PORT = +process.env.PORT || 1337;
  3.  
  4. if (cluster.isMaster) {
  5.     var cpuCount = require('os').cpus().length;
  6.     for (var i = 0; i < cpuCount; i++) {
  7.         cluster.fork();
  8.     }
  9.  
  10.     cluster.on('disconnect', function (worker) {
  11.         console.error('disconnect!');
  12.         cluster.fork();
  13.     });
  14. } else {
  15.     var domain = require('domain'),
  16.         Log = require('./modules/log'),
  17.         ErrorLog = new Log('error');
  18.     console.log('jestesm c');
  19.     var server = require('http').createServer(function (req, res) {
  20.  
  21.         var d = domain.create();
  22.         d.on('error', function (er) {
  23.             console.error('error', er.stack);
  24.             ErrorLog.write('error', er.stack);
  25.  
  26.             try {
  27.                 // make sure we close down within 30 seconds
  28.                 var killtimer = setTimeout(function () {
  29.                     process.exit(1);
  30.                 }, 30000);
  31.                 // But don't keep the process open just for that!
  32.                 killtimer.unref();
  33.  
  34.                 // stop taking new requests.
  35.                 server.close();
  36.  
  37.                 // Let the master know we're dead.  This will trigger a
  38.                 // 'disconnect' in the cluster master, and then it will fork
  39.                 // a new worker.
  40.                 cluster.worker.disconnect();
  41.  
  42.                 // try to send an error to the request that triggered the problem
  43.                 res.statusCode = 500;
  44.                 res.setHeader('content-type', 'text/plain');
  45.                 res.end('Oops, there was a problem!\n');
  46.             } catch (er2) {
  47.                 // oh well, not much we can do at this point.
  48.                 console.error('Error sending 500!', er2.stack);
  49.                 ErrorLog.write('Error sending 500!', er2.stack);
  50.             }
  51.         });
  52.  
  53.         // Because req and res were created before this domain existed,
  54.         // we need to explicitly add them.
  55.         // See the explanation of implicit vs explicit binding below.
  56.         d.add(req);
  57.         d.add(res);
  58.  
  59.         // Now run the handler function in the domain.
  60.         d.run(function () {
  61.             handleRequest(req, res);
  62.         });
  63.     });
  64.     server.listen(PORT);
  65. }
  66.  
  67. // This part isn't important.  Just an example routing thing.
  68. // You'd put your fancy application logic here.
  69. function handleRequest(req, res) {
  70.     //console.log(req.headers);
  71.  
  72.     var body = "";
  73.     req.on('data', function (chunk) {
  74.         body += chunk;
  75.     });
  76.     req.on('end', function () {
  77.         if (req.method == 'POST') {
  78.             console.log('POSTed: ' + body);
  79.         }
  80.  
  81.         var responseData = JSON.stringify({
  82.             'gourl': 'http://google.com',
  83.             'worker': cluster.worker.id
  84.         });
  85.         res.writeHead(200, {'Content-Type': 'text/plain'});
  86.         res.end(responseData);
  87.     });
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement