-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: actor
Are you sure you want to change the base?
Changes from 4 commits
f3390a1
4ceb452
4bba3ac
caa4caf
779b95a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -34,6 +34,34 @@ | |
NestedState.separator = "↦" | ||
|
||
|
||
class Transition(str, Enum): | ||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we're using the wrong terminology here. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 f"{name.lower()}_event" | ||
|
||
BOOTUP = auto() | ||
SHUTDOWN = auto() | ||
|
||
|
||
# region Enums | ||
|
||
|
||
|
@@ -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( | ||
|
@@ -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( | ||
|
@@ -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, | ||
|
@@ -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): | ||
|
@@ -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( | ||
|
@@ -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 | ||
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's definitely not correct. This is an AnyIO event. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I renamed the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should rename that enum to |
||
await event.set() | ||
except KeyError: | ||
pass | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♂️😆