Commits

jerryji committed a06f02c

added retry decorator to api.call

Comments (0)

Files changed (2)

amazonproduct/api.py

 from amazonproduct.utils import load_config, running_on_gae, REQUIRED_KEYS
 from amazonproduct.processors import ITEMS_PAGINATOR
 
+from amazonproduct.retry import retry
 
 # load default processor
 try:
             # otherwise simply re-raise
             raise
 
+    @retry(urllib2.URLError)
     def call(self, **qargs):
         """
         Builds a signed URL for the operation, fetches the result from Amazon

amazonproduct/retry.py

+import math
+import time
+
+def retry(ExceptionToRetry, tries=5, delay=3, logger=None):
+    """Retry decorator
+    original from http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
+    """
+    tries = math.floor(tries)
+    if tries < 0:
+        raise ValueError("tries must be 0 or greater")
+    if delay <= 0:
+        raise ValueError("delay must be greater than 0")
+    def deco_retry(f):
+        def f_retry(*args, **kwargs):
+            mtries, mdelay = tries, delay
+            while mtries > 0:
+                try:
+                    return f(*args, **kwargs)
+                except ExceptionToRetry, e:
+                    if mtries == 1:
+                        msg = "%s, retry maxed, gave up" % str(e)
+                    else:
+                        msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
+                    if logger:
+                        logger.warning(msg)
+                    else:
+                        print msg
+                    if mtries == 1:
+                        break
+                    time.sleep(mdelay)
+                    mtries -= 1
+            raise e
+        return f_retry # true decorator
+    return deco_retry