|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +Copyright 2026 The Dapr Authors |
| 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 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +""" |
| 15 | + |
| 16 | +from datetime import timedelta |
| 17 | +from typing import Any, Dict, Optional |
| 18 | + |
| 19 | + |
| 20 | +class ActorReminderFailurePolicy: |
| 21 | + """Defines what happens when an actor reminder fails to trigger. |
| 22 | +
|
| 23 | + Use :meth:`drop_policy` to discard failed ticks without retrying, or |
| 24 | + :meth:`constant_policy` to retry at a fixed interval. |
| 25 | +
|
| 26 | + Attributes: |
| 27 | + drop: whether this is a drop (no-retry) policy. |
| 28 | + interval: the retry interval for a constant policy. |
| 29 | + max_retries: the maximum number of retries for a constant policy. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__( |
| 33 | + self, |
| 34 | + *, |
| 35 | + drop: bool = False, |
| 36 | + interval: Optional[timedelta] = None, |
| 37 | + max_retries: Optional[int] = None, |
| 38 | + ): |
| 39 | + """Creates a new :class:`ActorReminderFailurePolicy` instance. |
| 40 | +
|
| 41 | + Args: |
| 42 | + drop (bool): if True, creates a drop policy that discards the reminder |
| 43 | + tick on failure without retrying. Cannot be combined with interval |
| 44 | + or max_retries. |
| 45 | + interval (datetime.timedelta): the retry interval for a constant policy. |
| 46 | + max_retries (int): the maximum number of retries for a constant policy. |
| 47 | + If not set, retries indefinitely. |
| 48 | +
|
| 49 | + Raises: |
| 50 | + ValueError: if drop is combined with interval or max_retries, or if |
| 51 | + neither drop=True nor at least one of interval/max_retries is provided. |
| 52 | + """ |
| 53 | + if drop and (interval is not None or max_retries is not None): |
| 54 | + raise ValueError('drop policy cannot be combined with interval or max_retries') |
| 55 | + if not drop and interval is None and max_retries is None: |
| 56 | + raise ValueError('specify either drop=True or at least one of interval or max_retries') |
| 57 | + self._drop = drop |
| 58 | + self._interval = interval |
| 59 | + self._max_retries = max_retries |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def drop_policy(cls) -> 'ActorReminderFailurePolicy': |
| 63 | + """Returns a policy that drops the reminder tick on failure (no retry).""" |
| 64 | + return cls(drop=True) |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def constant_policy( |
| 68 | + cls, |
| 69 | + interval: Optional[timedelta] = None, |
| 70 | + max_retries: Optional[int] = None, |
| 71 | + ) -> 'ActorReminderFailurePolicy': |
| 72 | + """Returns a policy that retries at a constant interval on failure. |
| 73 | +
|
| 74 | + Args: |
| 75 | + interval (datetime.timedelta): the time between retry attempts. |
| 76 | + max_retries (int): the maximum number of retry attempts. If not set, |
| 77 | + retries indefinitely. |
| 78 | + """ |
| 79 | + return cls(interval=interval, max_retries=max_retries) |
| 80 | + |
| 81 | + @property |
| 82 | + def drop(self) -> bool: |
| 83 | + """Returns True if this is a drop policy.""" |
| 84 | + return self._drop |
| 85 | + |
| 86 | + @property |
| 87 | + def interval(self) -> Optional[timedelta]: |
| 88 | + """Returns the retry interval for a constant policy.""" |
| 89 | + return self._interval |
| 90 | + |
| 91 | + @property |
| 92 | + def max_retries(self) -> Optional[int]: |
| 93 | + """Returns the maximum retries for a constant policy.""" |
| 94 | + return self._max_retries |
| 95 | + |
| 96 | + def as_dict(self) -> Dict[str, Any]: |
| 97 | + """Gets :class:`ActorReminderFailurePolicy` as a dict object.""" |
| 98 | + if self._drop: |
| 99 | + return {'drop': {}} |
| 100 | + d: Dict[str, Any] = {} |
| 101 | + if self._interval is not None: |
| 102 | + d['interval'] = self._interval |
| 103 | + if self._max_retries is not None: |
| 104 | + d['maxRetries'] = self._max_retries |
| 105 | + return {'constant': d} |
0 commit comments