Add simple retry for maas api ops
* Misc: auto-ident
Prod-related: PROD-27072 (PROD:27072)
Change-Id: I59cd35f7b00a855d503149107d12678c3950c309
diff --git a/_modules/maas_client.py b/_modules/maas_client.py
index 97aeb8b..061dcfa 100644
--- a/_modules/maas_client.py
+++ b/_modules/maas_client.py
@@ -7,7 +7,7 @@
absolute_import,
print_function,
unicode_literals,
- )
+)
str = None
@@ -16,9 +16,11 @@
'MAASClient',
'MAASDispatcher',
'MAASOAuth',
- ]
+]
import gzip
+import time
+from functools import wraps
from io import BytesIO
import urllib2
@@ -28,6 +30,51 @@
import oauth.oauth as oauth
+def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
+ """Retry calling the decorated function using an exponential backoff.
+
+ http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
+ original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
+
+ :param ExceptionToCheck: the exception to check. may be a tuple of
+ exceptions to check
+ :type ExceptionToCheck: Exception or tuple
+ :param tries: number of times to try (not retry) before giving up
+ :type tries: int
+ :param delay: initial delay between retries in seconds
+ :type delay: int
+ :param backoff: backoff multiplier e.g. value of 2 will double the delay
+ each retry
+ :type backoff: int
+ :param logger: logger to use. If None, print
+ :type logger: logging.Logger instance
+ """
+
+ def deco_retry(f):
+
+ @wraps(f)
+ def f_retry(*args, **kwargs):
+ mtries, mdelay = tries, delay
+ while mtries > 1:
+ try:
+ return f(*args, **kwargs)
+ except ExceptionToCheck, e:
+ msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
+ if logger:
+ logger.warning(msg)
+ else:
+ print
+ msg
+ time.sleep(mdelay)
+ mtries -= 1
+ mdelay *= backoff
+ return f(*args, **kwargs)
+
+ return f_retry # true decorator
+
+ return deco_retry
+
+
class MAASOAuth:
"""Helper class to OAuth-sign an HTTP request."""
@@ -67,6 +114,7 @@
class RequestWithMethod(urllib2.Request):
"""Enhances urllib2.Request so an http method can be supplied."""
+
def __init__(self, *args, **kwargs):
self._method = kwargs.pop('method', None)
urllib2.Request.__init__(self, *args, **kwargs)
@@ -85,6 +133,7 @@
provider in Juju for the code this would require.
"""
+ @retry(urllib2.URLError, tries=2, delay=5, backoff=2)
def dispatch_query(self, request_url, headers, method="GET", data=None):
"""Synchronously dispatch an OAuth-signed request to L{request_url}.