|
| 1 | +"""Example demonstrating nested chained invokes (invoke calling invoke).""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from aws_durable_execution_sdk_python.context import DurableContext |
| 6 | +from aws_durable_execution_sdk_python.execution import durable_execution |
| 7 | + |
| 8 | + |
| 9 | +@durable_execution |
| 10 | +def handler(_event: Any, context: DurableContext) -> dict: |
| 11 | + """Parent function that invokes a child which invokes another child.""" |
| 12 | + result = context.invoke( |
| 13 | + function_name="orchestrator", |
| 14 | + payload={"value": 5}, |
| 15 | + name="invoke_orchestrator", |
| 16 | + ) |
| 17 | + return {"final_result": result} |
| 18 | + |
| 19 | + |
| 20 | +@durable_execution |
| 21 | +def orchestrator_handler(event: dict, context: DurableContext) -> dict: |
| 22 | + """Middle function that invokes the worker.""" |
| 23 | + value = event.get("value", 0) |
| 24 | + |
| 25 | + # First invoke: add 10 |
| 26 | + added = context.invoke( |
| 27 | + function_name="adder", |
| 28 | + payload={"value": value, "add": 10}, |
| 29 | + name="invoke_adder", |
| 30 | + ) |
| 31 | + |
| 32 | + # Second invoke: multiply by 2 |
| 33 | + multiplied = context.invoke( |
| 34 | + function_name="multiplier", |
| 35 | + payload={"value": added["result"]}, |
| 36 | + name="invoke_multiplier", |
| 37 | + ) |
| 38 | + |
| 39 | + return {"result": multiplied["result"], "steps": ["add_10", "multiply_2"]} |
| 40 | + |
| 41 | + |
| 42 | +def adder_handler(event: dict, context: Any) -> dict: |
| 43 | + """Leaf handler that adds values.""" |
| 44 | + value = event.get("value", 0) |
| 45 | + add = event.get("add", 0) |
| 46 | + return {"result": value + add} |
| 47 | + |
| 48 | + |
| 49 | +def multiplier_handler(event: dict, context: Any) -> dict: |
| 50 | + """Leaf handler that multiplies by 2.""" |
| 51 | + value = event.get("value", 0) |
| 52 | + return {"result": value * 2} |
0 commit comments