Node.js - Cluster ModuleHow 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' eventBUse cluster.isWorker to detect unexpected exitsCListen to 'exit' event and check if worker.exitedAfterDisconnect is false before forkingDCall cluster.fork() inside worker process on exitCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand worker exit eventsThe 'exit' event provides worker.exitedAfterDisconnect to distinguish exit types.Step 2: Restart only if exitedAfterDisconnect is falseThis means the worker crashed unexpectedly.Step 3: Other options are incorrect because they restart on disconnect or misuse propertiesFinal Answer:Listen to 'exit' and check exitedAfterDisconnect before forking -> Option CQuick Check:Restart on unexpected exit using exitedAfterDisconnect [OK]Quick Trick: Check exitedAfterDisconnect to restart workers correctly [OK]Common Mistakes:Restarting on manual disconnectUsing cluster.isWorker for exit detectionForking inside worker process
Master "Cluster Module" in Node.js9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More Node.js Quizzes Child Processes - fork for Node.js child processes - Quiz 9hard Child Processes - Why child processes are needed - Quiz 8hard Cluster Module - Forking workers per CPU core - Quiz 7medium Cluster Module - Forking workers per CPU core - Quiz 5medium HTTP Module - Request object properties - Quiz 10hard Timers and Scheduling - Event loop phases and timer execution - Quiz 3easy URL and Query String Handling - URLSearchParams for query strings - Quiz 12easy URL and Query String Handling - Building URLs programmatically - Quiz 9hard Worker Threads - SharedArrayBuffer for shared memory - Quiz 9hard Worker Threads - Passing data to workers - Quiz 2easy