watchdog-timer
Detects and notifies when program does not check-in within a timeout.
Motivation
API
; /** * @property destroy Called when `reset` is not called within `timeout` interval. * @property reset Sets the timer's start time to the current time, and reschedules the timer to call its callback at the previously specified duration adjusted to the current time. */type WatchdogTimerType = | + void + void|; /** * @property consequentTimeouts Number of consequent timeouts. Calling `reset` resets `consequentTimeouts` to `0`. */type TimeoutEventType = | +consequentTimeouts: number|; /** * @property onTimeout Called when `reset` is not called within `timeout` interval. * @property timeout Timeout interval (in milliseconds). */type WatchdogTimerConfigurationInputType = | + void +timeout: number|; WatchdogTimerType;
Example usage
process.exit
Using watchdog-timer with A watchdog timeout is one of the rare, valid use cases for forced process termination, i.e. using process.exit()
.
; const main = async { const watchdogTimer = ; while true // Reset watchdog-timer on each loop. watchdogTimer; // `foo` is an arbitrary routine that might hang indefinitely, // e.g. due to a hanging database connection socket. await ; }; ;
Using watchdog-timer with Lightship
lightship
is an NPM module for signaling Kubernetes about the health of a Node.js application. In case of watchdog-timer, Lightship can be used to initiate a controlled termination of the Node.js process.
;; const main = async { const lightship = ; lightship; lightship; const watchdogTimer = ; while true if lightship console; break; // Reset watchdog-timer on each loop. watchdogTimer; // `foo` is an arbitrary routine that might hang indefinitely, // e.g. due to a hanging database connection socket. await ; watchdogTimer;}; ;