Skip to content

Commit

Permalink
Change keepalive to heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
xmatthias committed Oct 25, 2019
1 parent 0773a65 commit 2f1d969
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion config_full.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"forcebuy_enable": false,
"internals": {
"process_throttle_secs": 5,
"keep_alive_interval": 60
"heartbeat_interval": 60
},
"strategy": "DefaultStrategy",
"strategy_path": "user_data/strategies/"
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Mandatory parameters are marked as **Required**, which means that they are requi
| `strategy` | DefaultStrategy | Defines Strategy class to use.
| `strategy_path` | null | Adds an additional strategy lookup path (must be a directory).
| `internals.process_throttle_secs` | 5 | **Required.** Set the process throttle. Value in second.
| `internals.keep_alive_interval` | 60 | Print keepalive message every X seconds. Set to 0 to disable keepalive messages.
| `internals.heartbeat_interval` | 60 | Print heartbeat message every X seconds. Set to 0 to disable heartbeat messages.
| `internals.sd_notify` | false | Enables use of the sd_notify protocol to tell systemd service manager about changes in the bot state and issue keep-alive pings. See [here](installation.md#7-optional-configure-freqtrade-as-a-systemd-service) for more details.
| `logfile` | | Specify Logfile. Uses a rolling strategy of 10 files, with 1Mb per file.
| `user_data_dir` | cwd()/user_data | Directory containing user data. Defaults to `./user_data/`.
Expand Down
25 changes: 13 additions & 12 deletions freqtrade/freqtradebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@
import traceback
from datetime import datetime
from math import isclose
from os import getpid
from typing import Any, Dict, List, Optional, Tuple

import arrow
from requests.exceptions import RequestException

from freqtrade import (DependencyException, InvalidOrderException,
__version__, constants, persistence)
from freqtrade import (DependencyException, InvalidOrderException, __version__,
constants, persistence)
from freqtrade.configuration import validate_config_consistency
from freqtrade.data.converter import order_book_to_dataframe
from freqtrade.data.dataprovider import DataProvider
from freqtrade.edge import Edge
from freqtrade.configuration import validate_config_consistency
from freqtrade.exchange import timeframe_to_minutes, timeframe_to_next_date
from freqtrade.persistence import Trade
from freqtrade.resolvers import (ExchangeResolver, PairListResolver,
StrategyResolver)
from freqtrade.rpc import RPCManager, RPCMessageType
from freqtrade.resolvers import ExchangeResolver, StrategyResolver, PairListResolver
from freqtrade.state import State
from freqtrade.strategy.interface import SellType, IStrategy
from freqtrade.strategy.interface import IStrategy, SellType
from freqtrade.wallets import Wallets


logger = logging.getLogger(__name__)


Expand All @@ -50,9 +51,9 @@ def __init__(self, config: Dict[str, Any]) -> None:
# Init objects
self.config = config

self._last_alive_msg = 0
self._heartbeat_msg = 0

self.keep_alive_interval = self.config.get('internals', {}).get('keep_alive_interval', 60)
self.hearbeat_interval = self.config.get('internals', {}).get('heartbeat_interval', 60)

self.strategy: IStrategy = StrategyResolver(self.config).strategy

Expand Down Expand Up @@ -154,10 +155,10 @@ def process(self) -> None:
self.check_handle_timedout()
Trade.session.flush()

if (self.keep_alive_interval
and (arrow.utcnow().timestamp - self._last_alive_msg > self.keep_alive_interval)):
logger.info("I am alive.")
self._last_alive_msg = arrow.utcnow().timestamp
if (self.hearbeat_interval
and (arrow.utcnow().timestamp - self._heartbeat_msg > self.hearbeat_interval)):
logger.info(f"Freqtrade heartbeat. PID={getpid()}")
self._heartbeat_msg = arrow.utcnow().timestamp

def _extend_whitelist_with_trades(self, whitelist: List[str], trades: List[Any]):
"""
Expand Down
12 changes: 6 additions & 6 deletions tests/test_freqtradebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3656,19 +3656,19 @@ def test_process_i_am_alive(default_conf, mocker, caplog):
mocker.patch('freqtrade.exchange.Exchange.exchange_has', MagicMock(return_value=True))

ftbot = get_patched_freqtradebot(mocker, default_conf)
message = "I am alive."
message = r"Freqtrade heartbeat. PID=.*"
ftbot.process()
assert log_has(message, caplog)
assert ftbot._last_alive_msg != 0
assert log_has_re(message, caplog)
assert ftbot._heartbeat_msg != 0

caplog.clear()
# Message is not shown before interval is up
ftbot.process()
assert not log_has(message, caplog)
assert not log_has_re(message, caplog)

caplog.clear()
# Set clock - 70 seconds
ftbot._last_alive_msg -= 70
ftbot._heartbeat_msg -= 70

ftbot.process()
assert log_has(message, caplog)
assert log_has_re(message, caplog)

0 comments on commit 2f1d969

Please sign in to comment.