希望你们做得很好。
我在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)
我找不到解决这个问题的具体方法。请帮帮我。
谢谢你们!
暂无答案!
目前还没有任何答案,快来回答吧!