From 183b6340c69cd441fdb022df0662974e28e9e1c0 Mon Sep 17 00:00:00 2001 From: Menarul Alam Date: Mon, 3 Feb 2020 14:07:45 -0500 Subject: [PATCH] Added tenacity retry decorator with exponential backoff to function and updated requirements.txt --- report/firefox_code_coverage/codecoverage.py | 46 +++++++++++--------- report/requirements.txt | 1 + 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/report/firefox_code_coverage/codecoverage.py b/report/firefox_code_coverage/codecoverage.py index 4cc6d9498..19a132bfa 100644 --- a/report/firefox_code_coverage/codecoverage.py +++ b/report/firefox_code_coverage/codecoverage.py @@ -15,6 +15,7 @@ import requests from firefox_code_coverage import taskcluster +from tenacity import retry, stop_after_attempt, wait_exponential FINISHED_STATUSES = ["completed", "failed", "exception"] ALL_STATUSES = FINISHED_STATUSES + ["unscheduled", "pending", "running"] @@ -67,30 +68,33 @@ def _save_tasks(response): return tasks -def download_binary(url, path, retries=5): - """Download a binary file from an url""" - for i in range(1, retries + 1): +@retry(stop=stop_after_attempt(5), wait=wait_exponential(multiplier=1, max=64)) +def retry_download_binary(url, path): + """Download a binary file from an url with exponential backoff""" + try: + artifact = requests.get(url, stream=True) + artifact.raise_for_status() + + with open(path, "wb") as f: + for chunk in artifact.iter_content(chunk_size=8192): + f.write(chunk) + return + except: # noqa: E722 try: - artifact = requests.get(url, stream=True) - artifact.raise_for_status() - - with open(path, "wb") as f: - for chunk in artifact.iter_content(chunk_size=8192): - f.write(chunk) - break - except: # noqa: E722 - try: - os.remove(path) - except OSError: - pass - - if i == retries: - raise Exception( - "Download failed after {} retries - {}".format(retries, url) - ) + os.remove(path) + except OSError: + raise Exception - time.sleep(7 * i) +def download_binary(url, path): + """Wrapper function for retry_download_binary which raises exception after max number of retries""" + try: + retry_download_binary(url, path) + except: + raise Exception( + "Download failed after {} retries - {}".format(str(retry_download_binary.retry.statistics["attempt_number"]) + , url) + ) def download_artifact(task_id, artifact, artifacts_path): fname = os.path.join( diff --git a/report/requirements.txt b/report/requirements.txt index 28744c326..174d2f772 100644 --- a/report/requirements.txt +++ b/report/requirements.txt @@ -1 +1,2 @@ taskcluster==24.2.0 +tenacity==6.0.0 \ No newline at end of file