Allow to pass config into worker, as it's used in the tests

This commit is contained in:
hroff-1902 2019-03-26 11:07:02 +03:00
parent c8b0c9af0a
commit 5161e1abb3
2 changed files with 20 additions and 12 deletions

View File

@ -34,7 +34,7 @@ class FreqtradeBot(object):
This is from here the bot start its logic. This is from here the bot start its logic.
""" """
def __init__(self, config: Dict[str, Any], worker: Worker) -> None: def __init__(self, config: Dict[str, Any], worker: Optional[Worker] = None) -> None:
""" """
Init all variables and objects the bot needs to work Init all variables and objects the bot needs to work
:param config: configuration dict, you can use Configuration.get_config() :param config: configuration dict, you can use Configuration.get_config()
@ -45,7 +45,7 @@ class FreqtradeBot(object):
# Init objects # Init objects
self.config = config self.config = config
self._worker: Worker = worker self._worker = worker
self.strategy: IStrategy = StrategyResolver(self.config).strategy self.strategy: IStrategy = StrategyResolver(self.config).strategy
@ -75,10 +75,14 @@ class FreqtradeBot(object):
@property @property
def state(self) -> State: def state(self) -> State:
if self._worker is None:
raise DependencyException("No Worker is available")
return self._worker.state return self._worker.state
@state.setter @state.setter
def state(self, value: State): def state(self, value: State):
if self._worker is None:
raise DependencyException("No Worker is available")
self._worker.state = value self._worker.state = value
def cleanup(self) -> None: def cleanup(self) -> None:

View File

@ -5,7 +5,7 @@ import logging
import time import time
import traceback import traceback
from argparse import Namespace from argparse import Namespace
from typing import Any, Callable from typing import Any, Callable, Optional
import sdnotify import sdnotify
from freqtrade import (constants, OperationalException, TemporaryError, from freqtrade import (constants, OperationalException, TemporaryError,
@ -23,24 +23,26 @@ class Worker(object):
Freqtradebot worker class Freqtradebot worker class
""" """
def __init__(self, args: Namespace) -> None: def __init__(self, args: Optional[Namespace] = None, config = None) -> None:
""" """
Init all variables and objects the bot needs to work Init all variables and objects the bot needs to work
""" """
logger.info('Starting worker %s', __version__) logger.info('Starting worker %s', __version__)
self._args = args self._args = args
self._init() self._config = config
self._init(False)
# Tell systemd that we completed initialization phase # Tell systemd that we completed initialization phase
if self._sd_notify: if self._sd_notify:
logger.debug("sd_notify: READY=1") logger.debug("sd_notify: READY=1")
self._sd_notify.notify("READY=1") self._sd_notify.notify("READY=1")
def _init(self): def _init(self, reconfig: bool):
""" """
Also called from the _reconfigure() method. Also called from the _reconfigure() method (with reconfig=True).
""" """
if reconfig or self._config is None:
# Load configuration # Load configuration
self._config = Configuration(self._args, None).get_config() self._config = Configuration(self._args, None).get_config()
@ -77,17 +79,19 @@ class Worker(object):
def run(self): def run(self):
state = None state = None
while True: while True:
state = self._worker(old_state=state, throttle_secs=self._throttle_secs) state = self._worker(old_state=state)
if state == State.RELOAD_CONF: if state == State.RELOAD_CONF:
self.freqtrade = self._reconfigure() self.freqtrade = self._reconfigure()
def _worker(self, old_state: State, throttle_secs: float) -> State: def _worker(self, old_state: State, throttle_secs: Optional[float] = None) -> State:
""" """
Trading routine that must be run at each loop Trading routine that must be run at each loop
:param old_state: the previous service state from the previous call :param old_state: the previous service state from the previous call
:return: current service state :return: current service state
""" """
state = self._state state = self._state
if throttle_secs is None:
throttle_secs = self._throttle_secs
# Log state transition # Log state transition
if state != old_state: if state != old_state:
@ -166,7 +170,7 @@ class Worker(object):
self.freqtrade.cleanup() self.freqtrade.cleanup()
# Load and validate config and create new instance of the bot # Load and validate config and create new instance of the bot
self._init() self._init(True)
self.freqtrade.rpc.send_msg({ self.freqtrade.rpc.send_msg({
'type': RPCMessageType.STATUS_NOTIFICATION, 'type': RPCMessageType.STATUS_NOTIFICATION,