Depending on the application, something going wrong and stopping it from running could lead to big complications. Thankfully, a watchdog can monitor your application and react how you want it to using the Watchdog class. The watchdog can stop, restart, and report on applications not responding to it and more. Watchdogs have configurable timeframes, and when they aren’t updated before the time frame expires they trigger.
The watchdog is a very powerful concept. Wikipedia says:
A watchdog timer (sometimes called a computer operating properly or COP timer, or simply a watchdog) is an electronic timer that is used to detect and recover from computer malfunctions. During normal operation, the computer regularly resets the watchdog timer to prevent it from elapsing, or “timing out”. If, due to a hardware fault or program error, the computer fails to reset the watchdog, the timer will elapse and generate a timeout signal. The timeout signal is used to initiate corrective action or actions. The corrective actions typically include placing the computer system in a safe state and restoring normal system operation.
The example below is very basic. You will need to adjust the duration and location of the watchdog for your application.
View on GitHub
This is what we see in the log after the unit has come back up.
01/15/19 08:37:36.361, FTP/10.0.0.27:62888 uploaded /flash/WatchdogExample.jar [115.3 kbps]
01/15/19 08:37:44.081, WatchdogExample started
01/15/19 08:37:44.203, WatchdogExample activated
01/15/19 08:38:14.307, WatchdogExample loop finished
01/15/19 08:38:18.451, ** Assertion: WatchdogExample watchdog triggered reboot (Line 1278)
01/15/19 08:38:18.483, ** Terminating: System
01/15/19 08:38:20.971, ** Reboot on assertion: WatchdogExample watchdog triggered reboot (Line 1278)
01/15/19 08:38:20.997, -- JANOS 410 v1.7.1 initialized (POR: 1546)
Here are the options that are available to choose from when the watchdog expires. You see that we assign the action in the setAction() method above.
This is the default action. Expiration of the watchdog timer causes the unit to reboot.
This action indicates that a watchdog timer expiration is to be handled by the application itself. The system takes no action.
When the watchdog timer expires the system will terminate the application.
This interrupts the current application (similar to Ctrl-C interruption) when the watchdog timer expires.
When the watchdog timer expires this will interrupt the current application and restart it.
When the watchdog timer expires a
WM_WATCHDOG (0x11) message will be sent through the system message pump. The content contains the text associated with the watchdog.
Watchdog timer expiration will notify the first thread in the application that is waiting for notification using