Node.js
Consumer
Learn job processing with a Bull consumer/process.
Consumer
The consumer is the side that takes messages from the queue and processes them. In Bull you define a consumer with queue.process((job) => { ... }). The callback is called for each message; if it returns a Promise, Bull waits for it to finish.
Example: kuyruk.process((job) => { console.log("Processing: " + job.data.action); return Promise.resolve(); });. When a message is taken from the queue, the callback is called with a job parameter. job.data is the message's data. If the callback returns a Promise, resolve means success, reject means failure. Failed jobs are retried. Multiple consumers can process the queue in parallel.
job.data and Promise
The consumer callback takes a job parameter. job.data is the message's data. The callback should return a Promise; resolve means success, reject means failure.
Example: kuyruk.process(async (job) => { const user = job.data.user; await sendMail(user); });. An async function automatically returns a Promise, so no explicit return is needed. If sendMail succeeds, the job is completed; if it throws, Bull marks the job as failed and applies the retry rule. job.attemptsMade tells how many times it has been tried. Since the consumer is asynchronous, multiple jobs can run in parallel; Bull limits parallelism with { concurrency: 5 }.
Exercise Define a consumer
Use the kuyruk object below. Define a consumer with kuyruk.process(async (job) => { console.log("Job: " + job.data.action); }). Print "Consumer ready" with console.log.
Show example solution
const Bull = require("bull");
const kuyruk = new Bull("job-queue");
kuyruk.process(async (job) => {
console.log("Job: " + job.data.action);
});
console.log("Consumer ready");
Test yourself
-
What does queue.process do?
kuyruk.process((job) => { ... })
- Adds a message to the queue
- Takes and processes messages
- Closes Redis
- Creates a new queue
Show answer
Takes and processes messages — queue.process takes and processes messages.
-
Complete the job.data access
const user = job.____.user;
Show answer
const user = job.data.user;
-
Match each piece to its role
- queue.process
- Defines the consumer
- job.data
- Message data
- Promise resolve
- Job succeeded
- Promise reject
- Job failed
Next lesson: Retry and backoff