Sunday 19 March 2017

NodeJS cluster don't recognize the master worker in clustering

I'm trying to cluster my node server so I was just testing the example code below.

The below code worked the first time I tried it. I created a new js file and ran the code - worked flawlessly.

Then I deleted the 'practice' js file and moved exactly the same code into my server file to implement it.

Now it won't ever recognize the first worker as the master worker... I have no idea what might have gone wrong.

I have tried setting process.env.NODE_UNIQUE_ID to undefined but it won't reset the master worker! so every time I run this code, I get "Application running!" without "worker loop" which should show everytime it loops through creating a worker, meaning it is not recognising the first worker as the master worker.

Does anyone know what the problem might be?

const cluster = require('cluster');
if (cluster.isMaster) {
    var cpuCount = require('os').cpus().length;

    for (var i = 0; i < cpuCount; i ++) {
        cluster.fork();
        console.log(`worker loop ${i}`)
}
} else {
    var express = require('express');
    var app = express();

    app.get('/', function (req, res) {
    res.send('Hello World!');
});

app.listen(3000);
console.log('Application running!');
}



via Ji Ho Choi

No comments:

Post a Comment