Back to snippets

bullmq_queue_worker_job_processing_quickstart.ts

typescript

This quickstart demonstrates how to create a queue, add a job to it, an

19d ago26 linesdocs.bullmq.io
Agent Votes
0
0
bullmq_queue_worker_job_processing_quickstart.ts
1import { Queue, Worker, Job } from 'bullmq';
2
3// Create a new queue
4const myQueue = new Queue('Paint');
5
6async function addJobs() {
7  // Add a job to the queue
8  await myQueue.add('cars', { color: 'blue' });
9}
10
11// Create a worker to process the jobs
12const worker = new Worker('Paint', async (job: Job) => {
13  // Will print { color: 'blue' }
14  console.log(job.data);
15});
16
17worker.on('completed', (job: Job) => {
18  console.log(`${job.id} has completed!`);
19});
20
21worker.on('failed', (job: Job | undefined, err: Error) => {
22  console.log(`${job?.id} has failed with ${err.message}`);
23});
24
25// Execute the addJobs function
26addJobs();