node.jsredisqueuejob-schedulingbullmq

Node Js: Redis job is not completing after finish its task


Hope you guys are doing great.

I implemented BullMQ (next major version of Bull) into my nodejs project to schedule the jobs to send emails. For example, send email of forget password request. So, I have written my code something like below.

User Service:

await resetPasswordJob({email: 'xyz@test.com'});       // from service I'm calling a job

Reset Password Job:

const {Queue} = require('bullmq');
const IOredis = require('ioredis');

const connection = new IOredis(process.env.REDIS_PORT || 6379);
const resetPasswordResolver = require('../resolvers/resetPasswordResolver');

const resetPasswordJob = async (payload) => {
  const queue = new Queue('default', {connection});        // 'default' is queue name

  // Added below line, because I was getting this issue sometime
  // "MaxListenersExceededWarning: Possible EventEmitter memory leak detected."
  queue.setMaxListeners(queue.getMaxListeners() + 1); 
      
  await queue.add('resetPassword', payload, {
    removeOnFail: true,
    removeOnComplete: true,
  });               // 'resetPassword' is job name

  const worker = new Worker('default', async (job) => 
     await resetPasswordResolver(job.data)
  );
  worker.on('completed', (job) => {
    console.log(`Worker Mesg: ${job.id} has completed.`);
    done();
  });
  worker.on('failed', (job, err) => {
    console.log(
      `Worker Mesg: ${job.id} has failed with ${err.message}!`
    );
    done();
  });
};

module.exports = resetPasswordJob;

Reset Password Resolver:

const sendMail = require('../../utils/sendMail');

const resetPasswordMailResolver = async (payload) => {
  const body = `<html>Some html email template here</html>`;
  await sendMail({to: payload.email, subject: 'Reset your account password', body: body});

  return {};
};

module.exports = resetPasswordMailResolver;

But the problem is, if I execute this once and try to execute some other job, that time this job is only executing, no the latest one. And getting a error message in console:

Worker Mesg: 7 has completed.
Error: Missing lock for job 7 failed
    at Function.finishedErrors (/home/admini/Documents/node-project/node_modules/bullmq/dist/classes/scripts.js:135:24)
    at Job.moveToFailed (/home/admini/Documents/node-project/node_modules/bullmq/dist/classes/job.js:197:41)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async handleFailed (/home/admini/Documents/node-project/node_modules/bullmq/dist/classes/worker.js:207:17)
    at async Worker.run (/home/admini/Documents/node-project/node_modules/bullmq/dist/classes/worker.js:90:33)

I'm not able to find any concrete solution for this bug. Please help me out.

Thank you guys!


Solution

  • your issue can be made by several things.

    first you have a job that takes more than 30 seconds to finish.

    second you set the worker.on function to listen on events and inside on completed or on progress you do something that causes the error.

    third you set the keyPrefix in redis connection. just remove it.