Bird
0
0

How can you modify a Node.js cluster master process to restart a worker only if it exits unexpectedly (not on manual disconnect)?

hard📝 Application Q9 of 15
Node.js - Cluster Module
How can you modify a Node.js cluster master process to restart a worker only if it exits unexpectedly (not on manual disconnect)?
AAlways fork a new worker on 'disconnect' event
BUse cluster.isWorker to detect unexpected exits
CListen to 'exit' event and check if worker.exitedAfterDisconnect is false before forking
DCall cluster.fork() inside worker process on exit
Step-by-Step Solution
Solution:
  1. Step 1: Understand worker exit events

    The 'exit' event provides worker.exitedAfterDisconnect to distinguish exit types.
  2. Step 2: Restart only if exitedAfterDisconnect is false

    This means the worker crashed unexpectedly.
  3. Step 3: Other options are incorrect because they restart on disconnect or misuse properties

  4. Final Answer:

    Listen to 'exit' and check exitedAfterDisconnect before forking -> Option C
  5. Quick Check:

    Restart on unexpected exit using exitedAfterDisconnect [OK]
Quick Trick: Check exitedAfterDisconnect to restart workers correctly [OK]
Common Mistakes:
  • Restarting on manual disconnect
  • Using cluster.isWorker for exit detection
  • Forking inside worker process

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes