简体   繁体   中英

cluster in mongodb and how does it picks up the cores

So I am new to cluster and mongodb and i came across this bunch of code.

#!/usr/bin/env node
var cluster = require('cluster');
var os = require('os');
var app = require('../main')
var models = require("../models");


if(cluster.isMaster){
  var mCoreCount = os.cpus().length;
  console.log("Cores : ", mCoreCount);

  for (var i = 0; i < mCoreCount; i++) {
    cluster.fork();
  }

  cluster.on('exit', function(){
    cluster.fork();
  });
}else{
    models.sequelize.sync().then(function(){
      app.listen(app.get('port'), function(){
        console.log('api is live.' + app.get('port'));
      });
    });
}

So when I console I get cores as 4, I tried reading but I could not understand anything , If someone could point me whats going on here It will be a great help. I understood that the greater the number of cores the node instances will increase , but I guess right now its picking up from my system, what happens in production ?.

This script is trying to get the more efficient way to launch the NodeJS app by creating a fork for each available core on the hardware server.
It picks up the number of core with os.cpus().length

In production, it will append the same process, and the number of fork will depend of the number of available core production server.

Are you really sure the database is MongoDB in both environement ? We can't really tell without seeing the whole app code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM