I’m prototyping a project using BullMQ and Redis, and so far, everything seems to be working fine. Jobs are being added to the queue successfully. However, when I process the jobs using a worker and call job.remove()
after completing the job, the job is not removed from the queue.
The queue remains filled with the job, and I have to manually run the FLUSHDB
command in redis-cli
to reset the contents of the queue.
Here is the code I’m using for the prototype:
import { Queue, Worker } from 'bullmq';
import { Redis } from 'ioredis';
const redis_connection = new Redis({
host: 'localhost',
port: 6379,
maxRetriesPerRequest: null
})
const myQueue = new Queue('my-queue',{
connection:redis_connection,
});
// Add jobs to the queue
for (let i = 0; i < 50; i++) {
myQueue.add('myJob', { foo: 'bar' , jobNumber: i });
}
const myWorker = new Worker('my-queue', async (job) => {
// Process the job data
console.log('Processing job:', job.data);
// even adding job.remove() here doesnt remove the job from the queue
}, {
connection: redis_connection,
});
myWorker.on('completed', async (job) => {
console.log(`Job with ID ${job.id} has been completed.`);
await job.remove(); // Should removes the job from the queue
});
myWorker.on('failed', async (job, err) => {
console.error(`Job with ID ${job.id} has failed with error: ${err.message}`);
await job.remove(); // Should remove the job from the queue
});