Skip to content

Commit 23822e2

Browse files
hfudevigrr
authored andcommitted
ci: retry download if catched IOError/EOFError
1 parent 4395be9 commit 23822e2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tools/ci/python_packages/gitlab_api.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
import tempfile
55
import tarfile
66
import zipfile
7+
from functools import wraps
78

89
import gitlab
910

1011

1112
class Gitlab(object):
1213
JOB_NAME_PATTERN = re.compile(r"(\w+)(\s+(\d+)/(\d+))?")
1314

15+
DOWNLOAD_ERROR_MAX_RETRIES = 3
16+
1417
def __init__(self, project_id=None):
1518
config_data_from_env = os.getenv("PYTHON_GITLAB_CONFIG")
1619
if config_data_from_env:
@@ -64,6 +67,31 @@ def download_artifacts(self, job_id, destination):
6467
with zipfile.ZipFile(temp_file.name, "r") as archive_file:
6568
archive_file.extractall(destination)
6669

70+
def retry_download(func):
71+
"""
72+
This wrapper will only catch IOError and retry the whole function.
73+
74+
So only use it with download functions, read() inside and atomic
75+
functions
76+
"""
77+
@wraps(func)
78+
def wrapper(self, *args, **kwargs):
79+
retried = 0
80+
while True:
81+
try:
82+
res = func(self, *args, **kwargs)
83+
except (IOError, EOFError) as e:
84+
retried += 1
85+
if retried > self.DOWNLOAD_ERROR_MAX_RETRIES:
86+
raise e # get out of the loop
87+
else:
88+
print('Retried for the {} time'.format(retried))
89+
continue
90+
else:
91+
break
92+
return res
93+
return wrapper
94+
6795
def download_artifact(self, job_id, artifact_path, destination=None):
6896
"""
6997
download specific path of job artifacts and extract to destination.
@@ -118,6 +146,7 @@ def find_job_id(self, job_name, pipeline_id=None, job_status="success"):
118146
job_id_list.append({"id": job.id, "parallel_num": match.group(3)})
119147
return job_id_list
120148

149+
@retry_download
121150
def download_archive(self, ref, destination, project_id=None):
122151
"""
123152
Download archive of certain commit of a repository and extract to destination path

0 commit comments

Comments
 (0)