retry.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import functools
  2. import random
  3. import time
  4. from functools import partial
  5. from module.logger import logger as logging_logger
  6. """
  7. Copied from Alas
  8. """
  9. try:
  10. from decorator import decorator
  11. except ImportError:
  12. def decorator(caller):
  13. """ Turns caller into a decorator.
  14. Unlike decorator module, function signature is not preserved.
  15. :param caller: caller(f, *args, **kwargs)
  16. """
  17. def decor(f):
  18. @functools.wraps(f)
  19. def wrapper(*args, **kwargs):
  20. return caller(f, *args, **kwargs)
  21. return wrapper
  22. return decor
  23. def __retry_internal(f, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0,
  24. logger=logging_logger):
  25. """
  26. Executes a function and retries it if it failed.
  27. :param f: the function to execute.
  28. :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
  29. :param tries: the maximum number of attempts. default: -1 (infinite).
  30. :param delay: initial delay between attempts. default: 0.
  31. :param max_delay: the maximum value of delay. default: None (no limit).
  32. :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
  33. :param jitter: extra seconds added to delay between attempts. default: 0.
  34. fixed if a number, random if a range tuple (min, max)
  35. :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
  36. default: retry.logging_logger. if None, logging is disabled.
  37. :returns: the result of the f function.
  38. """
  39. _tries, _delay = tries, delay
  40. while _tries:
  41. try:
  42. return f()
  43. except exceptions as e:
  44. _tries -= 1
  45. if not _tries:
  46. # Difference, raise same exception
  47. raise e
  48. if logger is not None:
  49. # Difference, show exception
  50. logger.exception(e)
  51. logger.warning(f'{type(e).__name__}({e}), retrying in {_delay} seconds...')
  52. time.sleep(_delay)
  53. _delay *= backoff
  54. if isinstance(jitter, tuple):
  55. _delay += random.uniform(*jitter)
  56. else:
  57. _delay += jitter
  58. if max_delay is not None:
  59. _delay = min(_delay, max_delay)
  60. def retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0, logger=logging_logger):
  61. """Returns a retry decorator.
  62. :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
  63. :param tries: the maximum number of attempts. default: -1 (infinite).
  64. :param delay: initial delay between attempts. default: 0.
  65. :param max_delay: the maximum value of delay. default: None (no limit).
  66. :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
  67. :param jitter: extra seconds added to delay between attempts. default: 0.
  68. fixed if a number, random if a range tuple (min, max)
  69. :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
  70. default: retry.logging_logger. if None, logging is disabled.
  71. :returns: a retry decorator.
  72. """
  73. @decorator
  74. def retry_decorator(f, *fargs, **fkwargs):
  75. args = fargs if fargs else list()
  76. kwargs = fkwargs if fkwargs else dict()
  77. return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter,
  78. logger)
  79. return retry_decorator
  80. def retry_call(f, fargs=None, fkwargs=None, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1,
  81. jitter=0,
  82. logger=logging_logger):
  83. """
  84. Calls a function and re-executes it if it failed.
  85. :param f: the function to execute.
  86. :param fargs: the positional arguments of the function to execute.
  87. :param fkwargs: the named arguments of the function to execute.
  88. :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
  89. :param tries: the maximum number of attempts. default: -1 (infinite).
  90. :param delay: initial delay between attempts. default: 0.
  91. :param max_delay: the maximum value of delay. default: None (no limit).
  92. :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
  93. :param jitter: extra seconds added to delay between attempts. default: 0.
  94. fixed if a number, random if a range tuple (min, max)
  95. :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
  96. default: retry.logging_logger. if None, logging is disabled.
  97. :returns: the result of the f function.
  98. """
  99. args = fargs if fargs else list()
  100. kwargs = fkwargs if fkwargs else dict()
  101. return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter, logger)