Merge pull request #3323 from freqtrade/simplify_sdnotify

[minor] Simplify sd_notify usage
This commit is contained in:
hroff-1902 2020-05-18 11:29:18 +03:00 committed by GitHub
commit 18e38a3fe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,9 +37,7 @@ class Worker:
self._heartbeat_msg: float = 0 self._heartbeat_msg: float = 0
# Tell systemd that we completed initialization phase # Tell systemd that we completed initialization phase
if self._sd_notify: self._notify("READY=1")
logger.debug("sd_notify: READY=1")
self._sd_notify.notify("READY=1")
def _init(self, reconfig: bool) -> None: def _init(self, reconfig: bool) -> None:
""" """
@ -60,6 +58,15 @@ class Worker:
self._sd_notify = sdnotify.SystemdNotifier() if \ self._sd_notify = sdnotify.SystemdNotifier() if \
self._config.get('internals', {}).get('sd_notify', False) else None self._config.get('internals', {}).get('sd_notify', False) else None
def _notify(self, message: str) -> None:
"""
Removes the need to verify in all occurances if sd_notify is enabled
:param message: Message to send to systemd if it's enabled.
"""
if self._sd_notify:
logger.debug(f"sd_notify: {message}")
self._sd_notify.notify(message)
def run(self) -> None: def run(self) -> None:
state = None state = None
while True: while True:
@ -89,17 +96,13 @@ class Worker:
if state == State.STOPPED: if state == State.STOPPED:
# Ping systemd watchdog before sleeping in the stopped state # Ping systemd watchdog before sleeping in the stopped state
if self._sd_notify: self._notify("WATCHDOG=1\nSTATUS=State: STOPPED.")
logger.debug("sd_notify: WATCHDOG=1\\nSTATUS=State: STOPPED.")
self._sd_notify.notify("WATCHDOG=1\nSTATUS=State: STOPPED.")
self._throttle(func=self._process_stopped, throttle_secs=self._throttle_secs) self._throttle(func=self._process_stopped, throttle_secs=self._throttle_secs)
elif state == State.RUNNING: elif state == State.RUNNING:
# Ping systemd watchdog before throttling # Ping systemd watchdog before throttling
if self._sd_notify: self._notify("WATCHDOG=1\nSTATUS=State: RUNNING.")
logger.debug("sd_notify: WATCHDOG=1\\nSTATUS=State: RUNNING.")
self._sd_notify.notify("WATCHDOG=1\nSTATUS=State: RUNNING.")
self._throttle(func=self._process_running, throttle_secs=self._throttle_secs) self._throttle(func=self._process_running, throttle_secs=self._throttle_secs)
@ -154,9 +157,7 @@ class Worker:
replaces it with the new instance replaces it with the new instance
""" """
# Tell systemd that we initiated reconfiguration # Tell systemd that we initiated reconfiguration
if self._sd_notify: self._notify("RELOADING=1")
logger.debug("sd_notify: RELOADING=1")
self._sd_notify.notify("RELOADING=1")
# Clean up current freqtrade modules # Clean up current freqtrade modules
self.freqtrade.cleanup() self.freqtrade.cleanup()
@ -167,15 +168,11 @@ class Worker:
self.freqtrade.notify_status('config reloaded') self.freqtrade.notify_status('config reloaded')
# Tell systemd that we completed reconfiguration # Tell systemd that we completed reconfiguration
if self._sd_notify: self._notify("READY=1")
logger.debug("sd_notify: READY=1")
self._sd_notify.notify("READY=1")
def exit(self) -> None: def exit(self) -> None:
# Tell systemd that we are exiting now # Tell systemd that we are exiting now
if self._sd_notify: self._notify("STOPPING=1")
logger.debug("sd_notify: STOPPING=1")
self._sd_notify.notify("STOPPING=1")
if self.freqtrade: if self.freqtrade:
self.freqtrade.notify_status('process died') self.freqtrade.notify_status('process died')