Cluster in nodejs - node.js

I have tried the below piece of code in ubuntu
var cluster = exports.cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died. Trying to respawn...');
cluster.fork();
});
} else {
//spawn express etc
}
How can I check the CPU performance in ubuntu with or without using cluster in nodejs.
Any help on this will be really helpful

Related

Node.js Cluster not forking anything

I am trying to implement clustering in my Node.js application.
When I print out how many workers are spawned inside the for-loop with the fork() method, it prints out nothing.
The coreCounter variable also = 0 if I print it out.
Here is my code:
let cluster = require('cluster');
if (cluster.isMaster) {
let coreCounter = require('os').cpus.length;
for (let i = 0; i < coreCounter; i++) {
cluster.fork();
}
cluster.on('exit', function () {
cluster.fork();
});
} else {
require('server.js');
}
I tried to npm install cluster and npm install os, it did not work, do I have to do npm install if I "require" something?
You just need to add () after the cpus.
See here:
let cluster = require('cluster');
if (cluster.isMaster) {
let coreCounter = require('os').cpus().length;
for (let i = 0; i < coreCounter; i++) {
cluster.fork();
}
cluster.on('exit', function () {
cluster.fork();
});
} else {
//require('server.js');
console.log('walla!')
}
You should not npm install NodeJS core modules.
P.S. I suggest you get some practice with NodeJS before implement clustering

Cron Job running multiple times under cluster environment in Node js

i have set up Node JS server cluster environment to fully utilize all the cores of my server. There is a Cron Job which runs every day 08 O'clock to run some tasks. But due to clustering it runs 4 times(server is of 4 cores) every day at 08 O'clock.
How can i over come this problem to run Cron Job only once a day?
if(cluster.isMaster) {
var numWorkers = require('os').cpus().length;
console.log('Master cluster setting up ' + numWorkers + ' workers...');
for(var i = 0; i < numWorkers; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
cluster.fork();
});
} else {
var CronJob = require('cron').CronJob;
new CronJob('01 30 08 * * 0-6', function() {
console.log('Running Schedular');
//Performing tasks
}, null, true, 'America/Los_Angeles');
var server = app.listen(port, function() {
console.log('Process ' + process.pid + ' is listening to all incoming requests');
});
}
Hi Please find the below changes, I just changed cron job into if condition.
var CronJob = require('cron').CronJob;
if(cluster.isMaster) {
var numWorkers = require('os').cpus().length;
console.log('Master cluster setting up ' + numWorkers + ' workers...');
for(var i = 0; i < numWorkers; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
cluster.fork();
});
new CronJob('01 30 08 * * 0-6', function() {
console.log('Running Schedular');
//Performing tasks
}, null, true, 'America/Los_Angeles');
} else {
var server = app.listen(port, function() {
console.log('Process ' + process.pid + ' is listening to all incoming requests');
});
}
Let the master cluster handle the cron job and not one of the worker threads.

Node.js: Limit forks in cluster

I have a Web application and an application that runs in background, this last load much the server, so far has been working properly, but in the last version to add new features, carries a lot of load and blocks the server.
I have been trying to modify the code that runs the cluster, but always load the same number of processes.
This is my code:
var cluster = require('cluster'),
limitWorkers = Math.ceil(require('os').cpus().length / 2),
jobWorkers = [],
webWorkers = [];
if(cluster.isMaster) {
//let limit = 0;
require('os').cpus().forEach(function() {
addWebWorker();
/*if(limit < limitWorkers) {
limit++;
addJobWorker();
}*/
if( limitWorkers > 0 ) {
--limitWorkers;
addJobWorker();
}
});
cluster.on('exit', function(worker, code, signal) {
if (jobWorkers.indexOf(worker.id) != -1) {
console.log('job worker ' + worker.process.pid + ' died. Trying to respawn...');
removeJobWorker(worker.id);
addJobWorker();
}
if (webWorkers.indexOf(worker.id) != -1) {
console.log('http worker ' + worker.process.pid + ' died. Trying to respawn...');
removeWebWorker(worker.id);
addWebWorker();
}
});
} else {
console.log('start http server: ' + cluster.worker.id);
require('./apps/web-http'); //initialize the http server here
//if( process.env.job === 1 ){
if( jobWorkers.indexOf(cluster.worker.id) != 1 ) {
//if( limitWorkers > 0 ) {
//limitWorkers--;
console.log('start job server: ' + cluster.worker.id);
require('./apps/jobs-worker'); //initialize the job here
//}
}
}
function addWebWorker() {
webWorkers.push(cluster.fork({web: 1}).id);
}
function addJobWorker() {
jobWorkers.push(cluster.fork({job: 1}).id);
}
function removeWebWorker(id) {
webWorkers.splice(webWorkers.indexOf(id), 1);
}
function removeJobWorker(id) {
jobWorkers.splice(jobWorkers.indexOf(id), 1);
}
Any idea to run the job worker half the cores of the processor or just one? Thanks.

Prevent multiple console logging output while clustering

I'm using the cluster module for nodejs.
Here is how I have it set up:
var cluster = require('cluster');
if (cluster.isMaster) {
var numCPUs = require('os').cpus().length;
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
}else{
console.log("Turkey Test");
}
Now, I am forking 6 threads (6 cores) on my PC. So, when debugging my app and reading data from the console, this will appear:
Is there anyway to make console.log output only once regardless of how many clusters are running?
You could use the fact that you can communicate with the workers, and send a message that tells each worker if it should log or not. You'd send it so that only one worker (The first one for example) should log:
var cluster = require('cluster');
if (cluster.isMaster) {
var numCPUs = require('os').cpus().length;
for (var i = 0; i < numCPUs; i++) {
cluster.fork().send({doLog: i == 0});
}
}else{
process.on('message', function(msg) {
if(msg.doLog) {
console.log('Turkey Test');
}
});
}

one worker listen to event in nodejs

I want to use Cluster module of nodejs but when event trigger,when the first worker capture it,other ones don't do anything.
var cluster = require('cluster')
numCPUs = require('os').cpus().length;
if (cluster.isMaster){
for (var i = 0; i < numCPUs; i++)
cluster.fork();
cluster.on('exit', function(worker, code, signal) {
cluster.fork();
});
} else {
var redis = require('redis');
var client = redis.createClient();
client.on('message',function(channel,message){
console.log(channel,message);
});
client.subscribe("CH_IP");
}
The above code has below output
CH_IP {"LOC":"OTHER","PID":1,"SID":1}
CH_IP {"LOC":"OTHER","PID":1,"SID":1}
CH_IP {"LOC":"OTHER","PID":1,"SID":1}
CH_IP {"LOC":"OTHER","PID":1,"SID":1}
CH_IP {"LOC":"OTHER","PID":1,"SID":1}
CH_IP {"LOC":"OTHER","PID":1,"SID":1}
CH_IP {"LOC":"OTHER","PID":1,"SID":1}
CH_IP {"LOC":"OTHER","PID":1,"SID":1}
How to edit code to only run one console.log?
The fork method accepts an object of key/pair values, which gets added to the workers process environment. You can use this to identify each worker, and single out one to do the work:
if (cluster.isMaster){
for (var i = 0; i < numCPUs; i++)
cluster.fork({ workerId: i+1 });
// etc...
} else {
if(process.env.workerId == 1){
// do stuff
}
}

Resources