retry failed kue in nodejs

Spread the love

Hello there, today I’m gonna write about how to clear kue and retry failed kue.

Kue is a priority job queue backed by redis, built for node.js. Sometimes, our jobs will failed, that is when they are marked as a failure, and remain that way until you intervene. In this case you will want to remove or re-attempt them.

First of all you need to find all those failed jobs:

kue.Job.rangeByState( 'failed', 0, n, 'asc', function( err, jobs ) {
// you have an array of maximum n Job objects here
});

Next, if you want to remove those jobs from, simply throw this code:

async.eachSeries(jobs, function(job, cb) {
    job.remove(cb);
}, cb);

In case you want to retry them, change their state from
failed to inactive:

async.eachSeries(jobs, function(job, cb) {
    job.state('inactive').save(cb);
}, cb);

To sum up, the whole function to retry fail jobs is:


var retryFailedKue = function(cb) {
    var n = 2000;
    var kue = require('kue');
    async.waterfall([
        function (cb) {
            kue.Job.rangeByState('failed', 0, n, 'asc', cb);
        },
        function (jobs, cb) {
            async.eachSeries(jobs, function(job, cb) {	
                job.state('inactive').save(cb);
            }, cb);
        }],
        function (err) {
            if (err) console.log(err);
            process.exit();
        });
};