From edc0d7f2c74d564b0468f27e9eb53e438284dd6a Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 15 Nov 2019 20:10:17 +0100 Subject: [PATCH 1/3] Fix non-terminating bot --- freqtrade/main.py | 3 --- freqtrade/utils.py | 11 +++++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/freqtrade/main.py b/freqtrade/main.py index 0a2adf71a..7afaeb1a2 100755 --- a/freqtrade/main.py +++ b/freqtrade/main.py @@ -27,7 +27,6 @@ def main(sysargv: List[str] = None) -> None: """ return_code: Any = 1 - worker = None try: arguments = Arguments(sysargv) args = arguments.get_parsed_arg() @@ -57,8 +56,6 @@ def main(sysargv: List[str] = None) -> None: except Exception: logger.exception('Fatal exception!') finally: - if worker: - worker.exit() sys.exit(return_code) diff --git a/freqtrade/utils.py b/freqtrade/utils.py index 5ad134ef9..ff54790a5 100644 --- a/freqtrade/utils.py +++ b/freqtrade/utils.py @@ -45,8 +45,15 @@ def start_trading(args: Dict[str, Any]) -> int: """ from freqtrade.worker import Worker # Load and run worker - worker = Worker(args) - worker.run() + try: + worker = Worker(args) + worker.run() + except KeyboardInterrupt: + logger.info('SIGINT received, aborting ...') + finally: + if worker: + logger.info("worker found ... calling exit") + worker.exit() return 0 From 6e0655b3b70adf5be4af47e8a508a8bde74b3002 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 16 Nov 2019 09:47:35 +0100 Subject: [PATCH 2/3] add empty worker variable --- freqtrade/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/freqtrade/utils.py b/freqtrade/utils.py index ff54790a5..b8ab7504e 100644 --- a/freqtrade/utils.py +++ b/freqtrade/utils.py @@ -45,6 +45,7 @@ def start_trading(args: Dict[str, Any]) -> int: """ from freqtrade.worker import Worker # Load and run worker + worker = None try: worker = Worker(args) worker.run() From 91047830fd282baa72a21f0425f7e904e92ddd33 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 16 Nov 2019 09:56:16 +0100 Subject: [PATCH 3/3] Add tst for worker termination --- tests/test_utils.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 3dba7df1b..bbb4fc648 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -8,7 +8,8 @@ from freqtrade import OperationalException from freqtrade.state import RunMode from freqtrade.utils import (setup_utils_configuration, start_create_userdir, start_download_data, start_list_exchanges, - start_list_markets, start_list_timeframes) + start_list_markets, start_list_timeframes, + start_trading) from tests.conftest import get_args, log_has, patch_exchange @@ -24,6 +25,29 @@ def test_setup_utils_configuration(): assert config['exchange']['secret'] == '' +def test_start_trading_fail(mocker): + + mocker.patch("freqtrade.worker.Worker.run", MagicMock(side_effect=OperationalException)) + + mocker.patch("freqtrade.worker.Worker.__init__", MagicMock(return_value=None)) + + exitmock = mocker.patch("freqtrade.worker.Worker.exit", MagicMock()) + args = [ + 'trade', + '-c', 'config.json.example' + ] + with pytest.raises(OperationalException): + start_trading(get_args(args)) + assert exitmock.call_count == 1 + + exitmock.reset_mock() + + mocker.patch("freqtrade.worker.Worker.__init__", MagicMock(side_effect=OperationalException)) + with pytest.raises(OperationalException): + start_trading(get_args(args)) + assert exitmock.call_count == 0 + + def test_list_exchanges(capsys): args = [