Skip to content

Commit 1a15050

Browse files
committed
Use enum for the values returned by is_running()
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent fbb9f21 commit 1a15050

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

src/frequenz/dispatch/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
"""A highlevel interface for the dispatch API."""
55

6-
from ._dispatch import Dispatch
6+
from ._dispatch import Dispatch, RunningState
77
from ._dispatcher import Dispatcher, ReceiverFetcher
88
from ._event import Created, Deleted, DispatchEvent, Updated
99

@@ -15,4 +15,5 @@
1515
"ReceiverFetcher",
1616
"Updated",
1717
"Dispatch",
18+
"RunningState",
1819
]

src/frequenz/dispatch/_dispatch.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
from dataclasses import dataclass
99
from datetime import datetime, timezone
10+
from enum import Enum
1011
from typing import Iterator, cast
1112

1213
from dateutil import rrule
@@ -37,6 +38,19 @@
3738
"""To map from our Weekday enum to the dateutil library enum."""
3839

3940

41+
class RunningState(Enum):
42+
"""The running state of a dispatch."""
43+
44+
RUNNING = "RUNNING"
45+
"""The dispatch is running."""
46+
47+
STOPPED = "STOPPED"
48+
"""The dispatch is stopped."""
49+
50+
DIFFERENT_TYPE = "DIFFERENT_TYPE"
51+
"""The dispatch is for a different type."""
52+
53+
4054
@dataclass(frozen=True)
4155
class Dispatch(BaseDispatch):
4256
"""Dispatch type with extra functionality."""
@@ -85,26 +99,28 @@ def set_running_status_notified(self) -> None:
8599
"""Mark the latest running state change notification as sent."""
86100
object.__setattr__(self, "running_state_change_synced", self.update_time)
87101

88-
def running(self, type_: str) -> bool:
102+
def running(self, type_: str) -> RunningState:
89103
"""Check if the dispatch is currently supposed to be running.
90104
91105
Args:
92106
type_: The type of the dispatch that should be running.
93107
94108
Returns:
95-
True if the dispatch is currently meant to be running, False otherwise.
109+
RUNNING if the dispatch is running,
110+
STOPPED if it is stopped,
111+
DIFFERENT_TYPE if it is for a different type.
96112
"""
97113
if self.type != type_:
98-
return False
114+
return RunningState.DIFFERENT_TYPE
99115

100116
if not self.active or self.deleted:
101-
return False
117+
return RunningState.STOPPED
102118

103119
now = datetime.now(tz=timezone.utc)
104120
if until := self._until(now):
105-
return now < until
121+
return RunningState.RUNNING if now < until else RunningState.STOPPED
106122

107-
return False
123+
return RunningState.STOPPED
108124

109125
@property
110126
def until(self) -> datetime | None:

src/frequenz/dispatch/_dispatcher.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,29 @@ async def run():
7070
changed_running_status_rx = dispatcher.running_status_change.new_receiver()
7171
7272
async for dispatch in changed_running_status_rx:
73-
if dispatch.type != "DEMO_TYPE":
74-
continue
75-
76-
print(f"Executing dispatch {dispatch.id}, due on {dispatch.start_time}")
77-
if dispatch.running:
78-
if actor.is_running:
79-
actor.reconfigure(
80-
) # this will reconfigure the actor
81-
else:
82-
# this will start a new or reconfigure a running actor
83-
# and run it for the duration of the dispatch
84-
actor.start_or_reconfigure(
85-
components=dispatch.selector,
86-
run_parameters=dispatch.payload, # custom actor parameters
87-
dry_run=dispatch.dry_run,
88-
until=dispatch.until,
89-
)
90-
else:
91-
actor.stop() # this will stop the actor
73+
match dispatch.running("DEMO_TYPE"):
74+
case True:
75+
print(f"Executing dispatch {dispatch.id}, due on {dispatch.start_time}")
76+
if actor.is_running:
77+
actor.reconfigure(
78+
components=dispatch.selector,
79+
run_parameters=dispatch.payload, # custom actor parameters
80+
dry_run=dispatch.dry_run,
81+
until=dispatch.until,
82+
) # this will reconfigure the actor
83+
else:
84+
# this will start a new actor with the given components
85+
# and run it for the duration of the dispatch
86+
actor.start(
87+
components=dispatch.selector,
88+
run_parameters=dispatch.payload, # custom actor parameters
89+
dry_run=dispatch.dry_run,
90+
until=dispatch.until,
91+
)
92+
case False:
93+
actor.stop() # this will stop the actor
94+
case None:
95+
pass # dispatch not for this type
9296
```
9397
9498
Example: Getting notification about dispatch lifecycle events

src/frequenz/dispatch/actor.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from frequenz.client.dispatch import Client
1414
from frequenz.sdk.actor import Actor
1515

16-
from ._dispatch import Dispatch
16+
from ._dispatch import Dispatch, RunningState
1717
from ._event import Created, Deleted, DispatchEvent, Updated
1818

1919
_MAX_AHEAD_SCHEDULE = timedelta(hours=5)
@@ -220,7 +220,10 @@ def _running_state_change(
220220
# Deleted dispatch
221221
if updated_dispatch is None:
222222
assert previous_dispatch is not None
223-
return previous_dispatch.running(previous_dispatch.type)
223+
return (
224+
previous_dispatch.running(previous_dispatch.type)
225+
== RunningState.RUNNING
226+
)
224227

225228
# If any of the runtime attributes changed, we need to send a message
226229
runtime_state_attributes = [

0 commit comments

Comments
 (0)