运行下面的代码时,它会打印出许多结果。我怀疑该completed事件侦听当前队列实例中所做的所有先前作业。
如何管理已完成事件以仅侦听当前作业完成情况?
生产者创建一个带有默认数字 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 …Run Code Online (Sandbox Code Playgroud)