main: don't touch freqbot state in cleanup()

cleanup() should be only called after the main loop has been exited.
At that point the state shouldn't be modified.
This commit is contained in:
gcarq 2018-06-09 01:19:42 +02:00
parent 66f6e71e7e
commit 74db82d759
4 changed files with 12 additions and 17 deletions

View File

@ -76,17 +76,14 @@ class FreqtradeBot(object):
else:
self.state = State.STOPPED
def clean(self) -> bool:
def cleanup(self) -> None:
"""
Cleanup the application state und finish all pending tasks
Cleanup pending resources on an already stopped bot
:return: None
"""
self.rpc.send_msg('*Status:* `Stopping trader...`')
logger.info('Stopping trader and cleaning up modules...')
self.state = State.STOPPED
logger.info('Cleaning up modules ...')
self.rpc.cleanup()
persistence.cleanup()
return True
def worker(self, old_state: State = None) -> State:
"""

View File

@ -55,7 +55,8 @@ def main(sysargv: List[str]) -> None:
logger.exception('Fatal exception!')
finally:
if freqtrade:
freqtrade.clean()
freqtrade.rpc.send_msg('*Status:* `Stopping trader...`')
freqtrade.cleanup()
sys.exit(return_code)

View File

@ -68,7 +68,7 @@ def test_freqtradebot_object() -> None:
Test the FreqtradeBot object has the mandatory public methods
"""
assert hasattr(FreqtradeBot, 'worker')
assert hasattr(FreqtradeBot, 'clean')
assert hasattr(FreqtradeBot, 'cleanup')
assert hasattr(FreqtradeBot, 'create_trade')
assert hasattr(FreqtradeBot, 'get_target_bid')
assert hasattr(FreqtradeBot, 'process_maybe_execute_buy')
@ -93,7 +93,7 @@ def test_freqtradebot(mocker, default_conf) -> None:
assert freqtrade.state is State.STOPPED
def test_clean(mocker, default_conf, caplog) -> None:
def test_cleanup(mocker, default_conf, caplog) -> None:
"""
Test clean() method
"""
@ -101,11 +101,8 @@ def test_clean(mocker, default_conf, caplog) -> None:
mocker.patch('freqtrade.persistence.cleanup', mock_cleanup)
freqtrade = get_patched_freqtradebot(mocker, default_conf)
assert freqtrade.state == State.RUNNING
assert freqtrade.clean()
assert freqtrade.state == State.STOPPED
assert log_has('Stopping trader and cleaning up modules...', caplog.record_tuples)
freqtrade.cleanup()
assert log_has('Cleaning up modules ...', caplog.record_tuples)
assert mock_cleanup.call_count == 1

View File

@ -70,7 +70,7 @@ def test_main_fatal_exception(mocker, default_conf, caplog) -> None:
'freqtrade.freqtradebot.FreqtradeBot',
_init_modules=MagicMock(),
worker=MagicMock(side_effect=Exception),
clean=MagicMock(),
cleanup=MagicMock(),
)
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
@ -97,7 +97,7 @@ def test_main_keyboard_interrupt(mocker, default_conf, caplog) -> None:
'freqtrade.freqtradebot.FreqtradeBot',
_init_modules=MagicMock(),
worker=MagicMock(side_effect=KeyboardInterrupt),
clean=MagicMock(),
cleanup=MagicMock(),
)
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
@ -124,7 +124,7 @@ def test_main_operational_exception(mocker, default_conf, caplog) -> None:
'freqtrade.freqtradebot.FreqtradeBot',
_init_modules=MagicMock(),
worker=MagicMock(side_effect=OperationalException('Oh snap!')),
clean=MagicMock(),
cleanup=MagicMock(),
)
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',