stable/freqtrade/worker.py

183 lines
6.4 KiB
Python
Raw Normal View History

2019-03-25 14:45:03 +00:00
"""
Main Freqtrade worker class.
"""
import logging
import time
import traceback
2020-02-21 02:07:31 +00:00
from os import getpid
from typing import Any, Callable, Dict, Optional
2019-03-25 14:45:03 +00:00
import sdnotify
from freqtrade import __version__, constants
2019-03-25 14:45:03 +00:00
from freqtrade.configuration import Configuration
from freqtrade.exceptions import OperationalException, TemporaryError
from freqtrade.freqtradebot import FreqtradeBot
from freqtrade.state import State
2019-03-25 14:45:03 +00:00
logger = logging.getLogger(__name__)
2019-09-12 01:39:52 +00:00
class Worker:
2019-03-25 14:45:03 +00:00
"""
Freqtradebot worker class
"""
2020-02-02 04:00:40 +00:00
def __init__(self, args: Dict[str, Any], config: Dict[str, Any] = None) -> None:
2019-03-25 14:45:03 +00:00
"""
Init all variables and objects the bot needs to work
"""
2020-02-20 05:19:22 +00:00
logger.info(f"Starting worker {__version__}")
2019-03-25 14:45:03 +00:00
self._args = args
self._config = config
self._init(False)
2019-03-25 14:45:03 +00:00
2020-02-21 02:31:21 +00:00
self.last_throttle_start_time: float = 0
self._heartbeat_msg: float = 0
2019-03-25 14:45:03 +00:00
# Tell systemd that we completed initialization phase
2020-05-18 05:02:57 +00:00
self._notify("READY=1")
2019-03-25 14:45:03 +00:00
2019-04-30 16:47:55 +00:00
def _init(self, reconfig: bool) -> None:
2019-03-25 14:45:03 +00:00
"""
Also called from the _reconfigure() method (with reconfig=True).
2019-03-25 14:45:03 +00:00
"""
if reconfig or self._config is None:
# Load configuration
self._config = Configuration(self._args, None).get_config()
2019-03-25 14:45:03 +00:00
# Init the instance of the bot
self.freqtrade = FreqtradeBot(self._config)
2019-03-25 14:45:03 +00:00
2020-02-21 02:07:31 +00:00
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)
2019-03-25 14:45:03 +00:00
self._sd_notify = sdnotify.SystemdNotifier() if \
self._config.get('internals', {}).get('sd_notify', False) else None
2020-05-18 05:02:57 +00:00
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)
2019-04-30 16:47:55 +00:00
def run(self) -> None:
2019-03-25 14:45:03 +00:00
state = None
while True:
state = self._worker(old_state=state)
if state == State.RELOAD_CONFIG:
2019-04-30 07:29:49 +00:00
self._reconfigure()
2019-03-25 14:45:03 +00:00
2020-02-21 01:00:23 +00:00
def _worker(self, old_state: Optional[State]) -> State:
2019-03-25 14:45:03 +00:00
"""
2020-02-23 19:50:58 +00:00
The main routine that runs each throttling iteration and handles the states.
2019-03-25 14:45:03 +00:00
:param old_state: the previous service state from the previous call
:return: current service state
"""
state = self.freqtrade.state
2019-03-25 14:45:03 +00:00
# Log state transition
if state != old_state:
2020-01-27 00:34:53 +00:00
self.freqtrade.notify_status(f'{state.name.lower()}')
2020-02-20 05:19:22 +00:00
logger.info(f"Changing state to: {state.name}")
2019-03-25 14:45:03 +00:00
if state == State.RUNNING:
2019-05-19 18:06:26 +00:00
self.freqtrade.startup()
2019-03-25 14:45:03 +00:00
if state == State.STOPPED:
self.freqtrade.check_for_open_trades()
# Reset heartbeat timestamp to log the heartbeat message at
# first throttling iteration when the state changes
self._heartbeat_msg = 0
2019-03-25 14:45:03 +00:00
if state == State.STOPPED:
# Ping systemd watchdog before sleeping in the stopped state
2020-05-18 05:02:57 +00:00
self._notify("WATCHDOG=1\nSTATUS=State: STOPPED.")
2019-03-25 14:45:03 +00:00
2020-02-21 01:00:23 +00:00
self._throttle(func=self._process_stopped, throttle_secs=self._throttle_secs)
2019-03-25 14:45:03 +00:00
elif state == State.RUNNING:
# Ping systemd watchdog before throttling
2020-05-18 05:02:57 +00:00
self._notify("WATCHDOG=1\nSTATUS=State: RUNNING.")
2019-03-25 14:45:03 +00:00
2020-02-21 01:00:23 +00:00
self._throttle(func=self._process_running, throttle_secs=self._throttle_secs)
2019-03-25 14:45:03 +00:00
2020-02-21 02:31:21 +00:00
if self._heartbeat_interval:
now = time.time()
if (now - self._heartbeat_msg) > self._heartbeat_interval:
logger.info(f"Bot heartbeat. PID={getpid()}, "
f"version='{__version__}', state='{state.name}'")
2020-02-21 02:31:21 +00:00
self._heartbeat_msg = now
2020-02-21 02:07:31 +00:00
2019-03-25 14:45:03 +00:00
return state
2020-02-21 01:00:23 +00:00
def _throttle(self, func: Callable[..., Any], throttle_secs: float, *args, **kwargs) -> Any:
2019-03-25 14:45:03 +00:00
"""
Throttles the given callable that it
takes at least `min_secs` to finish execution.
:param func: Any callable
2020-02-21 01:00:23 +00:00
:param throttle_secs: throttling interation execution time limit in seconds
:return: Any (result of execution of func)
2019-03-25 14:45:03 +00:00
"""
self.last_throttle_start_time = time.time()
2020-02-20 05:17:24 +00:00
logger.debug("========================================")
2020-02-21 00:37:38 +00:00
result = func(*args, **kwargs)
time_passed = time.time() - self.last_throttle_start_time
2020-02-21 01:00:23 +00:00
sleep_duration = max(throttle_secs - time_passed, 0.0)
2020-02-21 00:37:38 +00:00
logger.debug(f"Throttling with '{func.__name__}()': sleep for {sleep_duration:.2f} s, "
f"last iteration took {time_passed:.2f} s.")
time.sleep(sleep_duration)
2019-03-25 14:45:03 +00:00
return result
def _process_stopped(self) -> None:
self.freqtrade.process_stopped()
def _process_running(self) -> None:
2019-03-25 14:45:03 +00:00
try:
2019-08-13 07:36:52 +00:00
self.freqtrade.process()
2019-03-25 14:45:03 +00:00
except TemporaryError as error:
logger.warning(f"Error: {error}, retrying in {constants.RETRY_TIMEOUT} seconds...")
time.sleep(constants.RETRY_TIMEOUT)
except OperationalException:
tb = traceback.format_exc()
hint = 'Issue `/start` if you think it is safe to restart.'
2020-01-27 00:34:53 +00:00
self.freqtrade.notify_status(f'OperationalException:\n```\n{tb}```{hint}')
2019-03-25 14:45:03 +00:00
logger.exception('OperationalException. Stopping trader ...')
self.freqtrade.state = State.STOPPED
2019-03-25 14:45:03 +00:00
2019-04-30 16:47:55 +00:00
def _reconfigure(self) -> None:
2019-03-25 14:45:03 +00:00
"""
Cleans up current freqtradebot instance, reloads the configuration and
2019-03-30 20:33:52 +00:00
replaces it with the new instance
2019-03-25 14:45:03 +00:00
"""
# Tell systemd that we initiated reconfiguration
2020-05-18 05:02:57 +00:00
self._notify("RELOADING=1")
2019-03-25 14:45:03 +00:00
# Clean up current freqtrade modules
self.freqtrade.cleanup()
# Load and validate config and create new instance of the bot
self._init(True)
2019-03-25 14:45:03 +00:00
2020-01-27 00:34:53 +00:00
self.freqtrade.notify_status('config reloaded')
2019-03-25 14:45:03 +00:00
# Tell systemd that we completed reconfiguration
2020-05-18 05:02:57 +00:00
self._notify("READY=1")
2019-03-25 14:45:03 +00:00
2019-04-30 16:47:55 +00:00
def exit(self) -> None:
2019-03-25 14:45:03 +00:00
# Tell systemd that we are exiting now
2020-05-18 05:02:57 +00:00
self._notify("STOPPING=1")
2019-03-25 14:45:03 +00:00
if self.freqtrade:
2020-01-27 00:34:53 +00:00
self.freqtrade.notify_status('process died')
2019-03-25 14:45:03 +00:00
self.freqtrade.cleanup()