Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js agenda: how to retry jobs after failure?

Tags:

node.js

Using Agenda, is it possible to set jobs to retry after several times after failure?

like image 491
Ronen Teva Avatar asked Nov 27 '22 04:11

Ronen Teva


1 Answers

If it's a repeating job, you can change the nextRunAt value when it fails and it will run again:

agenda.on("fail", async (error, job) => {
    const nextRunAt = new Date();
    job.attrs.nextRunAt = nextRunAt;
    await job.save();
});

if it's a scheduled job (meaning it doesn't repeat, the above code won't work. You have to create a new job.

agenda.on("fail", async (error, job) => {
    if(job.attrs.repeatInterval) {
        // create a new job and pass it job.attrs.data
    }
});
like image 165
Dev01 Avatar answered Jan 18 '23 07:01

Dev01