简体   繁体   中英

Node.js, Express, MongoDB - cluster worker crashes

My problem is one of the workers in the cluster crashes when I hit an express route.

4 workers:

API started on port 8000
API started on port 8000
API started on port 8000
API started on port 8000

Then I browse to http://localhost:8000

worker 1 has died :(

After the worker dies, the whole cluster becomes unresponsive. It works fine when I only use one cpu.

server.js

var cluster = require('cluster');


if(cluster.isMaster){

    var cpuCount = require('os').cpus().length;

    // Create a worker for each CPU
    for (var i = 0; i < cpuCount; i += 1) {
        cluster.fork();
    }

    cluster.on('exit', function(worker) {
        console.log('worker:' + worker.id + " is dead");
        cluster.fork();
    });

} else {
    var init = require('./config/init')();
    var config = require('./config/config');
    var mongoose = require('mongoose');
    var chalk = require('chalk');

    var db = mongoose.connect(config.db, function(err) {
        if (err) {
            console.error(chalk.red('Could not connect to MongoDB!'));
            console.log(chalk.red(err));
        }
    });

    var app = require('./config/express')(db);

    app.listen(config.port);

    require('./config/passport')();

    console.log('API started on port ' + config.port);   
}

You may want to to try JXcore , which supports multitasking (threads/instances under single process). From that reason clustering is not needed any more. Who knows, maybe this would work better for you.

So let's say, that you take just a worker code (I just paste it here without any change):

var init = require('./config/init')();
var config = require('./config/config');
var mongoose = require('mongoose');
var chalk = require('chalk');

var db = mongoose.connect(config.db, function(err) {
  if (err) {
    console.error(chalk.red('Could not connect to MongoDB!'));
    console.log(chalk.red(err));
  }
});

var app = require('./config/express')(db); 
app.listen(config.port);
require('./config/passport')();
console.log('API started on port ' + config.port);

and then you run it with mt-keep parameter (meaning: "multithread-keepalive"):

$ jxcore mt-keep:4 worker.js

The number 4 states for 4 cores - please change it to fit your platform.

Please note that I didn't test it. I'm just showing an alternative to the multi-process clusters. Also, depending on the implementation, JXcore may be even more efficient.

There is an one-year old article, which highlights performance differences between JXcore and Node.JS: JXcore vs Vert.x vs Node.JS Cluster .

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