Skip to content

Commit

Permalink
Move heartbeat to worker
Browse files Browse the repository at this point in the history
  • Loading branch information
hroff-1902 committed Feb 21, 2020
1 parent 881f602 commit 269a669
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
10 changes: 0 additions & 10 deletions freqtrade/freqtradebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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

Expand Down Expand Up @@ -52,10 +51,6 @@ def __init__(self, config: Dict[str, Any]) -> None:
# 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
Expand Down Expand Up @@ -159,11 +154,6 @@ def process(self) -> None:
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.
Expand Down
16 changes: 12 additions & 4 deletions freqtrade/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
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
Expand Down Expand Up @@ -33,6 +35,7 @@ def __init__(self, args: Dict[str, Any], config: Dict[str, Any] = None) -> None:
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:
Expand All @@ -50,10 +53,10 @@ def _init(self, reconfig: bool) -> None:
# 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
Expand Down Expand Up @@ -97,6 +100,11 @@ def _worker(self, old_state: Optional[State]) -> State:

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:
Expand Down

0 comments on commit 269a669

Please sign in to comment.