Bird
0
0

Why does the following code fail to restart a worker after it crashes?

medium📝 Debug Q7 of 15
Node.js - Cluster Module
Why does the following code fail to restart a worker after it crashes?
cluster.on('exit', (worker, code, signal) => {
  if (code !== 0) {
    worker.fork();
  }
});
AWorkers restart automatically; no need to call fork
BThe condition should check signal, not code
CThe exit event does not provide code or signal parameters
Dworker.fork() is not a valid method; should call cluster.fork()
Step-by-Step Solution
Solution:
  1. Step 1: Verify method to create new worker

    Only cluster.fork() creates new workers; worker.fork() does not exist.
  2. Step 2: Correct method usage

    To restart a worker, call cluster.fork() inside the exit event handler.
  3. Final Answer:

    worker.fork() is not a valid method; should call cluster.fork() -> Option D
  4. Quick Check:

    Use cluster.fork() to restart workers [OK]
Quick Trick: Call cluster.fork(), not worker.fork(), to restart workers [OK]
Common Mistakes:
  • Calling non-existent worker.fork()
  • Misunderstanding exit event parameters
  • Assuming automatic restart without code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes