Skip to content

Commit 433515e

Browse files
author
Logan Sulpizio
committed
Fix indexing logic when all attempts with the same RPC fail
1 parent d8adcca commit 433515e

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

yam_indexing_module/main_indexing.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,15 @@ def main_indexing():
6363
while True:
6464

6565
start_time = time.time()
66+
67+
success = False
6668

6769
for attempt in range(MAX_RETRIES_PER_BLOCK_RANGE):
6870

6971
try:
7072
raw_logs = get_raw_logs_yam(w3, yam_contract_address, from_block, to_block)
73+
success = True
74+
break # leave the for loop if success
7175

7276
except Exception as e:
7377

@@ -80,9 +84,13 @@ def main_indexing():
8084
old_indice = w3_indice
8185
w3_indice = (w3_indice + 1) % len(w3_urls) # This will give you the sequence: 0 → 1 → 2 → ... → n → 0 → 1 → 2 → ... → n → 0 ...
8286
w3 = Web3(Web3.HTTPProvider(w3_urls[w3_indice]))
83-
logger.info(f"Blocks retrieval failed too many times. Changing from w3 RPC n°{old_indice + 1} to w3 RPC n°{w3_indice + 1}")
87+
logger.info(f"Blocks retrieval failed too many times. Changing from w3 RPC n°{old_indice + 1} to w3 RPC n°{w3_indice + 1} [{w3.provider.endpoint_uri.split('//')[1].rsplit('/', 1)[0]}]")
8488
continue
8589

90+
if not success:
91+
logger.info(f"All attempts with the same RPC [{w3.provider.endpoint_uri.split('//')[1].rsplit('/', 1)[0]}] failed.")
92+
continue
93+
8694
decoded_logs = decode_raw_logs_yam(raw_logs)
8795

8896
### Add logs to the DB
@@ -124,8 +132,14 @@ def main_indexing():
124132
logger.info("Received Ctrl+C, shutting down the indexing service...")
125133
print("Process stopped by user")
126134
except Exception as e:
127-
logger.error(f"Indexing loop failed with error: {str(e)}", exc_info=True)
128-
print(f"Indexing loop failed with error: {str(e)}")
135+
logger.error(f"Indexing loop failed with error: {str(e)}", exc_info=True)
136+
print(f"Indexing loop failed with error: {str(e)}")
129137

130138
if __name__ == "__main__":
131-
main_indexing()
139+
while True:
140+
try:
141+
main_indexing()
142+
except Exception as e:
143+
logger = logging.getLogger(__name__)
144+
logger.exception("Fatal error in main_indexing. Restarting in 30 seconds...")
145+
time.sleep(30)

0 commit comments

Comments
 (0)