Commit 794846ae authored by BO ZHANG's avatar BO ZHANG 🏀
Browse files

rewrite retry

parent 1ad1b00c
Loading
Loading
Loading
Loading
+17 −10
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ Modified-History:
    2023-12-20, Bo Zhang, add retry
"""

import traceback

def retry(func, *args, **kwargs):
    if "n_try" in kwargs.keys():
@@ -15,11 +16,7 @@ def retry(func, *args, **kwargs):
    else:
        n_try = 5
    # is DFS?
    try:
        assert func.__self__.__class__.__module__.startswith("csst_dfs_api"), func
        is_dfs = True
    except BaseException:
        is_dfs = False
    is_dfs = func.__self__.__class__.__module__.startswith("csst_dfs_client")
    # start trials
    for attempt in range(n_try):
        print(f"Trial #{attempt}: call {func}")
@@ -28,9 +25,19 @@ def retry(func, *args, **kwargs):
            if is_dfs:
                assert res["code"] == 0, res
            return res
        except BaseException as e:
            print(f"Error occurs: {e.__repr__()}")
            print(f" - func: {func}")
            print(f" - args: {args}")
            print(f" - kwargs: {kwargs}")
        except Exception as e:
            # print error messages
            print(f"Error occurs in trial {attempt + 1}/{n_try}:")
            print(f" - Error type: {type(e).__name__}")
            print(f" - Error message: {str(e)}")
            print(f" - Function: {func}")
            print(f" - Args: {args}")
            print(f" - Kwargs: {kwargs}")
            # 打印完整的堆栈跟踪
            print(" - Stack trace:")
            traceback.print_exc()
            if attempt == n_try - 1:
                print(f"All {n_try} attempts failed. Reraising exception...")
                raise

    raise RuntimeError(f"All {n_try} attempts failed.")