Skip to content

using tenacity in download_binary function #399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
20 changes: 11 additions & 9 deletions report/firefox_code_coverage/codecoverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys
import tarfile
import tempfile
import tenacity
import time
import warnings

Expand Down Expand Up @@ -69,27 +70,28 @@ def _save_tasks(response):

def download_binary(url, path, retries=5):
"""Download a binary file from an url"""
for i in range(1, retries + 1):
@tenacity.retry(stop=tenacity.stop_after_attempt(retries),
wait=tenacity.wait_incrementing(start = 7, increment = 7),
reraise=True)
def retry_function():
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
except:
try:
os.remove(path)
except OSError:
pass

if i == retries:
raise Exception(
"Download failed after {} retries - {}".format(retries, url)

raise Exception(
"Download failed after {} retries - {}".format(retry_function.retry.statistics["attempt_number"], url)
)

time.sleep(7 * i)
retry_function()


def download_artifact(task_id, artifact, artifacts_path):
Expand Down