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 6 years ago
  • Reactions: 8
  • Comments: 60 (26 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);
})()

Bump, I have a similar use case as @IoannMelnychuk where my job sends HTTP requests to an external service and I need to update the status of the job from this external server once it finishes the process. Steps:

  • Send HTTP request from job processor, wait using job.waitUntilFinished()
  • Handle HTTP request from external service, perform large process
  • Once the large process finishes, obtain the job using Job.fromId() and afterwards update the status of the job using job.moveToCompleted() or job.moveToFailed() accordingly

Result: Missing lock for job 13. finished

Is there a way of doing this without manually processing jobs? Thanks!

@manast how to own the lock for the job? Not with getNextJob method, but with more accurate way (fromId, getJob, etc)?

I need to find a job by ID and process this job manually

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);
}