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:
# retry operations
@staticmethod
def retry(*args, **kwargs):
return retry(*args, **kwargs)
def retry(func: Callable, *args, **kwargs):
return retry(5, func, *args, **kwargs)
@property
def pmapname(self) -> str:
......
......@@ -6,31 +6,22 @@ Author: Bo Zhang
Created: 2023-12-20
Modified-History:
2023-12-20, Bo Zhang, add retry
2026-01-13, Bo Zhang, simplify retry
"""
import traceback
from typing import Callable
def retry(func, *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")
def retry(n_try: int, func: Callable, *args, **kwargs):
# start trials
for attempt in range(n_try):
print(f"Trial #{attempt}: call {func}")
for i_try in range(n_try):
print(f"Trial #{i_try}: call {func}")
try:
res = func(*args, **kwargs)
# if is_dfs:
# # TODO: support DFS2
# assert res.success, res
return res
return func(*args, **kwargs)
except Exception as e:
# 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 message: {str(e)}")
print(f" - Function: {func}")
......@@ -39,8 +30,7 @@ def retry(func, *args, **kwargs):
# 打印完整的堆栈跟踪
print(" - Stack trace:")
traceback.print_exc()
if attempt == n_try - 1:
if i_try == n_try - 1:
print(f"All {n_try} attempts failed. Reraising exception...")
raise
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