Reputation: 175
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.
Upvotes: 2
Views: 397
Reputation: 2395
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();
}
}
Upvotes: 2