[csw-devel] SF.net SVN: gar:[20806] csw/mgar/gar/v2/lib/python

wahwah at users.sourceforge.net wahwah at users.sourceforge.net
Thu Apr 18 17:19:28 CEST 2013


Revision: 20806
          http://gar.svn.sourceforge.net/gar/?rev=20806&view=rev
Author:   wahwah
Date:     2013-04-18 15:19:28 +0000 (Thu, 18 Apr 2013)
Log Message:
-----------
pkgdb: Retry failed calls in RestClient

Modified Paths:
--------------
    csw/mgar/gar/v2/lib/python/rest.py

Added Paths:
-----------
    csw/mgar/gar/v2/lib/python/retry_decorator.py

Modified: csw/mgar/gar/v2/lib/python/rest.py
===================================================================
--- csw/mgar/gar/v2/lib/python/rest.py	2013-04-18 15:19:15 UTC (rev 20805)
+++ csw/mgar/gar/v2/lib/python/rest.py	2013-04-18 15:19:28 UTC (rev 20806)
@@ -9,6 +9,8 @@
 import re
 import urllib2
 
+import retry_decorator
+
 DEFAULT_URL = "http://buildfarm.opencsw.org"
 
 class Error(Exception):
@@ -166,6 +168,7 @@
           "%s - HTTP code: %s, content: %s"
           % (url, http_code, d.getvalue()))
 
+  @retry_decorator.Retry(tries=4, exceptions=RestCommunicationError)
   def _CurlPut(self, url, data):
     """Makes a PUT request, potentially uploading data.
 

Added: csw/mgar/gar/v2/lib/python/retry_decorator.py
===================================================================
--- csw/mgar/gar/v2/lib/python/retry_decorator.py	                        (rev 0)
+++ csw/mgar/gar/v2/lib/python/retry_decorator.py	2013-04-18 15:19:28 UTC (rev 20806)
@@ -0,0 +1,40 @@
+# From: https://gist.github.com/hoffmann/470611
+# With small changes.
+
+import time
+import logging
+
+class Retry(object):
+  default_exceptions = (Exception,)
+  def __init__(self, tries, exceptions=None, delay=0, logger=None):
+    """Decorator for retrying function if exception occurs
+
+    Args:
+      tries: num tries
+      exceptions: exceptions to catch
+      delay: wait seconds between retries
+    """
+    self.tries = tries
+    self.exceptions = exceptions
+    if self.exceptions is None:
+      self.exceptions = Retry.default_exceptions
+    self.delay = delay
+    self.logger=logger
+
+  def __call__(self, f):
+    def fn(*args, **kwargs):
+      last_exception = None
+      for _ in range(self.tries):
+        try:
+          return f(*args, **kwargs)
+        except self.exceptions, e:
+          msg = "Retry, exception: "+str(e)
+          if self.logger:
+            self.logger.info(msg)
+          else:
+            logging.info(msg)
+          time.sleep(self.delay)
+          last_exception = e
+      # If no success after all tries, raise last exception.
+      raise last_exception
+    return fn

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the devel mailing list