【发布时间】:2019-08-22 13:58:42
【问题描述】:
运行下面的代码时会打印太多结果。
我怀疑completed 事件监听了当前队列实例中所有先前的作业。
如何管理已完成事件以仅监听当前作业完成?
producer.js
生产者创建一个具有默认数字 id 的作业并侦听全局完成,以便在作业完成时返回响应。
const BullQ = require('bull');
let bullQ = BullQ('my-first-queue', {
redis: {
host: process.env.REDISHOST || 'localhost',
password: process.env.REDISPASSWORD || ''
}
});
app.get('/search/:term', async (req, res) => {
const job = await bullQ.add({
searchTerm: req.params.term
});
// Listen to the global completion of the queue in order to return result.
bullQ.on('global:completed', (jobId, result) => {
// Check if id is a number without any additions
if (/^\d+$/.test(jobId) && !res.headersSent) {
console.log(`Producer get: Job ${jobId} completed! Result: ${result}`);
res.json(`Job is completed with result: ${result}`);
}
});
});
consumer.js
消费者有两个角色。
- 按本应有的方式使用工作
- 根据上一个作业的结果创建新作业。
const BullQ = require('bull');
let bullQ = BullQ('my-first-queue', {
redis: {
host: process.env.REDISHOST || 'localhost',
password: process.env.REDISPASSWORD || ''
}
});
bullQ.process((job, done) => {
// Simulate asynchronous server request.
setTimeout(async () => {
// Done the first job and return an answer to the producer after the timeout.
done(null, `Search result for ${job.data.searchTerm}`);
// next job run
if (counter < 10) {
// For the first run the id is just a number if not changed via the jobId in JobOpts,
// the next time the job id will be set to {{id}}_next_{{counter}} we need only the first number in order not to end with a long and not clear concatenated string.
let jobID = (/^\d+$/.test(job.id)) ? job.id : job.id.replace(/[^\d].*/,'');
await createNextJob(jobID, ++counter);
}
}, 100);
});
// Create next job and add it to the queue.
// Listen to the completed jobs (locally)
const createNextJob = async (id, counter) => {
const nextJob = bullQ.add({
searchTerm: "Next job"
}, {
jobId: `${id}_next_${counter}`
});
await bullQ.on('completed', (job, result) => {
job.finished();
console.log(`Consumer(next): Job ${job.id} completed! Result: ${result}`);
});
};
【问题讨论】:
-
嘿,请尝试在末尾添加
bullQ.on('global:failed', (jobid,data){console.log(jobid,data)});,因为我发现我的工作职能中有一个错误,没有被报告导致此问题
标签: javascript node.js queue bull.js