Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move transitions to enum #35

Open
wants to merge 5 commits into
base: actor
Choose a base branch
from
Open
Changes from 4 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
109 changes: 77 additions & 32 deletions jumpstarter/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import anyio
import transitions
from anyio.abc import Event
from anyio.abc import Event as EventType
from transitions import EventData
from transitions.core import _LOGGER, MachineError, listify
from transitions.extensions import GraphMachine
Expand Down Expand Up @@ -34,6 +34,34 @@
NestedState.separator = "↦"


class Transition(str, Enum):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are triggers, not transitions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️😆

def _generate_next_value_(name: str, *args) -> str:
name = name.replace("__", NestedState.separator)
return name.lower()

INITIALIZE = auto()
thedrow marked this conversation as resolved.
Show resolved Hide resolved
PAUSE = auto()
RECOVER = auto()
REPORT_ERROR = auto()
REPORT_PROBLEM = auto()
REPORT_WARNING = auto()
RESTART = auto()
RESTARTING__STARTING = auto()
RESTARTING__STOPPING = auto()
RESUME = auto()
START = auto()
STOP = auto()


class Event(str, Enum):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're using the wrong terminology here.
It could be because my design isn't 100% correct as of yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should rename that enum to ActorLifecycleEvent.
That would be more helpful.

def _generate_next_value_(name: str, *args) -> str:
name = name.replace("__", NestedState.separator)
return f"{name.lower()}_event"

BOOTUP = auto()
SHUTDOWN = auto()


# region Enums


Expand Down Expand Up @@ -178,35 +206,41 @@ def __init__(
)

self.add_transition(
"restart",
Transition.RESTART,
restart_state.ignore,
restart_state.restarting,
after="restart",
after=Transition.RESTART,
conditions=self._can_restart,
)
self.add_transition(
"restart",
Transition.RESTART,
restart_state.restarting.value.stopping,
restart_state.restarting.value.starting,
after="restart",
after=Transition.RESTART,
)
self.add_transition(
"restart", restart_state.restarting.value.starting, restart_state.restarted
Transition.RESTART,
restart_state.restarting.value.starting,
restart_state.restarted,
)

self.add_transition(
"restart",
Transition.RESTART,
restart_state.restarted,
restart_state.restarting,
after="restart",
after=Transition.RESTART,
conditions=self._can_restart,
)

self.on_enter("restarting↦stopping", self._stop_and_wait_for_completion)
self.on_enter("restarting↦starting", self._start_and_wait_for_completion)
self.on_enter(
Transition.RESTARTING__STOPPING.value, self._stop_and_wait_for_completion
)
self.on_enter(
Transition.RESTARTING__STARTING.value, self._start_and_wait_for_completion
)

async def _stop_and_wait_for_completion(self, event_data: EventData) -> None:
shutdown_event: Event = anyio.create_event()
shutdown_event: EventType = anyio.create_event()

async with anyio.create_task_group() as task_group:
await task_group.spawn(
Expand All @@ -215,7 +249,7 @@ async def _stop_and_wait_for_completion(self, event_data: EventData) -> None:
await task_group.spawn(shutdown_event.wait)

async def _start_and_wait_for_completion(self, event_data: EventData) -> None:
bootup_event: Event = anyio.create_event()
bootup_event: EventType = anyio.create_event()

async with anyio.create_task_group() as task_group:
await task_group.spawn(
Expand Down Expand Up @@ -296,42 +330,50 @@ def register_parallel_state_machine(self, machine: BaseStateMachine) -> None:
# region Protected API

def _create_crashed_transitions(self, actor_state):
self.add_transition("report_error", "*", actor_state.crashed)
self.add_transition(Transition.REPORT_ERROR, "*", actor_state.crashed)
self.add_transition(
"stop", actor_state.crashed, actor_state.stopping, after="stop"
Transition.STOP,
actor_state.crashed,
actor_state.stopping,
after=Transition.STOP,
)
self.add_transition(
"start", actor_state.crashed, actor_state.starting, after="start"
Transition.START,
actor_state.crashed,
actor_state.starting,
after=Transition.START,
)

def _create_started_substates_transitions(self, actor_state):
self.add_transition(
"pause", actor_state.started.value.running, actor_state.started.value.paused
Transition.PAUSE,
actor_state.started.value.running,
actor_state.started.value.paused,
)
self.add_transition(
"resume",
Transition.RESUME,
actor_state.started.value.paused,
actor_state.started.value.running.value.healthy,
)

self.add_transition(
"recover",
Transition.RECOVER,
[
actor_state.started.value.running.value.degraded,
actor_state.started.value.running.value.unhealthy,
],
actor_state.started.value.running.value.healthy,
)
self.add_transition(
"report_warning",
Transition.REPORT_WARNING,
[
actor_state.started.value.running.value.healthy,
actor_state.started.value.running.value.unhealthy,
],
actor_state.started.value.running.value.degraded,
)
self.add_transition(
"report_problem",
Transition.REPORT_PROBLEM,
[
actor_state.started.value.running.value.degraded,
actor_state.started.value.running.value.healthy,
Expand All @@ -341,7 +383,10 @@ def _create_started_substates_transitions(self, actor_state):

def _create_restart_transitions(self, actor_state):
self.add_transition(
"start", actor_state.stopped, actor_state.starting, after="start"
Transition.START,
actor_state.stopped,
actor_state.starting,
after=Transition.START,
)

def _create_shutdown_transitions(self, actor_state):
Expand All @@ -353,27 +398,27 @@ def _create_shutdown_transitions(self, actor_state):
actor_state.stopping.value.resources_released,
actor_state.stopping.value.dependencies_stopped,
],
trigger="stop",
trigger=Transition.STOP,
loop=False,
after="stop",
after=Transition.STOP,
)
self.add_transition(
"stop",
Transition.STOP,
actor_state.stopping.value.dependencies_stopped,
actor_state.stopped,
after=partial(_maybe_set_event, event_name="shutdown_event"),
after=partial(_maybe_set_event, event_name=Event.SHUTDOWN),
)

transition = self.get_transitions(
"stop",
Transition.STOP,
actor_state.stopping.value.tasks_stopped,
actor_state.stopping.value.resources_released,
)[0]
transition.before.append(_release_resources)

def _create_bootup_transitions(self, actor_state):
self.add_transition(
"initialize", actor_state.initializing, actor_state.initialized
Transition.INITIALIZE, actor_state.initializing, actor_state.initialized
)

self.add_ordered_transitions(
Expand All @@ -385,15 +430,15 @@ def _create_bootup_transitions(self, actor_state):
actor_state.starting.value.resources_acquired,
actor_state.starting.value.tasks_started,
],
trigger="start",
trigger=Transition.START,
loop=False,
after="start",
after=Transition.START,
)
self.add_transition(
"start",
Transition.START,
actor_state.starting.value.tasks_started,
actor_state.started,
after=partial(_maybe_set_event, event_name="bootup_event"),
after=partial(_maybe_set_event, event_name=Event.BOOTUP),
)

# endregion
Expand All @@ -409,7 +454,7 @@ async def _release_resources(event_data: transitions.EventData) -> None:
async def _maybe_set_event(event_data: EventData, event_name: str) -> None:
kwargs = _merge_event_data_kwargs(event_data)
try:
event: Event = kwargs[event_name]
event: EventType = kwargs[event_name]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's definitely not correct. This is an AnyIO event.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed the anyio.Event at import to not conflict with the Event enum. It seemed to me that the having a variable Event.bootup and Event.shutdown was more useful than the type annotation. By changing the name of the annotation at import, mypy will still work, and IDEs will still prompt you to use the correct value. It just avoids the name collision.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from anyio.abc import Event as EventType

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should rename that enum to ActorLifecycleEvent.
That would be more helpful.

await event.set()
except KeyError:
pass
Expand Down