systemd_watchdog
sd_notify(3) and sd_watchdog_enabled(3) client functionality implemented in Python 3 for writing Python daemons
Install
$ pip install systemd-watchdog
or
$ git clone ...
$ make install
Usage
import systemd_watchdog
wd = systemd_watchdog.watchdog()
if not wd.enabled():
raise Exception("Watchdog not enabled")
wd.status("Starting my service...")
time.sleep(3)
wd.ready()
wd.status("Waiting for web requests...")
wd.notify()
time.sleep(3)
timeout_half_sec = int(float(wd.timeout) / 2e6)
time.sleep(timeout_half_sec)
wd.notify()
wd.notify_error("An irrecoverable error occured!")
time.sleep(3)
Public Interface
systemd_watchdog.watchdog
- commonly used properties and methods
ping
The only method required for the simplest implementation; combines notify_due
with notify()
to only send "alive" notifications at reasonable intervals.
Returns boolean indicating if a message was sent or not.
beat
Alias for ping()
if you prefer heartbeat terminology.
ready()
Report ready service state, i.e. completed init (only needed with Type=notify
).
status(msg)
Send a service status message.
systemd_watchdog.watchdog
- less-used properties and methods
is_enabled
Boolean property stating whether watchdog capability is enabled.
timeout
Property reporting the number of microseconds (int) before process will be killed.
It is recommended that you call notify()
once roughly half of this interval has passed (see notify_due
).
timeout_td
Property that is the same as timeout
but presented as datetime.timedelta
for easier manipulation.
notify_due
Boolean property indicating more than half of the watchdog interval has passed since last update.
notify()
Report a healthy service state. Other calls, e.g. status()
do not reset the watchdog.
notify_error(msg=None)
Report an error to the watchdog manager. This program will likely be killed upon receipt.
If msg
is provided, it will be reported as a status message prior to the error.
History
Aaron D. Marasco May 2020
License
See LICENSE
file