start.js 760 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env node --harmony --trace_gc
  2. var cluster = require('cluster');
  3. var http = require('http');
  4. if (cluster.isMaster) {
  5. var num = require('os').cpus().length;
  6. num = 4;
  7. for (var i = 0; i < num; i++) {
  8. cluster.fork();
  9. }
  10. cluster.on('exit', function(worker, code, signal) {
  11. console.error('worker ' + worker.process.pid + ' died');
  12. cluster.fork();
  13. });
  14. cluster.on('listening', function(worker, address) {
  15. console.log("A worker with #" + worker.id + " is now connected to "
  16. + address.address + ":" + address.port);
  17. });
  18. } else {
  19. var app = require('./app.js');
  20. var port = process.env.PORT || '3000';
  21. app.set('port', port);
  22. var server = http.createServer(app);
  23. require('./ws.js');
  24. server.listen(port);
  25. }