11"""Example demonstrating multiple steps with retry logic."""
22
3- from random import random
3+ import random
44from typing import Any
55
66from aws_durable_execution_sdk_python .config import StepConfig
7- from aws_durable_execution_sdk_python .context import DurableContext
7+ from aws_durable_execution_sdk_python .context import DurableContext , StepContext
88from aws_durable_execution_sdk_python .execution import durable_execution
99from aws_durable_execution_sdk_python .retries import (
1010 RetryStrategyConfig ,
1111 create_retry_strategy ,
1212)
1313
1414
15- def simulated_get_item (name : str ) -> dict [str , Any ] | None :
16- """Simulate getting an item that may fail randomly."""
15+ def simulated_get_item (_step_context : StepContext , name : str ) -> dict [str , Any ] | None :
16+ """Simulate getting an item with deterministic seeded random behavior."""
17+ # Use seeded random for deterministic behavior
1718 # Fail 50% of the time
18- if random () < 0.5 : # noqa: S311
19+ if random . random () < 0.5 : # noqa: S311
1920 msg = "Random failure"
2021 raise RuntimeError (msg )
2122
2223 # Simulate finding item after some attempts
23- if random () > 0.3 : # noqa: S311
24+ if random . random () > 0.3 : # noqa: S311
2425 return {"id" : name , "data" : "item data" }
2526
2627 return None
@@ -29,6 +30,9 @@ def simulated_get_item(name: str) -> dict[str, Any] | None:
2930@durable_execution
3031def handler (event : Any , context : DurableContext ) -> dict [str , Any ]:
3132 """Handler demonstrating polling with retry logic."""
33+ # Seed random for deterministic behavior
34+ random .seed (42 )
35+
3236 name = event .get ("name" , "test-item" )
3337
3438 # Retry configuration for steps
@@ -49,7 +53,7 @@ def handler(event: Any, context: DurableContext) -> dict[str, Any]:
4953
5054 # Try to get the item with retry
5155 get_response = context .step (
52- lambda _ , n = name : simulated_get_item (n ),
56+ lambda _ , n = name : simulated_get_item (_ , n ),
5357 name = f"get_item_poll_{ poll_count } " ,
5458 config = step_config ,
5559 )
0 commit comments