Commit 4aba3bf0 authored by BO ZHANG's avatar BO ZHANG 🏀
Browse files

simplify retry

parent 2fc29baa
Pipeline #11836 passed with stage
in 0 seconds
...@@ -384,8 +384,8 @@ class Pipeline: ...@@ -384,8 +384,8 @@ class Pipeline:
# retry operations # retry operations
@staticmethod @staticmethod
def retry(*args, **kwargs): def retry(func: Callable, *args, **kwargs):
return retry(*args, **kwargs) return retry(5, func, *args, **kwargs)
@property @property
def pmapname(self) -> str: def pmapname(self) -> str:
......
...@@ -6,31 +6,22 @@ Author: Bo Zhang ...@@ -6,31 +6,22 @@ Author: Bo Zhang
Created: 2023-12-20 Created: 2023-12-20
Modified-History: Modified-History:
2023-12-20, Bo Zhang, add retry 2023-12-20, Bo Zhang, add retry
2026-01-13, Bo Zhang, simplify retry
""" """
import traceback import traceback
from typing import Callable
def retry(func, *args, **kwargs): def retry(n_try: int, func: Callable, *args, **kwargs):
if "n_try" in kwargs.keys():
n_try = kwargs.pop("n_try")
else:
n_try = 5
# is DFS?
# TODO: support DFS2
# is_dfs = func.__class__.__module__.startswith("csst_dfs_client")
# start trials # start trials
for attempt in range(n_try): for i_try in range(n_try):
print(f"Trial #{attempt}: call {func}") print(f"Trial #{i_try}: call {func}")
try: try:
res = func(*args, **kwargs) return func(*args, **kwargs)
# if is_dfs:
# # TODO: support DFS2
# assert res.success, res
return res
except Exception as e: except Exception as e:
# print error messages # print error messages
print(f"Error occurs in trial {attempt + 1}/{n_try}:") print(f"Error occurs in trial {i_try + 1}/{n_try}:")
print(f" - Error type: {type(e).__name__}") print(f" - Error type: {type(e).__name__}")
print(f" - Error message: {str(e)}") print(f" - Error message: {str(e)}")
print(f" - Function: {func}") print(f" - Function: {func}")
...@@ -39,8 +30,7 @@ def retry(func, *args, **kwargs): ...@@ -39,8 +30,7 @@ def retry(func, *args, **kwargs):
# 打印完整的堆栈跟踪 # 打印完整的堆栈跟踪
print(" - Stack trace:") print(" - Stack trace:")
traceback.print_exc() traceback.print_exc()
if attempt == n_try - 1: if i_try == n_try - 1:
print(f"All {n_try} attempts failed. Reraising exception...") print(f"All {n_try} attempts failed. Reraising exception...")
raise raise
raise RuntimeError(f"All {n_try} attempts failed.") raise RuntimeError(f"All {n_try} attempts failed.")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment