22 lines
572 B
Python
22 lines
572 B
Python
import logging
|
|
from collections.abc import Callable
|
|
from functools import wraps
|
|
from time import sleep
|
|
from sqlalchemy.exc import DBAPIError
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
def dbRetry(func: Callable, max_retries=60, delay=5, exceptions=(DBAPIError,)):
|
|
@wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
for i in range(max_retries):
|
|
try:
|
|
return func(*args, **kwargs)
|
|
except exceptions as e:
|
|
log.warning(f"DB function {func.__name__} attempt {i + 1} failed with: {e}")
|
|
sleep(delay)
|
|
raise Exception("DB operation retry limit reached!")
|
|
|
|
return wrapper
|