stop.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright 2016 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 six
  18. from tenacity import compat as _compat
  19. @six.add_metaclass(abc.ABCMeta)
  20. class stop_base(object):
  21. """Abstract base class for stop strategies."""
  22. @abc.abstractmethod
  23. def __call__(self, retry_state):
  24. pass
  25. def __and__(self, other):
  26. return stop_all(self, other)
  27. def __or__(self, other):
  28. return stop_any(self, other)
  29. class stop_any(stop_base):
  30. """Stop if any of the stop condition is valid."""
  31. def __init__(self, *stops):
  32. self.stops = tuple(_compat.stop_func_accept_retry_state(stop_func)
  33. for stop_func in stops)
  34. @_compat.stop_dunder_call_accept_old_params
  35. def __call__(self, retry_state):
  36. return any(x(retry_state) for x in self.stops)
  37. class stop_all(stop_base):
  38. """Stop if all the stop conditions are valid."""
  39. def __init__(self, *stops):
  40. self.stops = tuple(_compat.stop_func_accept_retry_state(stop_func)
  41. for stop_func in stops)
  42. @_compat.stop_dunder_call_accept_old_params
  43. def __call__(self, retry_state):
  44. return all(x(retry_state) for x in self.stops)
  45. class _stop_never(stop_base):
  46. """Never stop."""
  47. @_compat.stop_dunder_call_accept_old_params
  48. def __call__(self, retry_state):
  49. return False
  50. stop_never = _stop_never()
  51. class stop_when_event_set(stop_base):
  52. """Stop when the given event is set."""
  53. def __init__(self, event):
  54. self.event = event
  55. @_compat.stop_dunder_call_accept_old_params
  56. def __call__(self, retry_state):
  57. return self.event.is_set()
  58. class stop_after_attempt(stop_base):
  59. """Stop when the previous attempt >= max_attempt."""
  60. def __init__(self, max_attempt_number):
  61. self.max_attempt_number = max_attempt_number
  62. @_compat.stop_dunder_call_accept_old_params
  63. def __call__(self, retry_state):
  64. return retry_state.attempt_number >= self.max_attempt_number
  65. class stop_after_delay(stop_base):
  66. """Stop when the time from the first attempt >= limit."""
  67. def __init__(self, max_delay):
  68. self.max_delay = max_delay
  69. @_compat.stop_dunder_call_accept_old_params
  70. def __call__(self, retry_state):
  71. return retry_state.seconds_since_start >= self.max_delay