forked from egao1980/man.battleships
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
32 lines (25 loc) · 1003 Bytes
/
utils.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
from exceptions import MaxRetriesExceededException
from config import MAX_RETRIES
import logging
def retry(exceptions, max_retries=MAX_RETRIES):
"""
Retry decorator that retries the wrapped function a maximum of 'max_retries' times if 'exception' is raised
:param exceptions: Tuple
:param max_retries:
:return:
"""
def deco_retry(f):
def f_retry(*args, **kwargs):
n_retries = 0
while n_retries < max_retries:
logging.info(f'Attempt {n_retries}: {f.__name__} for {args[1].name}')
try:
return f(*args, **kwargs)
except exceptions as e:
logging.error(f'{args[1].name}: {e} when calling {f.__name__}')
n_retries += 1
continue
logging.error(f'{args[1].name} reached maximum number of retries on {f.__name__}')
raise MaxRetriesExceededException
return f_retry
return deco_retry