Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit f6f8401

Browse files
larroymarcoabreu
authored andcommitted
Add a retry to qemu_provision (#13551)
Fixes #13504
1 parent bd8e0f8 commit f6f8401

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

ci/docker/qemu/vmcontrol.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,46 @@
6969
-nographic
7070
"""
7171

72+
def retry(target_exception, tries=4, delay_s=1, backoff=2):
73+
"""Retry calling the decorated function using an exponential backoff.
74+
75+
http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
76+
original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
77+
78+
:param target_exception: the exception to check. may be a tuple of
79+
exceptions to check
80+
:type target_exception: Exception or tuple
81+
:param tries: number of times to try (not retry) before giving up
82+
:type tries: int
83+
:param delay_s: initial delay between retries in seconds
84+
:type delay_s: int
85+
:param backoff: backoff multiplier e.g. value of 2 will double the delay
86+
each retry
87+
:type backoff: int
88+
"""
89+
import time
90+
from functools import wraps
91+
92+
def decorated_retry(f):
93+
@wraps(f)
94+
def f_retry(*args, **kwargs):
95+
mtries, mdelay = tries, delay_s
96+
while mtries > 1:
97+
try:
98+
return f(*args, **kwargs)
99+
except target_exception as e:
100+
logging.warning("Exception: %s, Retrying in %d seconds...", str(e), mdelay)
101+
time.sleep(mdelay)
102+
mtries -= 1
103+
mdelay *= backoff
104+
return f(*args, **kwargs)
105+
106+
return f_retry # true decorator
107+
108+
return decorated_retry
109+
110+
111+
72112

73113
class VMError(RuntimeError):
74114
pass
@@ -177,6 +217,8 @@ def qemu_rsync(ssh_port, local_path, remote_path):
177217
def qemu_rsync_to_host(ssh_port, remote_path, local_path):
178218
check_call(['rsync', '-e', 'ssh -o StrictHostKeyChecking=no -p{}'.format(ssh_port), '-va', 'qemu@localhost:{}'.format(remote_path), local_path])
179219

220+
221+
@retry(subprocess.CalledProcessError)
180222
def qemu_provision(ssh_port=QEMU_SSH_PORT):
181223
import glob
182224
logging.info("Provisioning the VM with artifacts and sources")

0 commit comments

Comments
 (0)