Improve shutdown behavior

closes #7882
This commit is contained in:
Matthias 2022-12-12 20:01:54 +01:00
parent b328a18a97
commit abc3badfb5
2 changed files with 20 additions and 1 deletions

View File

@ -155,6 +155,8 @@ class FreqtradeBot(LoggingMixin):
self.cancel_all_open_orders() self.cancel_all_open_orders()
self.check_for_open_trades() self.check_for_open_trades()
except Exception as e:
logger.warning(f'Exception during cleanup: {e.__class__.__name__} {e}')
finally: finally:
self.strategy.ft_bot_cleanup() self.strategy.ft_bot_cleanup()
@ -162,8 +164,13 @@ class FreqtradeBot(LoggingMixin):
self.rpc.cleanup() self.rpc.cleanup()
if self.emc: if self.emc:
self.emc.shutdown() self.emc.shutdown()
Trade.commit()
self.exchange.close() self.exchange.close()
try:
Trade.commit()
except Exception:
# Exeptions here will be happening if the db disappeared.
# At which point we can no longer commit anyway.
pass
def startup(self) -> None: def startup(self) -> None:
""" """

View File

@ -88,6 +88,18 @@ def test_bot_cleanup(mocker, default_conf_usdt, caplog) -> None:
assert coo_mock.call_count == 1 assert coo_mock.call_count == 1
def test_bot_cleanup_db_errors(mocker, default_conf_usdt, caplog) -> None:
mocker.patch('freqtrade.freqtradebot.Trade.commit',
side_effect=OperationalException())
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.check_for_open_trades',
side_effect=OperationalException())
freqtrade = get_patched_freqtradebot(mocker, default_conf_usdt)
freqtrade.emc = MagicMock()
freqtrade.emc.shutdown = MagicMock()
freqtrade.cleanup()
assert freqtrade.emc.shutdown.call_count == 1
@pytest.mark.parametrize('runmode', [ @pytest.mark.parametrize('runmode', [
RunMode.DRY_RUN, RunMode.DRY_RUN,
RunMode.LIVE RunMode.LIVE