Skip to content
Merged
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
20 changes: 20 additions & 0 deletions simpleflow/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,23 @@ def __repr__(self):

def propagate_attribute(self, attr, val):
setattr(self, attr, val)


class TaskActivity(Activity):
"""
One task's attributes over a shared Activity: attributes the task did not
set read through to `shared`.
"""

def __init__(self, shared: Activity) -> None:
self.shared = shared

def __getattr__(self, name: str) -> Any:
try:
shared = vars(self)["shared"]
except KeyError:
raise AttributeError(name) from None
return getattr(shared, name)

def __repr__(self):
return f"TaskActivity({self.shared!r})"
19 changes: 16 additions & 3 deletions simpleflow/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from simpleflow.utils import import_from_module

from . import futures, logger
from .activity import Activity
from .activity import Activity, TaskActivity

if TYPE_CHECKING:
from typing import Any
Expand Down Expand Up @@ -137,8 +137,11 @@ def execute(self):

def propagate_attribute(self, attr, val):
"""
Propagate to the activity.
Propagate to this task's own view of the activity: the Activity itself
is shared by every task built from it.
"""
if getattr(self, "_own_activity", None) is not self.activity:
self.activity = self._own_activity = TaskActivity(self.activity)
setattr(self.activity, attr, val)


Expand Down Expand Up @@ -194,8 +197,18 @@ def execute(self) -> Any:

def propagate_attribute(self, attr: str, val: Any) -> None:
"""
Propagate to the workflow.
Propagate to a subclass owned by this task: the workflow class itself
is shared by every task built from it. The subclass keeps the class's
module and name, so the child workflow type is unchanged.
"""
if getattr(self, "_own_workflow", None) is not self.workflow:
shared = self.workflow
namespace = {
"__module__": shared.__module__,
"__qualname__": shared.__qualname__,
"__doc__": shared.__doc__,
}
self.workflow = self._own_workflow = type(shared)(shared.__name__, (shared,), namespace)
setattr(self.workflow, attr, val)


Expand Down
72 changes: 71 additions & 1 deletion tests/test_simpleflow/swf/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
from unittest import mock

from simpleflow import activity, exceptions, format, futures
from simpleflow.canvas import Group
from simpleflow.swf.executor import Executor
from simpleflow.swf.mapper.models.history import builder
from simpleflow.swf.mapper.responses import Response
from simpleflow.task import ActivityTask, ChildWorkflowTask
from tests.data.activities import increment, raise_on_failure
from tests.data.constants import DOMAIN
from tests.data.constants import DEFAULT_VERSION, DOMAIN
from tests.data.workflows import BaseTestWorkflow
from tests.utils import MockSWFTestCase

Expand Down Expand Up @@ -435,6 +437,74 @@ def test_a_refused_propagated_signal_is_sent_again_only_if_rate_limited(self):
assert self.replay_propagation(history) == ([decision] if sent_again else [])


@activity.with_attributes(version=DEFAULT_VERSION, raises_on_failure=True)
def mandatory_step(x):
return x


class MandatoryChildWorkflow(BaseTestWorkflow):
name = "mandatory_child_workflow"

def run(self):
pass


class OptionalUsesWorkflow(BaseTestWorkflow):
def run(self):
group = Group(
ActivityTask(mandatory_step, 1),
ChildWorkflowTask(MandatoryChildWorkflow),
raises_on_failure=False,
)
futures.wait(self.submit(group))


class MandatoryStepWorkflow(BaseTestWorkflow):
def run(self):
futures.wait(self.submit(mandatory_step, 2))


class MandatoryChildUseWorkflow(BaseTestWorkflow):
def run(self):
futures.wait(self.submit(MandatoryChildWorkflow))


def replay_after_a_failure(workflow):
history = builder.History(workflow, input={})
(decision,) = replay_decisions(workflow, history)
history.add_decision_task_completed()
if decision["decisionType"] == "ScheduleActivityTask":
attributes = decision["scheduleActivityTaskDecisionAttributes"]
history.add_activity_task(
mandatory_step, decision_id=history.last_id, last_state="failed", activity_id=attributes["activityId"]
)
else:
attributes = decision["startChildWorkflowExecutionDecisionAttributes"]
history.add_child_workflow(MandatoryChildWorkflow, last_state="failed", workflow_id=attributes["workflowId"])
history.add_decision_task_scheduled()
history.add_decision_task_started()
(closing,) = replay_decisions(workflow, history)
return closing["decisionType"]


class TestFailureSemanticsStayOnTheTask(unittest.TestCase):
def test_a_mandatory_activity_failure_fails_the_workflow(self):
assert replay_after_a_failure(MandatoryStepWorkflow) == "FailWorkflowExecution"

def test_an_optional_use_elsewhere_leaves_the_activity_mandatory(self):
replay_decisions(OptionalUsesWorkflow, builder.History(OptionalUsesWorkflow, input={}))
assert mandatory_step.raises_on_failure is True
assert replay_after_a_failure(MandatoryStepWorkflow) == "FailWorkflowExecution"

def test_a_mandatory_child_workflow_failure_fails_the_workflow(self):
assert replay_after_a_failure(MandatoryChildUseWorkflow) == "FailWorkflowExecution"

def test_an_optional_use_elsewhere_leaves_the_child_workflow_mandatory(self):
replay_decisions(OptionalUsesWorkflow, builder.History(OptionalUsesWorkflow, input={}))
assert MandatoryChildWorkflow.raises_on_failure is True
assert replay_after_a_failure(MandatoryChildUseWorkflow) == "FailWorkflowExecution"


@activity.with_attributes(raises_on_failure=True)
def print_me_n_times(s, n, raises=False):
if raises:
Expand Down
55 changes: 54 additions & 1 deletion tests/test_simpleflow/test_task.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from simpleflow import Workflow, activity, registry, task
from simpleflow import Workflow, activity, canvas, registry, task


@activity.with_attributes(task_list="test")
Expand Down Expand Up @@ -66,3 +66,56 @@ def test_child_workflow_task_forwards_explicit_task_list():
assert wf_task._task_list == "my_list"
assert wf_task.args == [1]
assert wf_task.kwargs == {"foo": "bar"}


@activity.with_attributes(task_list="test", raises_on_failure=True)
def shared(x):
return x


class SharedChildWorkflow(Workflow):
name = "shared_child_workflow"


def test_group_attributes_land_on_the_task_not_the_activity():
a_task = task.ActivityTask(shared, 1)
canvas.Group(a_task, raises_on_failure=False)
a_task.propagate_attribute("group_tag", "a")
assert a_task.activity.raises_on_failure is False
assert a_task.activity.group_tag == "a"
assert a_task.activity.name == shared.name
assert shared.raises_on_failure is True
assert not hasattr(shared, "group_tag")
assert a_task.activity.shared is shared


def test_tasks_of_one_activity_keep_their_own_attributes():
optional, mandatory = task.ActivityTask(shared, 1), task.ActivityTask(shared, 2)
optional.propagate_attribute("raises_on_failure", False)
mandatory.propagate_attribute("raises_on_failure", True)
assert optional.activity.raises_on_failure is False
assert mandatory.activity.raises_on_failure is True


def test_a_later_change_to_the_activity_reaches_the_task():
a_task = task.ActivityTask(shared, 1)
a_task.propagate_attribute("raises_on_failure", False)
shared.meta = {"binaries": {"tool": "v2"}}
try:
assert a_task.activity.meta == {"binaries": {"tool": "v2"}}
finally:
shared.meta = {}


def test_group_attributes_land_on_the_child_workflow_task_not_the_class():
child = task.ChildWorkflowTask(SharedChildWorkflow)
canvas.Group(child, raises_on_failure=False)
assert child.workflow.raises_on_failure is False
assert SharedChildWorkflow.raises_on_failure is True
assert issubclass(child.workflow, SharedChildWorkflow)
assert (child.workflow.__module__, child.workflow.__name__, child.workflow.__qualname__) == (
SharedChildWorkflow.__module__,
SharedChildWorkflow.__name__,
SharedChildWorkflow.__qualname__,
)
assert child.name == "workflow-shared_child_workflow"