5. You want to ensure your Node.js app automatically restarts a worker if it crashes, but only up to 3 restarts per minute to avoid infinite loops. Which approach best implements this behavior?
hard
A. Use a counter and timestamp in the master process to track restarts; restart only if under limit
B. Restart workers immediately on every exit event without limits
C. Use a setTimeout to delay restarts by 1 minute after each crash
D. Restart workers only if exit code is 0, ignore other exit codes
Solution
Step 1: Understand the need to limit restarts
Unlimited restarts can cause infinite loops if the worker crashes repeatedly.
Step 2: Implement a counter and timestamp logic
Track how many times workers restart within a time window (e.g., 3 restarts per minute) and only restart if under the limit.
Final Answer:
Use a counter and timestamp in the master process to track restarts; restart only if under limit -> Option A
Quick Check:
Limit restarts with counter and time check [OK]
Hint: Count restarts with time checks to avoid infinite loops [OK]