__init__.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016-2018 Julien Danjou
  3. # Copyright 2017 Elisey Zanko
  4. # Copyright 2016 Étienne Bersac
  5. # Copyright 2016 Joshua Harlow
  6. # Copyright 2013-2014 Ray Holder
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. try:
  20. from inspect import iscoroutinefunction
  21. except ImportError:
  22. iscoroutinefunction = None
  23. try:
  24. import tornado
  25. except ImportError:
  26. tornado = None
  27. import sys
  28. import threading
  29. import typing as t
  30. import warnings
  31. from abc import ABCMeta, abstractmethod
  32. from concurrent import futures
  33. import six
  34. from tenacity import _utils
  35. # Import all built-in retry strategies for easier usage.
  36. from .retry import retry_base # noqa
  37. from .retry import retry_all # noqa
  38. from .retry import retry_always # noqa
  39. from .retry import retry_any # noqa
  40. from .retry import retry_if_exception # noqa
  41. from .retry import retry_if_exception_type # noqa
  42. from .retry import retry_if_not_result # noqa
  43. from .retry import retry_if_result # noqa
  44. from .retry import retry_never # noqa
  45. from .retry import retry_unless_exception_type # noqa
  46. from .retry import retry_if_exception_message # noqa
  47. from .retry import retry_if_not_exception_message # noqa
  48. # Import all nap strategies for easier usage.
  49. from .nap import sleep # noqa
  50. from .nap import sleep_using_event # noqa
  51. # Import all built-in stop strategies for easier usage.
  52. from .stop import stop_after_attempt # noqa
  53. from .stop import stop_after_delay # noqa
  54. from .stop import stop_all # noqa
  55. from .stop import stop_any # noqa
  56. from .stop import stop_never # noqa
  57. from .stop import stop_when_event_set # noqa
  58. # Import all built-in wait strategies for easier usage.
  59. from .wait import wait_chain # noqa
  60. from .wait import wait_combine # noqa
  61. from .wait import wait_exponential # noqa
  62. from .wait import wait_fixed # noqa
  63. from .wait import wait_incrementing # noqa
  64. from .wait import wait_none # noqa
  65. from .wait import wait_random # noqa
  66. from .wait import wait_random_exponential # noqa
  67. from .wait import wait_random_exponential as wait_full_jitter # noqa
  68. # Import all built-in before strategies for easier usage.
  69. from .before import before_log # noqa
  70. from .before import before_nothing # noqa
  71. # Import all built-in after strategies for easier usage.
  72. from .after import after_log # noqa
  73. from .after import after_nothing # noqa
  74. # Import all built-in after strategies for easier usage.
  75. from .before_sleep import before_sleep_log # noqa
  76. from .before_sleep import before_sleep_nothing # noqa
  77. WrappedFn = t.TypeVar("WrappedFn", bound=t.Callable)
  78. @t.overload
  79. def retry(fn):
  80. # type: (WrappedFn) -> WrappedFn
  81. """Type signature for @retry as a raw decorator."""
  82. pass
  83. @t.overload
  84. def retry(*dargs, **dkw): # noqa
  85. # type: (...) -> t.Callable[[WrappedFn], WrappedFn]
  86. """Type signature for the @retry() decorator constructor."""
  87. pass
  88. def retry(*dargs, **dkw): # noqa
  89. """Wrap a function with a new `Retrying` object.
  90. :param dargs: positional arguments passed to Retrying object
  91. :param dkw: keyword arguments passed to the Retrying object
  92. """
  93. # support both @retry and @retry() as valid syntax
  94. if len(dargs) == 1 and callable(dargs[0]):
  95. return retry()(dargs[0])
  96. else:
  97. def wrap(f):
  98. if isinstance(f, retry_base):
  99. warnings.warn(
  100. (
  101. "Got retry_base instance ({cls}) as callable argument, "
  102. + "this will probably hang indefinitely (did you mean "
  103. + "retry={cls}(...)?)"
  104. ).format(cls=f.__class__.__name__)
  105. )
  106. if iscoroutinefunction is not None and iscoroutinefunction(f):
  107. r = AsyncRetrying(*dargs, **dkw)
  108. elif (
  109. tornado
  110. and hasattr(tornado.gen, "is_coroutine_function")
  111. and tornado.gen.is_coroutine_function(f)
  112. ):
  113. r = TornadoRetrying(*dargs, **dkw)
  114. else:
  115. r = Retrying(*dargs, **dkw)
  116. return r.wraps(f)
  117. return wrap
  118. class TryAgain(Exception):
  119. """Always retry the executed function when raised."""
  120. NO_RESULT = object()
  121. class DoAttempt(object):
  122. pass
  123. class DoSleep(float):
  124. pass
  125. class BaseAction(object):
  126. """Base class for representing actions to take by retry object.
  127. Concrete implementations must define:
  128. - __init__: to initialize all necessary fields
  129. - REPR_ATTRS: class variable specifying attributes to include in repr(self)
  130. - NAME: for identification in retry object methods and callbacks
  131. """
  132. REPR_FIELDS = ()
  133. NAME = None
  134. def __repr__(self):
  135. state_str = ", ".join(
  136. "%s=%r" % (field, getattr(self, field)) for field in self.REPR_FIELDS
  137. )
  138. return "%s(%s)" % (type(self).__name__, state_str)
  139. def __str__(self):
  140. return repr(self)
  141. class RetryAction(BaseAction):
  142. REPR_FIELDS = ("sleep",)
  143. NAME = "retry"
  144. def __init__(self, sleep):
  145. self.sleep = float(sleep)
  146. _unset = object()
  147. def _first_set(first, second):
  148. return second if first is _unset else first
  149. class RetryError(Exception):
  150. """Encapsulates the last attempt instance right before giving up."""
  151. def __init__(self, last_attempt):
  152. self.last_attempt = last_attempt
  153. super(RetryError, self).__init__(last_attempt)
  154. def reraise(self):
  155. if self.last_attempt.failed:
  156. raise self.last_attempt.result()
  157. raise self
  158. def __str__(self):
  159. return "{0}[{1}]".format(self.__class__.__name__, self.last_attempt)
  160. class AttemptManager(object):
  161. """Manage attempt context."""
  162. def __init__(self, retry_state):
  163. self.retry_state = retry_state
  164. def __enter__(self):
  165. pass
  166. def __exit__(self, exc_type, exc_value, traceback):
  167. if isinstance(exc_value, BaseException):
  168. self.retry_state.set_exception((exc_type, exc_value, traceback))
  169. return True # Swallow exception.
  170. else:
  171. # We don't have the result, actually.
  172. self.retry_state.set_result(None)
  173. class BaseRetrying(object):
  174. __metaclass__ = ABCMeta
  175. def __init__(
  176. self,
  177. sleep=sleep,
  178. stop=stop_never,
  179. wait=wait_none(),
  180. retry=retry_if_exception_type(),
  181. before=before_nothing,
  182. after=after_nothing,
  183. before_sleep=None,
  184. reraise=False,
  185. retry_error_cls=RetryError,
  186. retry_error_callback=None,
  187. ):
  188. self.sleep = sleep
  189. self.stop = stop
  190. self.wait = wait
  191. self.retry = retry
  192. self.before = before
  193. self.after = after
  194. self.before_sleep = before_sleep
  195. self.reraise = reraise
  196. self._local = threading.local()
  197. self.retry_error_cls = retry_error_cls
  198. self.retry_error_callback = retry_error_callback
  199. # This attribute was moved to RetryCallState and is deprecated on
  200. # Retrying objects but kept for backward compatibility.
  201. self.fn = None
  202. def copy(
  203. self,
  204. sleep=_unset,
  205. stop=_unset,
  206. wait=_unset,
  207. retry=_unset,
  208. before=_unset,
  209. after=_unset,
  210. before_sleep=_unset,
  211. reraise=_unset,
  212. retry_error_cls=_unset,
  213. retry_error_callback=_unset,
  214. ):
  215. """Copy this object with some parameters changed if needed."""
  216. return self.__class__(
  217. sleep=_first_set(sleep, self.sleep),
  218. stop=_first_set(stop, self.stop),
  219. wait=_first_set(wait, self.wait),
  220. retry=_first_set(retry, self.retry),
  221. before=_first_set(before, self.before),
  222. after=_first_set(after, self.after),
  223. before_sleep=_first_set(before_sleep, self.before_sleep),
  224. reraise=_first_set(reraise, self.reraise),
  225. retry_error_cls=_first_set(retry_error_cls, self.retry_error_cls),
  226. retry_error_callback=_first_set(
  227. retry_error_callback, self.retry_error_callback
  228. ),
  229. )
  230. def __repr__(self):
  231. attrs = dict(
  232. _utils.visible_attrs(self, attrs={"me": id(self)}),
  233. __class__=self.__class__.__name__,
  234. )
  235. return (
  236. "<%(__class__)s object at 0x%(me)x (stop=%(stop)s, "
  237. "wait=%(wait)s, sleep=%(sleep)s, retry=%(retry)s, "
  238. "before=%(before)s, after=%(after)s)>"
  239. ) % (attrs)
  240. @property
  241. def statistics(self):
  242. """Return a dictionary of runtime statistics.
  243. This dictionary will be empty when the controller has never been
  244. ran. When it is running or has ran previously it should have (but
  245. may not) have useful and/or informational keys and values when
  246. running is underway and/or completed.
  247. .. warning:: The keys in this dictionary **should** be some what
  248. stable (not changing), but there existence **may**
  249. change between major releases as new statistics are
  250. gathered or removed so before accessing keys ensure that
  251. they actually exist and handle when they do not.
  252. .. note:: The values in this dictionary are local to the thread
  253. running call (so if multiple threads share the same retrying
  254. object - either directly or indirectly) they will each have
  255. there own view of statistics they have collected (in the
  256. future we may provide a way to aggregate the various
  257. statistics from each thread).
  258. """
  259. try:
  260. return self._local.statistics
  261. except AttributeError:
  262. self._local.statistics = {}
  263. return self._local.statistics
  264. def wraps(self, f):
  265. """Wrap a function for retrying.
  266. :param f: A function to wraps for retrying.
  267. """
  268. @_utils.wraps(f)
  269. def wrapped_f(*args, **kw):
  270. return self(f, *args, **kw)
  271. def retry_with(*args, **kwargs):
  272. return self.copy(*args, **kwargs).wraps(f)
  273. wrapped_f.retry = self
  274. wrapped_f.retry_with = retry_with
  275. return wrapped_f
  276. def begin(self, fn):
  277. self.statistics.clear()
  278. self.statistics["start_time"] = _utils.now()
  279. self.statistics["attempt_number"] = 1
  280. self.statistics["idle_for"] = 0
  281. self.fn = fn
  282. def iter(self, retry_state): # noqa
  283. fut = retry_state.outcome
  284. if fut is None:
  285. if self.before is not None:
  286. self.before(retry_state)
  287. return DoAttempt()
  288. is_explicit_retry = retry_state.outcome.failed and isinstance(
  289. retry_state.outcome.exception(), TryAgain
  290. )
  291. if not (is_explicit_retry or self.retry(retry_state=retry_state)):
  292. return fut.result()
  293. if self.after is not None:
  294. self.after(retry_state=retry_state)
  295. self.statistics["delay_since_first_attempt"] = retry_state.seconds_since_start
  296. if self.stop(retry_state=retry_state):
  297. if self.retry_error_callback:
  298. return self.retry_error_callback(retry_state=retry_state)
  299. retry_exc = self.retry_error_cls(fut)
  300. if self.reraise:
  301. raise retry_exc.reraise()
  302. six.raise_from(retry_exc, fut.exception())
  303. if self.wait:
  304. sleep = self.wait(retry_state=retry_state)
  305. else:
  306. sleep = 0.0
  307. retry_state.next_action = RetryAction(sleep)
  308. retry_state.idle_for += sleep
  309. self.statistics["idle_for"] += sleep
  310. self.statistics["attempt_number"] += 1
  311. if self.before_sleep is not None:
  312. self.before_sleep(retry_state=retry_state)
  313. return DoSleep(sleep)
  314. def __iter__(self):
  315. self.begin(None)
  316. retry_state = RetryCallState(self, fn=None, args=(), kwargs={})
  317. while True:
  318. do = self.iter(retry_state=retry_state)
  319. if isinstance(do, DoAttempt):
  320. yield AttemptManager(retry_state=retry_state)
  321. elif isinstance(do, DoSleep):
  322. retry_state.prepare_for_next_attempt()
  323. self.sleep(do)
  324. else:
  325. break
  326. @abstractmethod
  327. def __call__(self, *args, **kwargs):
  328. pass
  329. def call(self, *args, **kwargs):
  330. """Use ``__call__`` instead because this method is deprecated."""
  331. warnings.warn(
  332. "'call()' method is deprecated. " + "Use '__call__()' instead",
  333. DeprecationWarning,
  334. )
  335. return self.__call__(*args, **kwargs)
  336. class Retrying(BaseRetrying):
  337. """Retrying controller."""
  338. def __call__(self, fn, *args, **kwargs):
  339. self.begin(fn)
  340. retry_state = RetryCallState(retry_object=self, fn=fn, args=args, kwargs=kwargs)
  341. while True:
  342. do = self.iter(retry_state=retry_state)
  343. if isinstance(do, DoAttempt):
  344. try:
  345. result = fn(*args, **kwargs)
  346. except BaseException: # noqa: B902
  347. retry_state.set_exception(sys.exc_info())
  348. else:
  349. retry_state.set_result(result)
  350. elif isinstance(do, DoSleep):
  351. retry_state.prepare_for_next_attempt()
  352. self.sleep(do)
  353. else:
  354. return do
  355. class Future(futures.Future):
  356. """Encapsulates a (future or past) attempted call to a target function."""
  357. def __init__(self, attempt_number):
  358. super(Future, self).__init__()
  359. self.attempt_number = attempt_number
  360. @property
  361. def failed(self):
  362. """Return whether a exception is being held in this future."""
  363. return self.exception() is not None
  364. @classmethod
  365. def construct(cls, attempt_number, value, has_exception):
  366. """Construct a new Future object."""
  367. fut = cls(attempt_number)
  368. if has_exception:
  369. fut.set_exception(value)
  370. else:
  371. fut.set_result(value)
  372. return fut
  373. class RetryCallState(object):
  374. """State related to a single call wrapped with Retrying."""
  375. def __init__(self, retry_object, fn, args, kwargs):
  376. #: Retry call start timestamp
  377. self.start_time = _utils.now()
  378. #: Retry manager object
  379. self.retry_object = retry_object
  380. #: Function wrapped by this retry call
  381. self.fn = fn
  382. #: Arguments of the function wrapped by this retry call
  383. self.args = args
  384. #: Keyword arguments of the function wrapped by this retry call
  385. self.kwargs = kwargs
  386. #: The number of the current attempt
  387. self.attempt_number = 1
  388. #: Last outcome (result or exception) produced by the function
  389. self.outcome = None
  390. #: Timestamp of the last outcome
  391. self.outcome_timestamp = None
  392. #: Time spent sleeping in retries
  393. self.idle_for = 0
  394. #: Next action as decided by the retry manager
  395. self.next_action = None
  396. @property
  397. def seconds_since_start(self):
  398. if self.outcome_timestamp is None:
  399. return None
  400. return self.outcome_timestamp - self.start_time
  401. def prepare_for_next_attempt(self):
  402. self.outcome = None
  403. self.outcome_timestamp = None
  404. self.attempt_number += 1
  405. self.next_action = None
  406. def set_result(self, val):
  407. ts = _utils.now()
  408. fut = Future(self.attempt_number)
  409. fut.set_result(val)
  410. self.outcome, self.outcome_timestamp = fut, ts
  411. def set_exception(self, exc_info):
  412. ts = _utils.now()
  413. fut = Future(self.attempt_number)
  414. _utils.capture(fut, exc_info)
  415. self.outcome, self.outcome_timestamp = fut, ts
  416. if iscoroutinefunction:
  417. from tenacity._asyncio import AsyncRetrying
  418. if tornado:
  419. from tenacity.tornadoweb import TornadoRetrying