0
0
NestJSframework~10 mins

Named jobs in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Named jobs
Define Named Job
Add Job to Queue with Name
Worker Listens for Named Job
Job Executes
Job Completes or Fails
Result or Retry Handling
This flow shows how a named job is defined, added to a queue, processed by a worker, and then completed or retried.
Execution Sample
NestJS
import { Processor, Process } from '@nestjs/bull';
import { Job } from 'bull';

@Processor('email')
export class EmailProcessor {
  @Process('send-welcome')
  async handleSendWelcome(job: Job) {
    console.log(`Sending welcome email to ${job.data.email}`);
  }
}
This code defines a named job 'send-welcome' inside an 'email' queue and processes it by logging the email address.
Execution Table
StepActionJob NameJob DataWorker ResponseOutput
1Define processor and named jobsend-welcomeN/AReady to processNo output
2Add job to queuesend-welcome{"email":"user@example.com"}Job queuedNo output
3Worker picks jobsend-welcome{"email":"user@example.com"}Job startedNo output
4Execute job handlersend-welcome{"email":"user@example.com"}ProcessingLogs: Sending welcome email to user@example.com
5Job completessend-welcome{"email":"user@example.com"}Job doneNo output
💡 Job completes successfully after processing the named job 'send-welcome'.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
job.nameundefinedsend-welcomesend-welcomesend-welcomesend-welcome
job.data.emailundefineduser@example.comuser@example.comuser@example.comuser@example.com
worker.statusidleidleprocessingprocessingidle
Key Moments - 2 Insights
Why does the worker only process jobs with the specific name 'send-welcome'?
Because the @Process decorator specifies the job name 'send-welcome', the worker listens only for jobs with that name as shown in execution_table step 3.
What happens if a job with a different name is added to the queue?
The worker will ignore it since it only processes 'send-welcome' jobs, so no handler runs for other names, as implied by the variable_tracker showing worker.status stays idle for other names.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the job name when the worker starts processing?
Asend-welcome
Bemail
ChandleSendWelcome
Dundefined
💡 Hint
Check the 'Job Name' column at Step 3 in the execution_table.
At which step does the console log output appear?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Output' column in the execution_table for the log message.
If the job data email changed to 'admin@example.com', what would change in the variable_tracker?
Aworker.status would stay 'idle' throughout
Bjob.name would change to 'admin@example.com'
Cjob.data.email would show 'admin@example.com' after Step 2
DNo changes in variable_tracker
💡 Hint
Refer to the 'job.data.email' row in variable_tracker and imagine the new email value.
Concept Snapshot
Named jobs in NestJS Bull queues let you label jobs with a string name.
Use @Process('job-name') to handle specific jobs.
Add jobs with queue.add('job-name', data).
Workers listen only for their named jobs.
This helps organize and separate job logic clearly.
Full Transcript
In NestJS, named jobs allow you to assign a specific name to each job type in a queue. You define a processor class with @Processor('queue-name') and use @Process('job-name') to handle jobs with that name. When you add a job to the queue with queue.add('job-name', data), the worker listens for jobs matching that name and runs the handler. The execution flow starts with defining the job, adding it to the queue, the worker picking it up, executing the handler, and completing the job. Variables like job.name and job.data track the job's identity and data throughout. This approach helps keep job processing organized and clear.