Skip to content

Commit e693f1f

Browse files
committed
Update child-workflows example
1 parent 7eb70b7 commit e693f1f

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

child_workflows/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ requires-python = ">=3.10,<3.13"
77
readme = "README.md"
88
dependencies = [
99
"pydantic>=2.10.6",
10-
"restack-ai==0.0.62",
1110
"watchfiles>=1.0.4",
1211
"python-dotenv==1.0.1",
12+
"restack-ai>=0.0.78",
1313
]
1414

1515
[project.scripts]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from restack_ai.function import function, log
1+
from restack_ai.function import function, log, NonRetryableError
22

33
@function.defn(name="welcome")
44
async def welcome(function_input: str) -> str:
@@ -7,4 +7,4 @@ async def welcome(function_input: str) -> str:
77
return f"Hello, {function_input}!"
88
except Exception as e:
99
log.error("welcome function failed", error=e)
10-
raise e
10+
raise NonRetryableError(f"Welcome function failed: {e}") from e

child_workflows/src/workflows/child.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import timedelta
22
from pydantic import BaseModel
3-
from restack_ai.workflow import workflow, import_functions, log
3+
from restack_ai.workflow import NonRetryableError, workflow, import_functions, log
44
with import_functions():
55
from src.functions.function import welcome
66

@@ -16,6 +16,15 @@ class ChildWorkflow:
1616
@workflow.run
1717
async def run(self, workflow_input: ChildInput) -> ChildOutput:
1818
log.info("ChildWorkflow started")
19-
result = await workflow.step(function=welcome, function_input=workflow_input.name, start_to_close_timeout=timedelta(seconds=120))
20-
log.info("ChildWorkflow completed", result=result)
21-
return ChildOutput(result=result)
19+
try:
20+
result = await workflow.step(
21+
function=welcome,
22+
function_input=workflow_input.name,
23+
start_to_close_timeout=timedelta(seconds=120)
24+
)
25+
except Exception as e:
26+
error_message = f"Error during welcome: {e}"
27+
raise NonRetryableError(error_message) from e
28+
else:
29+
log.info("ChildWorkflow completed", result=result)
30+
return ChildOutput(result=result)

child_workflows/src/workflows/parent.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from restack_ai.workflow import workflow, log, workflow_info
1+
from restack_ai.workflow import workflow, log, workflow_info, NonRetryableError
22
from pydantic import BaseModel
33
from .child import ChildWorkflow, ChildInput
44

@@ -22,9 +22,18 @@ async def run(self, workflow_input: ParentInput) -> ParentOutput:
2222
# result = await workflow.child_start(ChildWorkflow, input=ChildInput(name="world"), workflow_id=f"{parent_workflow_id}-child-start")
2323

2424
log.info("Start ChildWorkflow and wait for result")
25-
result = await workflow.child_execute(workflow=ChildWorkflow, workflow_input=ChildInput(name="world"), workflow_id=f"{parent_workflow_id}-child-execute")
26-
log.info("ChildWorkflow completed", result=result)
27-
return ParentOutput(result="ParentWorkflow completed")
25+
try:
26+
result = await workflow.child_execute(
27+
workflow=ChildWorkflow,
28+
workflow_input=ChildInput(name="world"),
29+
workflow_id=f"{parent_workflow_id}-child-execute",
30+
)
31+
except Exception as e:
32+
error_message = f"Error during child_execute: {e}"
33+
raise NonRetryableError(error_message) from e
34+
else:
35+
log.info("ChildWorkflow completed", result=result)
36+
return ParentOutput(result="ParentWorkflow completed")
2837

2938
else:
3039
log.info("ParentWorkflow without starting or executing child workflow")

0 commit comments

Comments
 (0)