From 8bf6781e22ac532802e0383f86298c836758a603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20D=C3=ADaz?= Date: Mon, 27 Dec 2021 13:29:59 +0100 Subject: [PATCH] fix: handle timeout on request dry-runs --- price_feed_poller.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/price_feed_poller.py b/price_feed_poller.py index 6fb49d9..35e7664 100755 --- a/price_feed_poller.py +++ b/price_feed_poller.py @@ -9,6 +9,7 @@ from config import load_config from contract import pf_contracts, pf_thresholds, wrb +from threading import Timer from web3 import Web3, exceptions # Post a data request to the post_dr method of the WRB contract @@ -164,9 +165,18 @@ def dry_run_request(bytecode): cmdline = "npx witnet-toolkit try-data-request --hex " cmdline += bytecode.hex() cmdline += " | tail -n 2 | head -n 1 | awk -F: '{ print $2 }' | sed 's/ //g' | tr -d \"│\"" + process = subprocess.Popen(cmdline, stdout=subprocess.PIPE, shell=True) - process.wait() - output, error = process.communicate() + timer = Timer(15, process.kill) + try: + timer.start() + process.wait() + output, error = process.communicate() + finally: + timer.cancel() + + if len(output) == 0: + raise Exception("Timeout while trying data request") if error is not None: raise Exception(error) return int(output) @@ -266,9 +276,10 @@ def log_loop( # If thresholds is configured, evaluate actual price deviation try: next_price = dry_run_request(element['feed'].functions.bytecode().call()) - except: + except Exception as ex: # If dry run fails, assume 0 deviation as to, at least, guarantee the heartbeat periodicity is met - print("Cannot dry run request:", element['feed'].functions.bytecode().call().hex()) + print("Cannot dry run request:", ex) + print(element['feed'].functions.bytecode().call().hex()) next_price = last_price deviation = round(100 * ((next_price - last_price) / last_price), 2) if abs(deviation) < thresholds[index] and elapsed_secs < max_secs_between_request_updates: