-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathwith_retry_callback.py
More file actions
68 lines (55 loc) · 2.19 KB
/
Copy pathwith_retry_callback.py
File metadata and controls
68 lines (55 loc) · 2.19 KB
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
"""Demonstrates with_retry wrapping a wait_for_callback operation.
The callback may fail multiple times before succeeding. The with_retry helper
retries the entire callback flow (including creating a new callback each attempt)
with exponential backoff between attempts.
"""
from typing import Any
from aws_durable_execution_sdk_python.config import Duration, WaitForCallbackConfig
from aws_durable_execution_sdk_python.context import DurableContext
from aws_durable_execution_sdk_python.execution import durable_execution
from aws_durable_execution_sdk_python.retries import (
RetryStrategyConfig,
WithRetryConfig,
create_retry_strategy,
with_retry,
)
@durable_execution
def handler(_event: Any, context: DurableContext) -> dict[str, Any]:
"""Handler demonstrating with_retry around a wait_for_callback.
The external system may fail to process the callback multiple times.
with_retry will re-create the callback and wait again on each retry,
with exponential backoff between attempts.
"""
def retryable_callback_flow(ctx: DurableContext, attempt: int) -> str:
"""The retryable block: create a callback and wait for the result."""
def submitter(callback_id: str, _callback_ctx) -> None:
"""Submit the callback ID to an external system."""
# In real usage, this would send the callback_id to an external
# system (e.g., via API call, SQS message, etc.)
pass
config = WaitForCallbackConfig(
timeout=Duration.from_seconds(30),
heartbeat_timeout=Duration.from_seconds(60),
)
return ctx.wait_for_callback(
submitter, name=f"external-call-attempt-{attempt}", config=config
)
retry_config = WithRetryConfig(
retry_strategy=create_retry_strategy(
RetryStrategyConfig(
max_attempts=5,
initial_delay=Duration.from_seconds(2),
backoff_rate=1.0,
)
),
)
result = with_retry(
context,
func=retryable_callback_flow,
config=retry_config,
name="callback-with-retry",
)
return {
"success": True,
"result": result,
}