Skip to content

Commit 57204b6

Browse files
authored
Clean up API a bit (#97)
- **Update example to new code** - **Update component selector example** - **Remove now unnessecary check & parameter** - **Make dispatch receiving less chatty/verbose**
2 parents e7fd295 + 2b7f7fd commit 57204b6

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-16
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Two properties have been replaced by methods that require a type as parameter.
1010
* `Dispatcher.lifecycle_events` has been replaced by the method `Dispatcher.new_lifecycle_events_receiver(self, dispatch_type: str)`.
1111
* `Dispatcher.running_status_change` has been replaced by the method `Dispatcher.new_running_state_event_receiver(self, dispatch_type: str, unify_running_intervals: bool)`.
12+
* The managing actor constructor no longer requires the `dispatch_type` parameter. Instead you're expected to pass the type to the new-receiver function.
1213

1314
## New Features
1415

src/frequenz/dispatch/_bg_service.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,18 @@ async def _fetch(self) -> None:
259259
self._dispatches[dispatch.id] = Dispatch(client_dispatch)
260260
old_dispatch = old_dispatches.pop(dispatch.id, None)
261261
if not old_dispatch:
262-
_logger.info("New dispatch: %s", dispatch)
262+
_logger.debug("New dispatch: %s", dispatch)
263263
await self._update_dispatch_schedule_and_notify(dispatch, None)
264264
await self._lifecycle_events_tx.send(Created(dispatch=dispatch))
265265
elif dispatch.update_time != old_dispatch.update_time:
266-
_logger.info("Updated dispatch: %s", dispatch)
266+
_logger.debug("Updated dispatch: %s", dispatch)
267267
await self._update_dispatch_schedule_and_notify(
268268
dispatch, old_dispatch
269269
)
270270
await self._lifecycle_events_tx.send(Updated(dispatch=dispatch))
271271

272+
_logger.info("Received %s dispatches", len(self._dispatches))
273+
272274
except grpc.aio.AioRpcError as error:
273275
_logger.error("Error fetching dispatches: %s", error)
274276
self._dispatches = old_dispatches

src/frequenz/dispatch/_managing_actor.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ async def _run(self) -> None:
6262
6363
def set_components(self, components: TargetComponents) -> None:
6464
match components:
65-
case [int(), *_] as component_ids:
66-
print("Dispatch: Setting components to %s", components)
65+
case []:
66+
print("Dispatch: Using all components")
67+
case list() as ids if isinstance(ids[0], int):
68+
component_ids = ids
6769
case [ComponentCategory.BATTERY, *_]:
68-
print("Dispatch: Using all battery components")
70+
component_category = ComponentCategory.BATTERY
6971
case unsupported:
7072
print(
71-
"Dispatch: Requested an unsupported target component %r, "
73+
"Dispatch: Requested an unsupported selector %r, "
7274
"but only component IDs or category BATTERY are supported.",
7375
unsupported,
7476
)
@@ -91,11 +93,10 @@ async def run():
9193
# Start actor and give it an dispatch updates channel receiver
9294
my_actor = MyActor(dispatch_updates_channel.new_receiver())
9395
94-
status_receiver = dispatcher.running_status_change.new_receiver()
96+
status_receiver = dispatcher.new_running_state_event_receiver("EXAMPLE_TYPE")
9597
9698
managing_actor = DispatchManagingActor(
9799
actor=my_actor,
98-
dispatch_type="EXAMPLE",
99100
running_status_receiver=status_receiver,
100101
updates_sender=dispatch_updates_channel.new_sender(),
101102
)
@@ -107,15 +108,13 @@ async def run():
107108
def __init__(
108109
self,
109110
actor: Actor | Set[Actor],
110-
dispatch_type: str,
111111
running_status_receiver: Receiver[Dispatch],
112112
updates_sender: Sender[DispatchUpdate] | None = None,
113113
) -> None:
114114
"""Initialize the dispatch handler.
115115
116116
Args:
117117
actor: A set of actors or a single actor to manage.
118-
dispatch_type: The type of dispatches to handle.
119118
running_status_receiver: The receiver for dispatch running status changes.
120119
updates_sender: The sender for dispatch events
121120
"""
@@ -124,7 +123,6 @@ def __init__(
124123
self._actors: frozenset[Actor] = frozenset(
125124
[actor] if isinstance(actor, Actor) else actor
126125
)
127-
self._dispatch_type = dispatch_type
128126
self._updates_sender = updates_sender
129127

130128
def _start_actors(self) -> None:
@@ -158,10 +156,6 @@ async def _handle_dispatch(self, dispatch: Dispatch) -> None:
158156
Args:
159157
dispatch: The dispatch to handle.
160158
"""
161-
if dispatch.type != self._dispatch_type:
162-
_logger.debug("Ignoring dispatch %s", dispatch.id)
163-
return
164-
165159
if dispatch.started:
166160
if self._updates_sender is not None:
167161
_logger.info("Updated by dispatch %s", dispatch.id)

tests/test_mananging_actor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ async def test_env() -> AsyncIterator[TestEnv]:
7272

7373
runner_actor = DispatchManagingActor(
7474
actor=actor,
75-
dispatch_type="UNIT_TEST",
7675
running_status_receiver=channel.new_receiver(),
7776
updates_sender=updates_channel.new_sender(),
7877
)

0 commit comments

Comments
 (0)