Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion simpleflow/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from . import futures
from .activity import Activity
from .base import Submittable, SubmittableContainer
from .task import ActivityTask, WorkflowTask
from .task import ActivityTask, ChildWorkflowTask, WorkflowTask


class FuncGroup(SubmittableContainer):
Expand Down Expand Up @@ -225,6 +225,17 @@ def __init__(
# ActivityTask.args is ignored when building swf.ActivityTask (#247)
args = [*a.args, previous_result]
a = ActivityTask(a.activity, *args, **a.kwargs)
elif isinstance(a, WorkflowTask):
# WorkflowTask.args is ignored when building swf.WorkflowTask
args = [*a.args, previous_result]
task_cls = WorkflowTask if isinstance(a, ChildWorkflowTask) else type(a)
a = task_cls(
a.executor,
a.workflow,
*args,
workflow_task_list=a._task_list,
**a.kwargs,
)
else:
a.args.append(previous_result)

Expand Down
60 changes: 60 additions & 0 deletions tests/test_simpleflow/test_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from simpleflow.constants import HOUR, MINUTE
from simpleflow.exceptions import AggregateException, TaskFailed
from simpleflow.local.executor import Executor
from simpleflow.swf.executor import Executor as SwfExecutor
from simpleflow.swf.mapper.models import Domain
from simpleflow.swf.mapper.models.history import builder
from simpleflow.swf.mapper.responses import Response
from simpleflow.task import ActivityTask, ChildWorkflowTask


Expand Down Expand Up @@ -260,6 +264,62 @@ def test_propagate_attribute(self):
self.assertFalse(inner_a.activities[0].activity.raises_on_failure)
self.assertFalse(inner_a.activities[1].activity.raises_on_failure)

def test_send_result_to_child_workflow_on_swf(self):
class Child(MyWorkflow):
name = "child_workflow"

def get_workflow_id(self, *args, **kwargs):
return "child-" + "-".join(map(str, args))

def run(self, *args, **kwargs):
return args

class ParentWithTask(MyWorkflow):
def run(self):
chain = Chain(
(to_int, "42"),
ChildWorkflowTask(Child, "prefix", flag=True, workflow_task_list="custom_tl"),
send_result=True,
)
futures.wait(self.submit(chain))

class ParentWithClass(MyWorkflow):
def run(self):
chain = Chain(
(to_int, "42"),
Child,
send_result=True,
)
futures.wait(self.submit(chain))

domain = Domain("TestDomain")
for wf_cls, expected_args, expected_kwargs, expected_wf_id, expected_tl in (
(ParentWithTask, ["prefix", 42], {"flag": True}, "child-prefix-42", "custom_tl"),
(ParentWithClass, [42], {}, "child-42", "test_task_list"),
):
with self.subTest(workflow=wf_cls.__name__):
history = builder.History(wf_cls, input={})
swf_executor = SwfExecutor(domain, wf_cls)
decisions = swf_executor.replay(Response(history=history, execution=None)).decisions
activity_id = decisions[0]["scheduleActivityTaskDecisionAttributes"]["activityId"]
history.add_decision_task_completed()
history.add_activity_task(
to_int,
decision_id=history.last_id,
last_state="completed",
activity_id=activity_id,
result=42,
)
history.add_decision_task_scheduled()
history.add_decision_task_started()

decisions = swf_executor.replay(Response(history=history, execution=None)).decisions
self.assertEqual(len(decisions), 1)
attrs = decisions[0]["startChildWorkflowExecutionDecisionAttributes"]
self.assertEqual(json.loads(attrs["input"]), {"args": expected_args, "kwargs": expected_kwargs})
self.assertEqual(attrs["workflowId"], expected_wf_id)
self.assertEqual(attrs["taskList"]["name"], expected_tl)


class TestFuncGroup(unittest.TestCase):
def test_previous_value_with_func(self):
Expand Down