From 269a669af82b586e69d061ed5866b8c56af173d0 Mon Sep 17 00:00:00 2001 From: hroff-1902 Date: Fri, 21 Feb 2020 05:07:31 +0300 Subject: [PATCH] Move heartbeat to worker --- freqtrade/freqtradebot.py | 10 ---------- freqtrade/worker.py | 16 ++++++++++++---- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 127586437..00d5c369a 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -6,7 +6,6 @@ import logging import traceback from datetime import datetime from math import isclose -from os import getpid from threading import Lock from typing import Any, Dict, List, Optional, Tuple @@ -52,10 +51,6 @@ class FreqtradeBot: # Init objects self.config = config - self._heartbeat_msg = 0 - - self.heartbeat_interval = self.config.get('internals', {}).get('heartbeat_interval', 60) - self.strategy: IStrategy = StrategyResolver.load_strategy(self.config) # Check config consistency here since strategies can set certain options @@ -159,11 +154,6 @@ class FreqtradeBot: self.check_handle_timedout() Trade.session.flush() - if (self.heartbeat_interval - and (arrow.utcnow().timestamp - self._heartbeat_msg > self.heartbeat_interval)): - logger.info(f"Bot heartbeat. PID={getpid()}") - self._heartbeat_msg = arrow.utcnow().timestamp - def _refresh_whitelist(self, trades: List[Trade] = []) -> List[str]: """ Refresh whitelist from pairlist or edge and extend it with trades. diff --git a/freqtrade/worker.py b/freqtrade/worker.py index 088526d85..adce7ddda 100755 --- a/freqtrade/worker.py +++ b/freqtrade/worker.py @@ -4,8 +4,10 @@ Main Freqtrade worker class. import logging import time import traceback +from os import getpid from typing import Any, Callable, Dict, Optional +import arrow import sdnotify from freqtrade import __version__, constants @@ -33,6 +35,7 @@ class Worker: self._init(False) self.last_throttle_start_time: Optional[float] = None + self._heartbeat_msg = 0 # Tell systemd that we completed initialization phase if self._sd_notify: @@ -50,10 +53,10 @@ class Worker: # Init the instance of the bot self.freqtrade = FreqtradeBot(self._config) - self._throttle_secs = self._config.get('internals', {}).get( - 'process_throttle_secs', - constants.PROCESS_THROTTLE_SECS - ) + internals_config = self._config.get('internals', {}) + self._throttle_secs = internals_config.get('process_throttle_secs', + constants.PROCESS_THROTTLE_SECS) + self._heartbeat_interval = internals_config.get('heartbeat_interval', 60) self._sd_notify = sdnotify.SystemdNotifier() if \ self._config.get('internals', {}).get('sd_notify', False) else None @@ -97,6 +100,11 @@ class Worker: self._throttle(func=self._process_running, throttle_secs=self._throttle_secs) + if (self._heartbeat_interval + and (arrow.utcnow().timestamp - self._heartbeat_msg > self._heartbeat_interval)): + logger.info(f"Bot heartbeat. PID={getpid()}") + self._heartbeat_msg = arrow.utcnow().timestamp + return state def _throttle(self, func: Callable[..., Any], throttle_secs: float, *args, **kwargs) -> Any: