Add notify_status() to FreqtradeBot

This commit is contained in:
hroff-1902 2020-01-27 03:34:53 +03:00
parent 27d46ed06f
commit 30e3e434ab
2 changed files with 17 additions and 17 deletions

View File

@ -95,6 +95,16 @@ class FreqtradeBot:
# Protect sell-logic from forcesell and viceversa # Protect sell-logic from forcesell and viceversa
self._sell_lock = Lock() self._sell_lock = Lock()
def notify_status(self, msg: str) -> None:
"""
Public method for users of this class (worker, etc.) to send notifications
via RPC about changes in the bot status.
"""
self.rpc.send_msg({
'type': RPCMessageType.STATUS_NOTIFICATION,
'status': msg
})
def cleanup(self) -> None: def cleanup(self) -> None:
""" """
Cleanup pending resources on an already stopped bot Cleanup pending resources on an already stopped bot

View File

@ -12,7 +12,6 @@ from freqtrade import __version__, constants
from freqtrade.configuration import Configuration from freqtrade.configuration import Configuration
from freqtrade.exceptions import OperationalException, TemporaryError from freqtrade.exceptions import OperationalException, TemporaryError
from freqtrade.freqtradebot import FreqtradeBot from freqtrade.freqtradebot import FreqtradeBot
from freqtrade.rpc import RPCMessageType
from freqtrade.state import State from freqtrade.state import State
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -84,10 +83,8 @@ class Worker:
# Log state transition # Log state transition
if state != old_state: if state != old_state:
self.freqtrade.rpc.send_msg({ self.freqtrade.notify_status(f'{state.name.lower()}')
'type': RPCMessageType.STATUS_NOTIFICATION,
'status': f'{state.name.lower()}'
})
logger.info('Changing state to: %s', state.name) logger.info('Changing state to: %s', state.name)
if state == State.RUNNING: if state == State.RUNNING:
self.freqtrade.startup() self.freqtrade.startup()
@ -136,10 +133,9 @@ class Worker:
except OperationalException: except OperationalException:
tb = traceback.format_exc() tb = traceback.format_exc()
hint = 'Issue `/start` if you think it is safe to restart.' hint = 'Issue `/start` if you think it is safe to restart.'
self.freqtrade.rpc.send_msg({
'type': RPCMessageType.STATUS_NOTIFICATION, self.freqtrade.notify_status(f'OperationalException:\n```\n{tb}```{hint}')
'status': f'OperationalException:\n```\n{tb}```{hint}'
})
logger.exception('OperationalException. Stopping trader ...') logger.exception('OperationalException. Stopping trader ...')
self.freqtrade.state = State.STOPPED self.freqtrade.state = State.STOPPED
@ -159,10 +155,7 @@ class Worker:
# Load and validate config and create new instance of the bot # Load and validate config and create new instance of the bot
self._init(True) self._init(True)
self.freqtrade.rpc.send_msg({ self.freqtrade.notify_status('config reloaded')
'type': RPCMessageType.STATUS_NOTIFICATION,
'status': 'config reloaded'
})
# Tell systemd that we completed reconfiguration # Tell systemd that we completed reconfiguration
if self._sd_notify: if self._sd_notify:
@ -176,8 +169,5 @@ class Worker:
self._sd_notify.notify("STOPPING=1") self._sd_notify.notify("STOPPING=1")
if self.freqtrade: if self.freqtrade:
self.freqtrade.rpc.send_msg({ self.freqtrade.notify_status('process died')
'type': RPCMessageType.STATUS_NOTIFICATION,
'status': 'process died'
})
self.freqtrade.cleanup() self.freqtrade.cleanup()