Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var cluster = require('cluster');
- var PORT = +process.env.PORT || 1337;
- if (cluster.isMaster) {
- var cpuCount = require('os').cpus().length;
- for (var i = 0; i < cpuCount; i++) {
- cluster.fork();
- }
- cluster.on('disconnect', function (worker) {
- console.error('disconnect!');
- cluster.fork();
- });
- } else {
- var domain = require('domain'),
- Log = require('./modules/log'),
- ErrorLog = new Log('error');
- console.log('jestesm c');
- var server = require('http').createServer(function (req, res) {
- var d = domain.create();
- d.on('error', function (er) {
- console.error('error', er.stack);
- ErrorLog.write('error', er.stack);
- try {
- // make sure we close down within 30 seconds
- var killtimer = setTimeout(function () {
- process.exit(1);
- }, 30000);
- // But don't keep the process open just for that!
- killtimer.unref();
- // stop taking new requests.
- server.close();
- // Let the master know we're dead. This will trigger a
- // 'disconnect' in the cluster master, and then it will fork
- // a new worker.
- cluster.worker.disconnect();
- // try to send an error to the request that triggered the problem
- res.statusCode = 500;
- res.setHeader('content-type', 'text/plain');
- res.end('Oops, there was a problem!\n');
- } catch (er2) {
- // oh well, not much we can do at this point.
- console.error('Error sending 500!', er2.stack);
- ErrorLog.write('Error sending 500!', er2.stack);
- }
- });
- // Because req and res were created before this domain existed,
- // we need to explicitly add them.
- // See the explanation of implicit vs explicit binding below.
- d.add(req);
- d.add(res);
- // Now run the handler function in the domain.
- d.run(function () {
- handleRequest(req, res);
- });
- });
- server.listen(PORT);
- }
- // This part isn't important. Just an example routing thing.
- // You'd put your fancy application logic here.
- function handleRequest(req, res) {
- //console.log(req.headers);
- var body = "";
- req.on('data', function (chunk) {
- body += chunk;
- });
- req.on('end', function () {
- if (req.method == 'POST') {
- console.log('POSTed: ' + body);
- }
- var responseData = JSON.stringify({
- 'gourl': 'http://google.com',
- 'worker': cluster.worker.id
- });
- res.writeHead(200, {'Content-Type': 'text/plain'});
- res.end(responseData);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement