1

希望你们做得很好。

我在我的 nodejs 项目中实现了 BullMQ(Bull 的下一个主要版本)来安排发送电子邮件的作业。例如,发送忘记密码请求的电子邮件。所以,我写了我的代码,如下所示。

用户服务:

await resetPasswordJob({email: 'xyz@test.com'});       // from service I'm calling a 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;

重置密码解析器:

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;

但问题是,如果我执行一次并尝试执行其他作业,那么该作业只是在执行,没有最新的。并在控制台中收到错误消息:

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)

我无法为这个错误找到任何具体的解决方案。请帮帮我。

感谢你们!

4

1 回答 1

1

您的问题可能是由几件事造成的。

首先,您有一项需要 30 多秒才能完成的工作。

其次,您将 worker.on 函数设置为侦听事件,并在内部完成或进行中,您会执行导致错误的操作。

第三,您在 redis 连接中设置 keyPrefix。只需将其删除。

于 2021-08-03T13:20:29.710 回答