Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

NodeJs Node.js by default is using a single core of a processor for code execution. Therefore, NodeJs Node.js introduced a cluster module to spawn processes. “Cluster” was introduced to scale an application execution on multiple processor cores by creating worker processes. Worker processes share a single port, therefore, requests are routed through a shared port.

...

Code Block
languagejavascript
titleprofoundjs/start.js
#!/usr/bin/env node                              
                                                 
var cluster = require("cluster");                
                                                 
if (cluster.isMaster) {                          
   var conf = require("./cluster.json");         
                                                 
   // Figure out number of workers.              
   var numWorkers = conf.workers;                
   if (numWorkers == 0) {                        
      var numCPUs = require("os").cpus().length; 
      if (numCPUs < conf.min_workers) {          
         numWorkers = conf.min_workers;          
      } else {                                   
         numWorkers = numCPUs;                   
      }                                          
   }                                             
 
   // Fork workers.                                       
   //console.log("Starting " + numWorkers + " workers."); 
   for (var i = 0; i < numWorkers; i++) {                 
       cluster.fork();                                    
   }                                                      
                                                          
   cluster.on('exit', (worker, code, signal) => { });     
                                                          
} else {                                                  
                                                          
   // Load Profound.js                                    
   var profoundjs = require("profoundjs");                
                                                          
   // Apply configuration                                 
   var config = require("./config.js");                   
   profoundjs.applyConfig(config);                        
                                                          
   // Start Profound.js server                            
   var isWorker = profoundjs.server.listen();                              
   if (isWorker) {                                                         
                                                                           
      // This is the top-level Express Application.                        
      // Custom Express coding can be added here.                          
      var express = profoundjs.server.express;                             
      var app = profoundjs.server.app;                                     
      app.use(express.json());  // default to use JSON-encoded post data   
                                                                           
   }                                                                       
}                                                                          

#!/usr/bin/env node                             

                                                 

var cluster = require("cluster");               

                                                 

if (cluster.isMaster) {                         

   var conf = require("./cluster.json");        

                                                 

   // Figure out number of workers.             

   var numWorkers = conf.workers;               

   if (numWorkers == 0) {                       

      var numCPUs = require("os").cpus().length;

      if (numCPUs < conf.min_workers) {         

         numWorkers = conf.min_workers;         

      } else {                                  

         numWorkers = numCPUs;                  

      }                                         

   }                                            

 

   // Fork workers.                                      

   //console.log("Starting " + numWorkers + " workers.");

   for (var i = 0; i < numWorkers; i++) {                

       cluster.fork();                                   

   }                                                      

                                                          

   cluster.on('exit', (worker, code, signal) => { });    

                                                          

} else {                                                  

                                                          

   // Load Profound.js                                   

   var profoundjs = require("profoundjs");               

                                                          

   // Apply configuration                                

   var config = require("./config.js");                  

   profoundjs.applyConfig(config);                       

                                                          

   // Start Profound.js server                            

   var isWorker = profoundjs.server.listen();                             

   if (isWorker) {                                                        

                                                                           

      // This is the top-level Express Application.                       

      // Custom Express coding can be added here.                         

      var express = profoundjs.server.express;                            

      var app = profoundjs.server.app;                                    

      app.use(express.json());  // default to use JSON-encoded post data  

                                                                           

   }                                                                      

}                                                                         

...