gpt4 book ai didi

node.js - Node Js : Redis job is not completing after finish its task

转载 作者:行者123 更新时间:2023-12-05 03:48:27 29 4
gpt4 key购买 nike

希望你们做得很好。

我在我的 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)

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

谢谢大家!

最佳答案

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

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.

关于node.js - Node Js : Redis job is not completing after finish its task,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64366547/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com