BVB Source Codes

prerender Show index.js Source code

Return Download prerender: download index.js Source code - Download prerender Source code - Type:.js
  1. var cluster = require('cluster')
  2.   , os = require('os')
  3.   , fs = require('fs')
  4.   , path = require('path')
  5.   , http = require('http')
  6.   , _ = require('lodash')
  7.   , util = require('./util')
  8.   , basename = path.basename
  9.   , onExit = require('signal-exit');
  10.  
  11. // Starts either a server or client depending on whether this is a master or
  12. // worker cluster process
  13. exports = module.exports = function(options) {
  14.     var port = options.port || process.env.PORT || 3000;
  15.     var hostname = options.hostname || process.env.NODE_HOSTNAME || undefined;
  16.  
  17.     var server = require('./server');
  18.     options.isMaster = cluster.isMaster;
  19.     options.worker = cluster.worker;
  20.     server.init(options);
  21.  
  22.     if(cluster.isMaster) {
  23.  
  24.         var workersPhantomjsPid = {};
  25.  
  26.         for (var i = 0; i < (options.workers || os.cpus().length); i += 1) {
  27.             util.log('starting worker thread #' + i);
  28.             var worker = cluster.fork();
  29.  
  30.             worker.on('message', function(msg) {
  31.                 workersPhantomjsPid[this.id] = msg['phantomjsPid'];
  32.             });
  33.         }
  34.  
  35.         cluster.on('exit', function (worker) {
  36.             if (worker.suicide === true || worker.exitedAfterDisconnect === true) return;
  37.  
  38.             if(workersPhantomjsPid[worker.id]) {
  39.                 process.kill(workersPhantomjsPid[worker.id], 'SIGKILL');
  40.                 delete workersPhantomjsPid[worker.id];
  41.             }
  42.  
  43.             util.log('worker ' + worker.id + ' died, restarting!');
  44.             cluster.fork();
  45.         });
  46.     } else {
  47.         var httpServer = http.createServer(_.bind(server.onRequest, server));
  48.  
  49.         httpServer.listen(port, hostname, function () {
  50.             util.log('Server running on port ' + port);
  51.         });
  52.  
  53.         onExit(function() {
  54.             util.log('Terminating worker #' + cluster.worker.id);
  55.             server.exit();
  56.         });
  57.     }
  58.  
  59.     return server;
  60. };
  61.  
  62. fs.readdirSync(__dirname + '/plugins').forEach(function(filename){
  63.     if (!/\.js$/.test(filename)) return;
  64.     var name = basename(filename, '.js');
  65.     function load(){ return require('./plugins/' + name); }
  66.     Object.defineProperty(exports, name, {value: load});
  67. });
  68.  
downloadindex.js Source code - Download prerender Source code
Related Source Codes/Software:
libsodium - A modern and easy-to-use crypto library. 2017-01-09
Side-Menu.Android - Side menu with some categories to choose. ... 2017-01-09
docker-gitlab - Dockerized GitLab http://www.da... 2017-01-09
sinon - Test spies, stubs and mocks for JavaScript. ... 2017-01-09
in-view - Get notified when a DOM element enters or exits th... 2017-01-09
falcon - Falcon is a low-level, high-performance Python fra... 2017-01-09
ImageOptim - GUI image optimizer for Mac htt... 2017-01-09
node-style-guide - A guide for styling your node.js / JavaScript code... 2017-01-09
FDFullscreenPopGesture - A UINavigationController's category to enable full... 2017-01-10
Gource - oftware version control visualization ... 2017-01-10
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top