From dfccc6e3b1ecde5b6a48c50c5ca72ca02628d758 Mon Sep 17 00:00:00 2001 From: Julia Ortiz <94128293+julia-script@users.noreply.github.com> Date: Tue, 16 Jan 2024 12:09:09 -0300 Subject: [PATCH] add checks on cron jobs and flushes redis on exceptions (#103) --- cronjobs/cron.py | 42 +++++++++++++++++++++++++------------ cronjobs/price.py | 28 ++++++++++++++++++------- cronjobs/redis_functions.py | 7 +++++++ 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/cronjobs/cron.py b/cronjobs/cron.py index d4300ec..95393e8 100644 --- a/cronjobs/cron.py +++ b/cronjobs/cron.py @@ -16,6 +16,7 @@ from redis_functions import setPrimaryEndpoint from redis_functions import setSecondaryEndpoint from redis_functions import setTertiaryEndpoint +from redis_functions import flushChains @total_ordering @@ -195,21 +196,36 @@ def process_chain(chain: str, chain_info): def main(): global running threads = [] + attempt = 0 while running: - chain_data = get_chain_config() - chain_info = get_chains_info(chain_data) - start_time = time.time() - for chain in chain_info: - t = Thread(target=process_chain, args=(chain, chain_info, )) - t.start() - threads.append(t) - - for t in threads: - t.join() + try: + print('Getting chain config...') + chain_data = get_chain_config() + chain_info = get_chains_info(chain_data) + start_time = time.time() + for chain in chain_info: + t = Thread(target=process_chain, args=(chain, chain_info, )) + t.start() + threads.append(t) + + for t in threads: + t.join() + + print(f'Time used: {time.time() - start_time}') + threads = [] + attempt = 0 + time.sleep(5) + + except Exception as e: + print('Failed to get chain config, flushing redis and trying again') + print(e) + attempt += 1 + flushChains() + if attempt > 5: + time.sleep(5) + + - print(f'Time used: {time.time() - start_time}') - threads = [] - time.sleep(5) def signal_handler(sig, frame): diff --git a/cronjobs/price.py b/cronjobs/price.py index 427bcd2..cf725d5 100644 --- a/cronjobs/price.py +++ b/cronjobs/price.py @@ -8,7 +8,7 @@ from github import get_tokens from helpers import get_erc20_coins -from redis_functions import redisSetPrice, redisSetEvmosChange +from redis_functions import redisSetPrice, redisSetEvmosChange, flushTokens def get_evmos_change(): @@ -49,14 +49,26 @@ def process_assets(prices): def main(): global running + attempt = 0 while running: - tracked_tokens = get_tokens() - erc20_module_coins = get_erc20_coins(tracked_tokens) - print('Getting prices...') - prices = get_prices("usd", erc20_module_coins) - get_evmos_change() - process_assets(prices) - time.sleep(300) + try: + tracked_tokens = get_tokens() + erc20_module_coins = get_erc20_coins(tracked_tokens) + print('Getting prices...') + prices = get_prices("usd", erc20_module_coins) + get_evmos_change() + process_assets(prices) + attempt = 0 + time.sleep(300) + except Exception as e: + print('Failed to get prices, trying again') + print(e) + attempt += 1 + flushTokens() + if attempt > 5: + time.sleep(5) + + def signal_handler(sig, frame): diff --git a/cronjobs/redis_functions.py b/cronjobs/redis_functions.py index 80290e0..731b67e 100644 --- a/cronjobs/redis_functions.py +++ b/cronjobs/redis_functions.py @@ -101,3 +101,10 @@ def getChains(): if not value: return None return json.loads(value) + + +def flushChains(): + r.delete(networkConfig) + +def flushTokens(): + r.delete(erc20TokensDirectoryKey) \ No newline at end of file