0
0
NestJSframework~10 mins

Queue events and monitoring in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Queue events and monitoring
Queue Initialized
Job Added to Queue
Event: Job Waiting
Monitor: Log Waiting
Event: Job Active
Monitor: Log Active
Event: Job Completed
Monitor: Log Success
Event: Job Failed
Monitor: Log Failure
Event: Job Stalled
Monitor: Log Stall
Repeat for next jobs or Shutdown
This flow shows how a queue processes jobs and emits events that can be monitored for status updates.
Execution Sample
NestJS
const queue = new Queue('myQueue');

queue.on('completed', job => console.log(`Job ${job.id} done`));
queue.add({ task: 'sendEmail' });
This code creates a queue, listens for job completion events, and adds a job to the queue.
Execution Table
StepEvent TriggeredJob IDAction TakenMonitor Output
1Job Added1Job added to queueNo output
2Job Waiting1Job is waiting to be processedLog: Job 1 is waiting
3Job Active1Job started processingLog: Job 1 is active
4Job Completed1Job finished successfullyLog: Job 1 done
5Job Added2Job added to queueNo output
6Job Waiting2Job is waiting to be processedLog: Job 2 is waiting
7Job Active2Job started processingLog: Job 2 is active
8Job Failed2Job failed during processingLog: Job 2 failed
9Job Added3Job added to queueNo output
10Job Waiting3Job is waiting to be processedLog: Job 3 is waiting
11Job Active3Job started processingLog: Job 3 is active
12Job Stalled3Job stalled during processingLog: Job 3 stalled
13Queue Shutdown-Queue stopped processingLog: Queue shutdown
💡 Queue shutdown after processing jobs and emitting all events.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 8After Step 12Final
queue.jobs[][{id:1, status:'waiting'}][{id:1, status:'completed'}][{id:1, status:'completed'}, {id:2, status:'failed'}][{id:1, status:'completed'}, {id:2, status:'failed'}, {id:3, status:'stalled'}][{id:1, status:'completed'}, {id:2, status:'failed'}, {id:3, status:'stalled'}]
job.idN/A1123N/A
job.statusN/AwaitingcompletedfailedstalledN/A
Key Moments - 3 Insights
Why do we see a 'Job Waiting' event before 'Job Active'?
The 'Job Waiting' event signals the job is queued but not yet processed. 'Job Active' means processing started. See execution_table rows 2 and 3.
What happens if a job fails? Does the queue stop?
No, the queue continues processing other jobs even if one fails. See execution_table rows 8 and 9 where job 2 fails but job 3 is still added.
What does the 'Job Stalled' event mean?
'Job Stalled' means the job was active but stopped responding temporarily. It's a warning to monitor. See execution_table row 12.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the status of job 1 at step 4?
Acompleted
Bactive
Cwaiting
Dfailed
💡 Hint
Check the 'Event Triggered' and 'Action Taken' columns at step 4.
At which step does job 2 fail?
AStep 6
BStep 7
CStep 8
DStep 9
💡 Hint
Look for 'Job Failed' event in the 'Event Triggered' column.
If we remove the listener for 'Job Stalled', what changes in the monitor output?
ANo log for stalled jobs
BNo logs at all
CNo logs for completed jobs
DQueue stops processing
💡 Hint
Refer to the 'Monitor Output' column for 'Job Stalled' event at step 12.
Concept Snapshot
Queue events in NestJS emit status updates like 'waiting', 'active', 'completed', 'failed', and 'stalled'.
Listeners capture these events to monitor job progress.
Add jobs with queue.add(), listen with queue.on(event).
Monitoring helps track job lifecycle and handle errors.
Queue continues processing despite failures.
Stalled jobs indicate temporary issues needing attention.
Full Transcript
In NestJS, queues emit events during job processing such as when a job is added, waiting, active, completed, failed, or stalled. We create a queue instance and add jobs to it. We listen for events like 'completed' to log or monitor job status. The execution table shows each step where events trigger and what actions happen. Variables track job IDs and statuses changing from waiting to completed or failed. Key moments clarify why waiting comes before active, that failures don't stop the queue, and what stalled means. The visual quiz tests understanding of job statuses at specific steps and effects of removing event listeners. This helps beginners see how queue events flow and how monitoring works in real time.