Nesh
Nesh

Reputation: 2561

Run two tasks in two different workers using cluster in nodejs

I am learning clustering in NodeJS. I have two tasks one is node-sass and another one is uglifyjs which I want to run by two different workers using cluster in nodeJS. Though the code is working file and creating the SASS -> CSS file and main.js to main.min.js file.

But I am not sure whether it is handled by separate workers or not. Let me know where I can make the amendments to make -

Following is my code:

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
var fs = require('fs');
var UglifyJS = require("uglify-js");
var sass = require('node-sass');

if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`);

  // Fork workers.
  for (let i = 0; i < 2; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`);
  });
} else {


var result = UglifyJS.minify("js/main.js");
fs.writeFile(__dirname + '/js/main.min.js', result.code, function(err){
    if(err)
        throw err;
});

sass.render({
  file: './css/main.scss',
  outFile: 'css',
}, function(err, result) { 
    if(err)
        throw err;
    fs.writeFile(__dirname + '/css/main.css', result.css, function(err){
        if(err)
            throw err;
    });
});

  console.log(`Worker ${process.pid} started`);
}

Upvotes: 1

Views: 3210

Answers (3)

shijin
shijin

Reputation: 3046

For poc part I tried node cluster and pm2, pm2 looks very easy to setup. pm2 also can keep the node project running in background. Add pm2 command in your build script or just try this and see how it works

pm2 start app.js -i max

Refer http://pm2.keymetrics.io/docs/usage/cluster-mode/

Upvotes: 0

Basilin Joe
Basilin Joe

Reputation: 702

I think this will help

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
var fs = require('fs');
var UglifyJS = require("uglify-js");
var sass = require('node-sass');

if (cluster.isMaster) {
    console.log(`Master ${process.pid} is running`);
    cluster.fork()
        .on('exit', (worker, code, signal) => {
            console.log(`worker ${worker.process.pid} died`);
        });
    cluster.fork()
        .on('exit', (worker, code, signal) => {
            console.log(`worker ${worker.process.pid} died`);
        });

} else if (cluster.worker.id === 1) {
    console.log(`Worker ${process.pid} started`);
    sass.render({
        file: './css/main.scss',
        outFile: 'css',
    }, function (err, result) {
        if (err)
            throw err;
        fs.writeFile(__dirname + '/css/main.css', result.css, function (err) {
            if (err)
                throw err;
        });
    });
    process.exit();
} else {
    var result = UglifyJS.minify("js/main.js");
    fs.writeFile(__dirname + '/js/main.min.js', result.code, function (err) {
        if (err)
            throw err;
    });
    process.exit();
}

Upvotes: 1

twg
twg

Reputation: 1105

In the cluster --> master-slave scenarios the real basic code, as in many parts of the original Node.js structure, is sometimes a bit more complicated than just declaring a master and slave. Here is one of the cases where I would strongly advise a couple of hours searching NPM and finding a module that will work for your schema. I have tested cluster-master but in your case you may actually need more than one NPM module. It would be well to keep in mind that clusters usually mean cores where you are going to fork -- above code cluster.fork();.

You want to implement the cluster master-worker paradigm correctly and you want a return from each worker and know the process is running as you think it should. Either that means delving deep into the Node.js cluster documentation and implementation, or researching the various NPM modules available which will usually obfuscate the hard work for you.

Upvotes: 1

Related Questions