-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry.py
48 lines (40 loc) · 1.67 KB
/
retry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from typing import TypeVar, Callable, Optional, Any
import logging
import time
T = TypeVar('T')
def with_retries(
f: Callable[..., T],
max_retries: int,
delay: int,
description: str = "Operation",
check_fn: Optional[Callable[[T], bool]] = None
) -> Callable[..., T]:
"""Higher-order function that adds retry capability to any function.
Args:
f: The function to wrap with retry logic
max_retries: Maximum number of retry attempts
delay: Delay between retries in seconds
description: Description of the operation for logging
check_fn: Optional function to check if retry should continue
"""
def retried_f(*args: Any, **kwargs: Any) -> T:
last_exception = None
for attempt in range(max_retries):
try:
result = f(*args, **kwargs)
if check_fn is None or check_fn(result):
return result
if attempt < max_retries - 1:
logging.info(f"{description}: attempt {attempt + 1}/{max_retries} failed, retrying in {delay}s...")
time.sleep(delay)
continue
except Exception as e:
last_exception = e
if attempt < max_retries - 1:
logging.info(f"{description}: attempt {attempt + 1}/{max_retries} failed with {str(e)}, retrying in {delay}s...")
time.sleep(delay)
continue
if last_exception:
raise last_exception
raise TimeoutError(f"{description} failed after {max_retries} attempts")
return retried_f