]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
node-proxy: add a decorator 'retry'
authorGuillaume Abrioux <gabrioux@ibm.com>
Fri, 9 Jun 2023 12:58:02 +0000 (14:58 +0200)
committerGuillaume Abrioux <gabrioux@ibm.com>
Thu, 25 Jan 2024 14:43:30 +0000 (14:43 +0000)
This decorator will be useful for calls that should do multiple
attempts before actually failing.

Signed-off-by: Guillaume Abrioux <gabrioux@ibm.com>
src/cephadm/node-proxy/util.py

index 817583e6ff7f568038abb044a1db762494f5f95d..6420e904876f2f2df209cc5f42c8ca6aecb809e8 100644 (file)
@@ -1,7 +1,8 @@
 import logging
 import yaml
 import os
-from typing import Dict, List
+import time
+from typing import Dict, List, Callable, Any
 
 
 def normalize_dict(test_dict: Dict) -> Dict:
@@ -69,3 +70,20 @@ class Logger:
         logger.addHandler(handler)
 
         return logger
+
+
+def retry(exceptions: Any = Exception, retries: int = 20, delay: int = 1) -> Callable:
+    def decorator(f: Callable) -> Callable:
+        def _retry(*args: str, **kwargs: Any) -> Callable:
+            _tries = retries
+            while _tries > 1:
+                try:
+                    print("{}".format(_tries))
+                    return f(*args, **kwargs)
+                except exceptions:
+                    time.sleep(delay)
+                    _tries -= 1
+            print("{} has failed after {} tries".format(f, retries))
+            return f(*args, **kwargs)
+        return _retry
+    return decorator