|
5 | 5 | import math |
6 | 6 | import re |
7 | 7 | from dataclasses import dataclass, field |
8 | | -from typing import TYPE_CHECKING |
| 8 | +from typing import TYPE_CHECKING, TypeVar |
9 | 9 |
|
10 | 10 | from aws_durable_execution_sdk_python.config import Duration, JitterStrategy |
| 11 | +from aws_durable_execution_sdk_python.exceptions import SuspendExecution |
| 12 | + |
11 | 13 |
|
12 | 14 | if TYPE_CHECKING: |
13 | 15 | from collections.abc import Callable |
14 | 16 |
|
| 17 | + from aws_durable_execution_sdk_python.config import ChildConfig |
| 18 | + from aws_durable_execution_sdk_python.types import DurableContext |
| 19 | + |
| 20 | +T = TypeVar("T") |
| 21 | + |
15 | 22 | Numeric = int | float |
16 | 23 |
|
17 | 24 | # Default pattern that matches all error messages |
@@ -172,3 +179,92 @@ def critical(cls) -> Callable[[Exception, int], RetryDecision]: |
172 | 179 | jitter_strategy=JitterStrategy.NONE, |
173 | 180 | ) |
174 | 181 | ) |
| 182 | + |
| 183 | + |
| 184 | +@dataclass(frozen=True) |
| 185 | +class WithRetryConfig: |
| 186 | + """Configuration for with_retry. |
| 187 | +
|
| 188 | + Wraps the existing RetryStrategyConfig (same config used for step |
| 189 | + retries) and adds execution-mode options specific to with_retry. |
| 190 | +
|
| 191 | + Attributes: |
| 192 | + retry_strategy_config: RetryStrategyConfig controlling retry |
| 193 | + behavior (max_attempts, initial_delay, backoff_rate, jitter, |
| 194 | + error filtering). The same config used for step retries. |
| 195 | + wrap_with_run_in_child_context: Whether to wrap the retry loop in |
| 196 | + a child context for isolation. Default True. |
| 197 | + child_context_config: Optional ChildConfig forwarded to |
| 198 | + run_in_child_context when wrapping is enabled. Ignored when |
| 199 | + wrap_with_run_in_child_context is False. |
| 200 | + """ |
| 201 | + |
| 202 | + retry_strategy_config: RetryStrategyConfig |
| 203 | + wrap_with_run_in_child_context: bool = True |
| 204 | + child_context_config: ChildConfig | None = None |
| 205 | + |
| 206 | + |
| 207 | +def with_retry( |
| 208 | + context: DurableContext, |
| 209 | + func: Callable[[DurableContext, int], T], |
| 210 | + config: WithRetryConfig, |
| 211 | + name: str | None = None, |
| 212 | +) -> T: |
| 213 | + """Retry a block of durable logic with configurable backoff. |
| 214 | +
|
| 215 | + Semantically a run_in_child_context with a retry policy wrapped around |
| 216 | + it — on failure the whole function body is re-run from the beginning |
| 217 | + with configurable backoff. |
| 218 | +
|
| 219 | + Unlike context.step() which retries a single atomic operation, |
| 220 | + with_retry retries an entire function body that may contain multiple |
| 221 | + durable operations (steps, waits, invokes, callbacks, etc.). |
| 222 | +
|
| 223 | + Uses the existing RetryStrategyConfig (via WithRetryConfig), so retry |
| 224 | + configuration is consistent across the SDK. |
| 225 | +
|
| 226 | + Args: |
| 227 | + context: The DurableContext to execute within. |
| 228 | + func: A callable that accepts (DurableContext, attempt: int) and |
| 229 | + returns T. The function body may contain multiple durable |
| 230 | + operations. |
| 231 | + config: WithRetryConfig containing a RetryStrategyConfig plus |
| 232 | + execution-mode options. |
| 233 | + name: Optional name for the child context and backoff waits. |
| 234 | + When provided, backoff waits are named |
| 235 | + "{name}-backoff-{attempt}". |
| 236 | +
|
| 237 | + Returns: |
| 238 | + The result of func on successful execution. |
| 239 | +
|
| 240 | + Raises: |
| 241 | + The exception from the last failed attempt when retries are |
| 242 | + exhausted or the retry strategy returns should_retry=False. |
| 243 | + SuspendExecution: Re-raised immediately (SDK control flow). |
| 244 | + """ |
| 245 | + retry_strategy = create_retry_strategy(config.retry_strategy_config) |
| 246 | + |
| 247 | + def run_loop(ctx: DurableContext) -> T: |
| 248 | + attempt = 0 |
| 249 | + while True: |
| 250 | + attempt += 1 |
| 251 | + try: |
| 252 | + return func(ctx, attempt) |
| 253 | + except SuspendExecution: |
| 254 | + raise # SDK control flow - never intercept |
| 255 | + except Exception as err: |
| 256 | + decision = retry_strategy(err, attempt) |
| 257 | + if not decision.should_retry: |
| 258 | + raise |
| 259 | + wait_name = f"{name}-backoff-{attempt}" if name else None |
| 260 | + print("Going to wait " + wait_name + " " + str(err)) |
| 261 | + ctx.wait(duration=decision.delay, name=wait_name) |
| 262 | + |
| 263 | + if config.wrap_with_run_in_child_context: |
| 264 | + return context.run_in_child_context( |
| 265 | + run_loop, |
| 266 | + name=name, |
| 267 | + config=config.child_context_config, |
| 268 | + ) |
| 269 | + else: |
| 270 | + return run_loop(context) |
0 commit comments