summaryrefslogtreecommitdiff
path: root/src/apscheduler/_retry.py
blob: f05931f555bb67eda970a34e8b8b8741907407ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from __future__ import annotations

import attrs
from attr.validators import instance_of
from tenacity import (
    AsyncRetrying,
    RetryCallState,
    retry_if_exception_type,
    stop_after_delay,
    wait_exponential,
)
from tenacity.stop import stop_base
from tenacity.wait import wait_base


@attrs.define(kw_only=True, frozen=True)
class RetrySettings:
    """
    Settings for retrying an operation with Tenacity.

    :param stop: defines when to stop trying
    :param wait: defines how long to wait between attempts
    """

    stop: stop_base = attrs.field(
        validator=instance_of(stop_base),
        default=stop_after_delay(60),
    )
    wait: wait_base = attrs.field(
        validator=instance_of(wait_base),
        default=wait_exponential(min=0.5, max=20),
    )


@attrs.define(kw_only=True, slots=False)
class RetryMixin:
    """
    Mixin that provides support for retrying operations.

    :param retry_settings: Tenacity settings for retrying operations in case of a
        database connecitivty problem
    """

    retry_settings: RetrySettings = attrs.field(default=RetrySettings())

    @property
    def _temporary_failure_exceptions(self) -> tuple[type[Exception], ...]:
        """
        Tuple of exception classes which indicate that the operation should be retried.

        """
        return ()

    def _retry(self) -> AsyncRetrying:
        def after_attempt(self, retry_state: RetryCallState) -> None:
            self._logger.warning(
                "Temporary data store error (attempt %d): %s",
                retry_state.attempt_number,
                retry_state.outcome.exception(),
            )

        return AsyncRetrying(
            stop=self.retry_settings.stop,
            wait=self.retry_settings.wait,
            retry=retry_if_exception_type(self._temporary_failure_exceptions),
            after=after_attempt,
            reraise=True,
        )