Enhance Keyboard interrupt handling for dl-trades (stores data it already downloaded).

This commit is contained in:
Matthias
2023-08-20 11:57:59 +02:00
parent 4fefae6f07
commit b71a44f27c
+37 -25
View File
@@ -5,6 +5,7 @@ Cryptocurrency Exchanges support
import asyncio import asyncio
import inspect import inspect
import logging import logging
import signal
from copy import deepcopy from copy import deepcopy
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from math import floor from math import floor
@@ -2253,20 +2254,24 @@ class Exchange:
from_id = t[-1][1] from_id = t[-1][1]
trades.extend(t[:-1]) trades.extend(t[:-1])
while True: while True:
t = await self._async_fetch_trades(pair, try:
params={self._trades_pagination_arg: from_id}) t = await self._async_fetch_trades(pair,
if t: params={self._trades_pagination_arg: from_id})
# Skip last id since its the key for the next call if t:
trades.extend(t[:-1]) # Skip last id since its the key for the next call
if from_id == t[-1][1] or t[-1][0] > until: trades.extend(t[:-1])
logger.debug(f"Stopping because from_id did not change. " if from_id == t[-1][1] or t[-1][0] > until:
f"Reached {t[-1][0]} > {until}") logger.debug(f"Stopping because from_id did not change. "
# Reached the end of the defined-download period - add last trade as well. f"Reached {t[-1][0]} > {until}")
trades.extend(t[-1:]) # Reached the end of the defined-download period - add last trade as well.
break trades.extend(t[-1:])
break
from_id = t[-1][1] from_id = t[-1][1]
else: else:
break
except asyncio.CancelledError:
logger.debug("Async operation Interrupted, breaking trades DL loop.")
break break
return (pair, trades) return (pair, trades)
@@ -2286,16 +2291,20 @@ class Exchange:
# DEFAULT_TRADES_COLUMNS: 0 -> timestamp # DEFAULT_TRADES_COLUMNS: 0 -> timestamp
# DEFAULT_TRADES_COLUMNS: 1 -> id # DEFAULT_TRADES_COLUMNS: 1 -> id
while True: while True:
t = await self._async_fetch_trades(pair, since=since) try:
if t: t = await self._async_fetch_trades(pair, since=since)
since = t[-1][0] if t:
trades.extend(t) since = t[-1][0]
# Reached the end of the defined-download period trades.extend(t)
if until and t[-1][0] > until: # Reached the end of the defined-download period
logger.debug( if until and t[-1][0] > until:
f"Stopping because until was reached. {t[-1][0]} > {until}") logger.debug(
f"Stopping because until was reached. {t[-1][0]} > {until}")
break
else:
break break
else: except asyncio.CancelledError:
logger.debug("Async operation Interrupted, breaking trades DL loop.")
break break
return (pair, trades) return (pair, trades)
@@ -2344,9 +2353,12 @@ class Exchange:
raise OperationalException("This exchange does not support downloading Trades.") raise OperationalException("This exchange does not support downloading Trades.")
with self._loop_lock: with self._loop_lock:
return self.loop.run_until_complete( task = asyncio.ensure_future(self._async_get_trade_history(
self._async_get_trade_history(pair=pair, since=since, pair=pair, since=since, until=until, from_id=from_id))
until=until, from_id=from_id))
for sig in [signal.SIGINT, signal.SIGTERM]:
self.loop.add_signal_handler(sig, task.cancel)
return self.loop.run_until_complete(task)
@retrier @retrier
def _get_funding_fees_from_exchange(self, pair: str, since: Union[datetime, int]) -> float: def _get_funding_fees_from_exchange(self, pair: str, since: Union[datetime, int]) -> float: