2

Is it possible to queue and execute scripts as jobs in KUE https://github.com/LearnBoost/kue ?

Script like /usr/bin/myScript.sh -a 10 -b 20 -c 30
All the examples that I have seen suppose the job to be defined as Node.js function.

If it is possible, how I can define the job name, the script path and the script parameters?

Are there any better alternatives than KUE for the same task.
Preferably with UI and small memory footprint. :)

Antoan Milkov
  • 2,152
  • 17
  • 30
  • Have you thought about executing the external process from within the node app that is called from your Kue queue? Ex.: write a node script that contains a call to your external script and let Kue call your node script. – noderman Jun 20 '15 at 02:29

1 Answers1

2

First, write a node worker to consume the Kue jobs. Example: worker.js

Then, add the kue code:

var kue = require('kue')
, queue = kue.createQueue();

And add the listener to your desired code:

queue.process('MYEXTERNALJOB', function(job, done){
    runJob(job.data, done);
});

Try child_process.exec:

function runJob(data, done){
        //You can use the job's data object to pass your external script parameters
        var a = data.a;
        var b = data.b;
        var c = data.c;
        var yourCode = '/usr/bin/myScript.sh -a ' + a + ' -b ' + b + ' -c ' + c
        var exec = require('child_process').exec,
        child;

        child = exec(yourCode,
            function (error, stdout, stderr) {
                console.log('stdout: ' + stdout);
                console.log('stderr: ' +  stderr);
                if (error !== null) {
                    console.log('exec error: ' + error);
                    done(error); //callback with error for your publisher code
                }else{
                    done(null, stdout); // callback with the results, which you can potentially use on your publisher code
                }
            });
         }
}

Then, remember to run your worker so it can poll for Kue jobs:

$ node worker.js

Now you just need to create jobs for MYEXTERNALJOB on the main code

var kue = require('kue')
, queue = kue.createQueue();

var job = queue.create('MYEXTERNALJOB', {
    a: 10
  , b: 20
  , c: 30
}).save( function(err){
   if( !err ) console.log( job.id );
});

Remember to add event handlers for complete, failed and failed attempt before save.

noderman
  • 1,934
  • 1
  • 20
  • 36