gpt4 book ai didi

node.js - Bull 队列尚未完成

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:16 29 4
gpt4 key购买 nike

我是 Bull 的新手。我尝试根据他们的文档代码运行 bull。流程正在启动,但我的工作尚未完成,或者不确定它是否触发完成事件?不确定哪里出错了

附上我的代码

const Queue = require('bull');

const myFirstQueue = new Queue('my-first-queue',
{
redis: {
port: Config.redis.port,
host: Config.redis.host,
password: Config.redis.password
},
});



(async function ad() {
const job = await myFirstQueue.add({
foo: 'bar',
});
})();

myFirstQueue.process(async (job, data) => {
log.debug({ job, data }, 'Job data');
let progress = 0;
for (let i = 0; i < 10; i++) {
await doSomething(data);
progress += 10;
job.progress(progress).catch(err => {
log.debug({ err }, 'Job progress err');
});
log.debug({ progress }, 'After await');
}

return job;
});

const doSomething = data => {
return new Promise((resolve, reject) => {
return resolve(data);
});
};

myFirstQueue.on('completed', (job, result) => {
log.debug(`Job completed with result ${job}`);
});


myFirstQueue.on('progress', (job, progress) => {
log.debug(`Job progress with result ${job} ${progress}`);
});

我可以看到进度事件处理程序内的日志,但未触发完整事件。任何帮助表示赞赏

最佳答案

您需要从流程中调用done(),然后只有完成的事件才会触发。

myFirstQueue.process(async (job, done) => {
const data = job.data;
let progress = 0;
for (let i = 0; i < 10; i++) {
await doSomething(data);
progress += 10;

job.progress(progress).catch(err => {
log.debug({ err }, 'Job progress err');
});
}

done();
});

关于node.js - Bull 队列尚未完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56256609/

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