Use digidevice.appmonitor to monitor your application
The appmonitor Python module allows you to monitor the health of your Python application and take action on failure.
The application monitor daemon acts as a system service, allowing external applications to register with the daemon , and the daemon will take a specific action when a registered application does not refresh the daemon within the refresh timeout. The daemon can be configured to:
-
Terminate the application.
-
Restart the application by executing the provided command.
-
Reboot the system.
Application monitor workflow
-
The application registers with the application monitor daemon. In this step, the application must provide:
-
The refresh timeout.
-
The action to take in case of failure.
-
The restart command to execute in the case the application will be restarted.
-
-
The daemon stores the registered application information and begins monitoring the application refresh time.
-
The application periodically sends a refresh request to the daemon. The time between refresh requests should be lower than the configured refresh timeout.
-
If the application does not send a refresh request during the configured timeout, the daemon executes the action specified when the application was registered.
The application can stop the monitoring process at any time by sending an unregister request to the daemon.
digidevice.appmonitor API
This API provides all the required methods and objects to access and interact with the application monitor daemon.
Register the application
The first step to start using the application monitor daemon is to register the application in it. To do so, the appmonitor module provides the register_app method:
Method | register_app(timeout, action, restart_command) |
Description | Registers a new application to the application monitor daemon with the provided data. |
Parameters |
|
Returns |
An AppMonitorHandler object to use to refresh or unregister the application from the Application Monitor daemon. |
Raises |
|
Usage example:
from digidevice import appmonitor from digidevice.appmonitor import AppMonitorAction ... mon_handle = appmonitor.register_app(5000, AppMonitorAction.KILL) #mon_handle = appmonitor.register_app(5000, AppMonitorAction.REBOOT) #mon_handle = appmonitor.register_app(5000, AppMonitorAction.RESTART, "<restart_command>")
Refresh the application
After the application has been registered, it must start sending refresh requests to the application monitor daemon, using the refresh() method of the received application monitor handler. The time between the refresh calls should not be longer than the timeout specified in the registration process; otherwise, the daemon will execute the registered action.
Method | refresh() |
Description | Refreshes the application in the application monitor daemon. |
Parameters | None. |
Returns |
None. |
Raises |
AppMonitorError if there is an error refreshing the application. |
Usage example:
import time ... while True: mon_handle.refresh() time.sleep(1)
Unregister the application
The application can be unregistered from the application monitor daemon at any time by calling the unregister function of the the received application monitor handler.
Method | unregister() |
Description | Unregisters the application from the application monitor daemon. |
Parameters | None. |
Returns |
None. |
Raises |
AppMonitorError if there is an error unregistering the application. |
Usage example:
... mon_handle.unregister()