bullmq: How to use moveToFailed and moveToCompleted

Issuehunt badges

These two functions are different from v3 and I just don’t know what to set for the token.

job.moveToFailed(new Error('failed message'), ???);


IssueHunt Summary

manast manast has been rewarded.

Backers (Total: $40.00)

Submitted pull Requests


Tips

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 8
  • Comments: 60 (25 by maintainers)

Commits related to this issue

Most upvoted comments

@manast Of course, sorry about that…

const { Queue, QueueScheduler, Worker } = require('bullmq');

const connection = {connection: {port: '6379', host: 'localhost'}};
const queue = new Queue('myqueue', connection).on("error", (err) =>{console.log(err);});
new QueueScheduler('myqueue', Object.assign({stalledInterval: 1000, maxStalledCount:Number.MAX_SAFE_INTEGER}, connection)).on("error", (err) =>{console.log(err);});

(async () =>{
    await queue.add('test1', {}, {
        attempts: Number.MAX_SAFE_INTEGER
    }).catch(err => {
        console.log(err);
    });
    await queue.add('test2', {}, {
        attempts: Number.MAX_SAFE_INTEGER
    }).catch(err => {
        console.log(err);
    });
    await queue.add('test3', {}, {
        attempts: Number.MAX_SAFE_INTEGER
    }).catch(err => {
        console.log(err);
    });

    async function getJob(complete){
        const worker = new Worker('myqueue', null, Object.assign({lockDuration: 2000}, connection));
        const next = await worker.getNextJob('test@gmail.com');

        if(complete){
            const job = await queue.getJob(next.id);
            if(job){
                console.log('complete: ' + next.id);
                await job.moveToCompleted('completed by: ' + 'test@gmail.com' + ' at: ' + Date.now(), 'test@gmail.com');
            }
        }
    }


    getJob(false);
    setTimeout(getJob, 1000, false);
    setTimeout(getJob, 2000, true);
})()

Thank you both!

One will have to manually process the queue to use moveToFailed()?

My approach was to use it like this (simplified) in process functions:

worker.on("completed", async (job: job) => {
  [ ... ]
  // move some job in job.data to failed
  await moveJobToFailedById(job.data.someOtherJobId);
});

async moveJobToFailedById(jobId: string) {
  const job = await Job.fromId(myQueue, jobId);
  await job.moveToFailed(new Error(errorMsg), myQueue.token);
}