Bird
0
0

In a Node.js cluster, how do you correctly attach a listener to detect when a worker process exits?

easy📝 Syntax Q3 of 15
Node.js - Cluster Module
In a Node.js cluster, how do you correctly attach a listener to detect when a worker process exits?
Acluster.on('exit', (worker, code, signal) => { /* handler */ });
Bcluster.worker.on('exit', (code, signal) => { /* handler */ });
Cworker.on('exit', (code, signal) => { /* handler */ });
Dprocess.on('exit', (worker) => { /* handler */ });
Step-by-Step Solution
Solution:
  1. Step 1: Identify the event emitter

    The 'exit' event for a worker is emitted on the worker object itself, not the cluster or process.
  2. Step 2: Attach listener to the worker

    Use worker.on('exit', callback) to listen for the exit event.
  3. Final Answer:

    worker.on('exit', (code, signal) => { /* handler */ }); corresponds to worker.on('exit', (code, signal) => { /* handler */ });.
  4. Quick Check:

    Listener must be on the worker instance, not cluster or process. [OK]
Quick Trick: Listen to 'exit' on worker instance, not cluster or process [OK]
Common Mistakes:
  • Attaching 'exit' listener on cluster instead of worker
  • Using process.on('exit') to detect worker exit
  • Referencing cluster.worker instead of the worker instance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes