2

I am trying to create a multiprocess application but I am having problems with NodeJS clustering api. Now it is very simple, however I'm not able to make it work. This is my code. Clustering.js:

var cluster = require('cluster');

var port = 8101;

if (cluster.isMaster) {

 var startChooser = function (chooserId) {
     cluster.fork({WorkerName: chooserId, port: port});
     port++;
 }
}
else {
    console.log(process.env.WorkerName);
}

module.exports.startChooser = startChooser

This is my main to do simple tests:

var clustering = require('./clustering');

clustering.startChooser('12345');

The problem is that it is showing this error:

/usr/local/bin/node /Users/../../main.js
12345
/Users/../../main.js:38
clustering.startChooser('12345');
           ^

TypeError: clustering.startChooser is not a function
    at Object.<anonymous>         (/Users/../../main.js:38:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

So, apparently it is working because it is printing the workerName but it is showing that error.

2
  • 3
    module.exports = { startChooser: startChooser } The error is telling you that startChooser is not defined properly. And you are only defining startChooser if a condition is met, this needs a refactor. Commented Apr 10, 2018 at 8:43
  • The master is the only one who can fork and create a new worker, there is no other way to do that... I don't know how to fix this. Commented Apr 10, 2018 at 9:04

1 Answer 1

2

Please use PM2 (https://github.com/Unitech/pm2) if you want to cluster your application. It's much easier and you don't need to modify your code. It automatically assumes that your code is a child process and pm2 manages all processes and it also gives your monitoring.

Also it helps you avoiding mistakes as you did and it's a well tested library in production.

Your problem:

The main concept of clustering is that your main file it's the guy who requires the child code to fork. Your problem you are only defining function startChooser when you are the master... if your process is a child then the function is not defined.

Cool tutorials: http://dealwithjs.io/scaling-clustering-in-node-js/ https://rowanmanning.com/posts/node-cluster-and-express/

Here is an example how you should use cluster module. You should listen 'exit' event if you want to respawn a child if some dies.

const cluster = require('cluster');
const numCPUs = require('os').cpus().length;

if (cluster.isWorker) {
  require('./worker-lib')
} else {
  console.log('I am a master');
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have to use the cluster library in nodeJS. Thanks for your answer but I have to use that library.
I understand that, but PM2 uses cluster under the hood and manages the lifetime of your children process. I have updated my answer to give you some hints how to use cluster module! I hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.