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

♀尐吖头ヾ 提交于 2020-12-13 03:04:38

问题


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!

来源:https://stackoverflow.com/questions/64366547/node-js-redis-job-is-not-completing-after-finish-its-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!