I’m wondering if it is possible to restart an AppDaemon app from within an AppDaemon app.
I’m using an AppDaemon python script (running on a Synology Nas) communicating via modbus to 14 Arduino’s. This works fine but once in a while (~once per 3 to 7 days) I loose communication to one of the Arduino’s. Everytime a different one so difficult to pinpoint the rootcause.
Restarting AppDaemon solves the problem. I would like to automatically restart the AppDaemon app (or AppDaemon completely) to prevent a manual restart of AppDaemon. Is this possible? I couldn’t find it by googling…
I don’t know about how the Synology works, but with Docker you can say, “restart: always”. If you can do that with your NAS, maybe you could try sys.exit() or try signaling appdaemon - os.kill(<pid>, signal.SIGTERM). Though you’d have to figure out the pid for the main process. Just a thought.
The method you want is restart_app e.g. - self.restart_app("lights_app")
You would also need to know when to restart the app. If an error or message is being reported in a log you can add a watchdog app that listens to that particular log and receives a callback when something is written to it.
So you might do something like:
class watchdog(hass.Hass):
def initialize(self):
self.listen_log(self.restart_my_app, log="error_log")
def restart_my_app(self, app_name, ts, level, log_type, message, kwargs):
# check the message here and if appropriate
self.restart_app("my_app")
Just an FYI - the appdaemon documentation (which is searchable) is here - https://appdaemon.readthedocs.io/en/latest/. You can search for “restart_app” and “listen_log” to get more details on the calls.