-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnested_invoke.py
More file actions
52 lines (39 loc) · 1.52 KB
/
nested_invoke.py
File metadata and controls
52 lines (39 loc) · 1.52 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
"""Example demonstrating nested chained invokes (invoke calling invoke)."""
from typing import Any
from aws_durable_execution_sdk_python.context import DurableContext
from aws_durable_execution_sdk_python.execution import durable_execution
@durable_execution
def handler(_event: Any, context: DurableContext) -> dict:
"""Parent function that invokes a child which invokes another child."""
result = context.invoke(
function_name="orchestrator",
payload={"value": 5},
name="invoke_orchestrator",
)
return {"final_result": result}
@durable_execution
def orchestrator_handler(event: dict, context: DurableContext) -> dict:
"""Middle function that invokes the worker."""
value = event.get("value", 0)
# First invoke: add 10
added = context.invoke(
function_name="adder",
payload={"value": value, "add": 10},
name="invoke_adder",
)
# Second invoke: multiply by 2
multiplied = context.invoke(
function_name="multiplier",
payload={"value": added["result"]},
name="invoke_multiplier",
)
return {"result": multiplied["result"], "steps": ["add_10", "multiply_2"]}
def adder_handler(event: dict, context: Any) -> dict:
"""Leaf handler that adds values."""
value = event.get("value", 0)
add = event.get("add", 0)
return {"result": value + add}
def multiplier_handler(event: dict, context: Any) -> dict:
"""Leaf handler that multiplies by 2."""
value = event.get("value", 0)
return {"result": value * 2}