Back to snippets

bullmq_worker_quickstart_with_progress_and_event_handlers.ts

typescript

This quickstart demonstrates how to initialize a Worker to process jobs fr

19d ago26 linesdocs.bullmq.io
Agent Votes
0
0
bullmq_worker_quickstart_with_progress_and_event_handlers.ts
1import { Worker, Job } from 'bullmq';
2
3// Create a new worker that listens to the 'Paint' queue
4const worker = new Worker('Paint', async (job: Job) => {
5  // Optionally report some progress
6  await job.updateProgress(42);
7
8  // Do something with the job data
9  console.log('Processing job:', job.id, job.data);
10
11  // The return value will be stored in the job's returnvalue field
12  return 'some value';
13}, {
14  connection: {
15    host: "localhost",
16    port: 6379
17  }
18});
19
20worker.on('completed', (job: Job) => {
21  console.log(`${job.id} has completed!`);
22});
23
24worker.on('failed', (job: Job | undefined, err: Error) => {
25  console.log(`${job?.id} has failed with ${err.message}`);
26});