On Tue, Apr 28, 2015 at 06:32:44PM +0200, 'Klaus Aehlig' via ganeti-devel wrote:


commit c52e1d86db89e450c2d6884215ec2ece8436e871
Merge: 46716e3 1d0abda
Author: Klaus Aehlig <[email protected]>
Date:   Tue Apr 28 18:24:02 2015 +0200

   Merge branch 'stable-2.13' into stable-2.14

   * stable-2.13
     Rename SSH Update evaluation function more expressively
     Using named tuple for node info in ssh file manager
     Helper functions to assert key existence for sets of nodes
     Add RetryByNumberOfTimes utility function

   * stable-2.12
     Revision bump to 2.12.3
     Update NEWS for 2.12.3

   Conflicts:
        lib/cmdlib/cluster/verify.py: take stable-2.14
        lib/utils/retry.py: take both additions

diff --cc lib/utils/retry.py
index d6ed1ac,fda3fcd..8079303
--- a/lib/utils/retry.py
+++ b/lib/utils/retry.py
@@@ -232,21 -233,28 +233,48 @@@ def SimpleRetry(expected, fn, delay, ti
   return result


+def CountRetry(expected, fn, count, args=None):
+  """A wrapper over L{SimpleRetry} implementing a count down.
+
+  Where L{Retry} fixes the time, after which the command is assumed to be
+  failing, this function assumes the total number of tries.
+
+  @see: L{Retry}
+  """
+
+  rdict = {"tries": 0}
+
+  get_tries = lambda: rdict["tries"]
+
+  def inc_tries(t):
+    rdict["tries"] += t
+
+  return SimpleRetry(expected, fn, 1, count, args=args,
+                     wait_fn=inc_tries, _time_fn=get_tries)
++
++
+ def RetryByNumberOfTimes(max_retries, exception_class, fn, *args, **kwargs):
+   """Retries calling a function up to the specified number of times.
+
+   @type max_retries: integer
+   @param max_retries: Maximum number of retries.
+   @type exception_class: class
+   @param exception_class: Exception class which is used for throwing the
+                           final exception.
+   @type fn: callable
+   @param fn: Function to be called (up to the specified maximum number of
+              retries.
+
+   """
+   last_exception = None
+   for i in range(max_retries):
+     try:
+       fn(*args, **kwargs)
+       break
+     except errors.OpExecError as e:
+       logging.error("Error after retry no. %s: %s.", i, e)
+       last_exception = e
+   else:
+     if last_exception:
+       raise exception_class("Error after %s retries. Last exception: %s."
+                             % (max_retries, last_exception))

--
Klaus Aehlig
Google Germany GmbH, Dienerstr. 12, 80331 Muenchen
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschaeftsfuehrer: Graham Law, Christine Elizabeth Flores

LGTM, thanks

Reply via email to