retry.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. # Copyright 2016–2021 Julien Danjou
  2. # Copyright 2016 Joshua Harlow
  3. # Copyright 2013-2014 Ray Holder
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import abc
  17. import re
  18. import typing
  19. if typing.TYPE_CHECKING:
  20. from tenacity import RetryCallState
  21. class retry_base(abc.ABC):
  22. """Abstract base class for retry strategies."""
  23. @abc.abstractmethod
  24. def __call__(self, retry_state: "RetryCallState") -> bool:
  25. pass
  26. def __and__(self, other: "retry_base") -> "retry_all":
  27. return other.__rand__(self)
  28. def __rand__(self, other: "retry_base") -> "retry_all":
  29. return retry_all(other, self)
  30. def __or__(self, other: "retry_base") -> "retry_any":
  31. return other.__ror__(self)
  32. def __ror__(self, other: "retry_base") -> "retry_any":
  33. return retry_any(other, self)
  34. RetryBaseT = typing.Union[retry_base, typing.Callable[["RetryCallState"], bool]]
  35. class _retry_never(retry_base):
  36. """Retry strategy that never rejects any result."""
  37. def __call__(self, retry_state: "RetryCallState") -> bool:
  38. return False
  39. retry_never = _retry_never()
  40. class _retry_always(retry_base):
  41. """Retry strategy that always rejects any result."""
  42. def __call__(self, retry_state: "RetryCallState") -> bool:
  43. return True
  44. retry_always = _retry_always()
  45. class retry_if_exception(retry_base):
  46. """Retry strategy that retries if an exception verifies a predicate."""
  47. def __init__(self, predicate: typing.Callable[[BaseException], bool]) -> None:
  48. self.predicate = predicate
  49. def __call__(self, retry_state: "RetryCallState") -> bool:
  50. if retry_state.outcome is None:
  51. raise RuntimeError("__call__() called before outcome was set")
  52. if retry_state.outcome.failed:
  53. exception = retry_state.outcome.exception()
  54. if exception is None:
  55. raise RuntimeError("outcome failed but the exception is None")
  56. return self.predicate(exception)
  57. else:
  58. return False
  59. class retry_if_exception_type(retry_if_exception):
  60. """Retries if an exception has been raised of one or more types."""
  61. def __init__(
  62. self,
  63. exception_types: typing.Union[
  64. typing.Type[BaseException],
  65. typing.Tuple[typing.Type[BaseException], ...],
  66. ] = Exception,
  67. ) -> None:
  68. self.exception_types = exception_types
  69. super().__init__(lambda e: isinstance(e, exception_types))
  70. class retry_if_not_exception_type(retry_if_exception):
  71. """Retries except an exception has been raised of one or more types."""
  72. def __init__(
  73. self,
  74. exception_types: typing.Union[
  75. typing.Type[BaseException],
  76. typing.Tuple[typing.Type[BaseException], ...],
  77. ] = Exception,
  78. ) -> None:
  79. self.exception_types = exception_types
  80. super().__init__(lambda e: not isinstance(e, exception_types))
  81. class retry_unless_exception_type(retry_if_exception):
  82. """Retries until an exception is raised of one or more types."""
  83. def __init__(
  84. self,
  85. exception_types: typing.Union[
  86. typing.Type[BaseException],
  87. typing.Tuple[typing.Type[BaseException], ...],
  88. ] = Exception,
  89. ) -> None:
  90. self.exception_types = exception_types
  91. super().__init__(lambda e: not isinstance(e, exception_types))
  92. def __call__(self, retry_state: "RetryCallState") -> bool:
  93. if retry_state.outcome is None:
  94. raise RuntimeError("__call__() called before outcome was set")
  95. # always retry if no exception was raised
  96. if not retry_state.outcome.failed:
  97. return True
  98. exception = retry_state.outcome.exception()
  99. if exception is None:
  100. raise RuntimeError("outcome failed but the exception is None")
  101. return self.predicate(exception)
  102. class retry_if_exception_cause_type(retry_base):
  103. """Retries if any of the causes of the raised exception is of one or more types.
  104. The check on the type of the cause of the exception is done recursively (until finding
  105. an exception in the chain that has no `__cause__`)
  106. """
  107. def __init__(
  108. self,
  109. exception_types: typing.Union[
  110. typing.Type[BaseException],
  111. typing.Tuple[typing.Type[BaseException], ...],
  112. ] = Exception,
  113. ) -> None:
  114. self.exception_cause_types = exception_types
  115. def __call__(self, retry_state: "RetryCallState") -> bool:
  116. if retry_state.outcome is None:
  117. raise RuntimeError("__call__ called before outcome was set")
  118. if retry_state.outcome.failed:
  119. exc = retry_state.outcome.exception()
  120. while exc is not None:
  121. if isinstance(exc.__cause__, self.exception_cause_types):
  122. return True
  123. exc = exc.__cause__
  124. return False
  125. class retry_if_result(retry_base):
  126. """Retries if the result verifies a predicate."""
  127. def __init__(self, predicate: typing.Callable[[typing.Any], bool]) -> None:
  128. self.predicate = predicate
  129. def __call__(self, retry_state: "RetryCallState") -> bool:
  130. if retry_state.outcome is None:
  131. raise RuntimeError("__call__() called before outcome was set")
  132. if not retry_state.outcome.failed:
  133. return self.predicate(retry_state.outcome.result())
  134. else:
  135. return False
  136. class retry_if_not_result(retry_base):
  137. """Retries if the result refutes a predicate."""
  138. def __init__(self, predicate: typing.Callable[[typing.Any], bool]) -> None:
  139. self.predicate = predicate
  140. def __call__(self, retry_state: "RetryCallState") -> bool:
  141. if retry_state.outcome is None:
  142. raise RuntimeError("__call__() called before outcome was set")
  143. if not retry_state.outcome.failed:
  144. return not self.predicate(retry_state.outcome.result())
  145. else:
  146. return False
  147. class retry_if_exception_message(retry_if_exception):
  148. """Retries if an exception message equals or matches."""
  149. def __init__(
  150. self,
  151. message: typing.Optional[str] = None,
  152. match: typing.Optional[str] = None,
  153. ) -> None:
  154. if message and match:
  155. raise TypeError(
  156. f"{self.__class__.__name__}() takes either 'message' or 'match', not both"
  157. )
  158. # set predicate
  159. if message:
  160. def message_fnc(exception: BaseException) -> bool:
  161. return message == str(exception)
  162. predicate = message_fnc
  163. elif match:
  164. prog = re.compile(match)
  165. def match_fnc(exception: BaseException) -> bool:
  166. return bool(prog.match(str(exception)))
  167. predicate = match_fnc
  168. else:
  169. raise TypeError(
  170. f"{self.__class__.__name__}() missing 1 required argument 'message' or 'match'"
  171. )
  172. super().__init__(predicate)
  173. class retry_if_not_exception_message(retry_if_exception_message):
  174. """Retries until an exception message equals or matches."""
  175. def __init__(
  176. self,
  177. message: typing.Optional[str] = None,
  178. match: typing.Optional[str] = None,
  179. ) -> None:
  180. super().__init__(message, match)
  181. # invert predicate
  182. if_predicate = self.predicate
  183. self.predicate = lambda *args_, **kwargs_: not if_predicate(*args_, **kwargs_)
  184. def __call__(self, retry_state: "RetryCallState") -> bool:
  185. if retry_state.outcome is None:
  186. raise RuntimeError("__call__() called before outcome was set")
  187. if not retry_state.outcome.failed:
  188. return True
  189. exception = retry_state.outcome.exception()
  190. if exception is None:
  191. raise RuntimeError("outcome failed but the exception is None")
  192. return self.predicate(exception)
  193. class retry_any(retry_base):
  194. """Retries if any of the retries condition is valid."""
  195. def __init__(self, *retries: retry_base) -> None:
  196. self.retries = retries
  197. def __call__(self, retry_state: "RetryCallState") -> bool:
  198. return any(r(retry_state) for r in self.retries)
  199. class retry_all(retry_base):
  200. """Retries if all the retries condition are valid."""
  201. def __init__(self, *retries: retry_base) -> None:
  202. self.retries = retries
  203. def __call__(self, retry_state: "RetryCallState") -> bool:
  204. return all(r(retry_state) for r in self.retries)