Search code examples
javascriptnode.jscallbackkue

execute callback after Kue job is completed


I have seen questions like this but they do not answer my question.

I have a function

var kue = require('kue');
var jobs = kue.createQueue();

var create_job = function(data, callback){
    jobs.create('job', {
        stuff: data   
    }).save();
}

How can I make it so that callback is called when jobs.process('job', function(job, done){ ... is finished?


Solution

  • I've not used kue before, so this is going off their documentation:

    var kue = require('kue');
    var jobs = kue.createQueue();
    
    var create_job = function(data, callback){
        var job = jobs.create('job', {
            stuff: data   
        }).save();
        job.on('complete', function(){
            callback();
        }).on('failed', function(){
          callback({error: true});
        });    
    };
    

    I've created/used a JavaScript closure to capture the value of the callback argument (by attaching the events within the scope of the create_job function, and it's later called when the function completes (or fails).