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
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## Upgrading

<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
The `frequenz.disptach.TargetComponents` tye was removed, use `frequenz.client.dispatch.TargetComponents` instead.

## New Features

Expand Down
3 changes: 1 addition & 2 deletions src/frequenz/dispatch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

"""

from ._actor_dispatcher import ActorDispatcher, DispatchInfo, TargetComponents
from ._actor_dispatcher import ActorDispatcher, DispatchInfo
from ._bg_service import MergeStrategy
from ._dispatch import Dispatch
from ._dispatcher import Dispatcher
Expand All @@ -34,5 +34,4 @@
"MergeStrategy",
"MergeByType",
"MergeByTypeTarget",
"TargetComponents",
]
24 changes: 3 additions & 21 deletions src/frequenz/dispatch/_actor_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,18 @@
from collections.abc import Callable
from dataclasses import dataclass
from datetime import timedelta
from typing import Any, Awaitable, cast
from typing import Any, Awaitable

from frequenz.channels import Broadcast, Receiver, Sender, select
from frequenz.channels.timer import SkipMissedAndDrift, Timer
from frequenz.client.common.microgrid.components import ComponentCategory, ComponentId
from frequenz.client.dispatch.types import DispatchId
from frequenz.client.dispatch.types import TargetComponents as ClientTargetComponents
from frequenz.client.dispatch.types import DispatchId, TargetComponents
from frequenz.core.id import BaseId
from frequenz.sdk.actor import Actor, BackgroundService

from ._dispatch import Dispatch

_logger = logging.getLogger(__name__)

TargetComponents = list[ComponentId] | list[ComponentCategory]
"""One or more target components specifying which components a dispatch targets.

It can be a list of component IDs or a list of categories.
"""


class DispatchActorId(BaseId, str_prefix="DA"):
"""ID for a dispatch actor."""
Expand Down Expand Up @@ -208,7 +200,7 @@ def start(self) -> None:
async def _start_actor(self, dispatch: Dispatch) -> None:
"""Start the actor the given dispatch refers to."""
dispatch_update = DispatchInfo(
components=_convert_target_components(dispatch.target),
components=dispatch.target,
dry_run=dispatch.dry_run,
options=dispatch.payload,
_src=dispatch,
Expand Down Expand Up @@ -305,13 +297,3 @@ async def _handle_dispatch(self, dispatch: Dispatch) -> None:
await self._start_actor(dispatch)
else:
await self._stop_actor(dispatch, "Dispatch stopped")


def _convert_target_components(target: ClientTargetComponents) -> TargetComponents:
if all(isinstance(comp, int) for comp in target):
# We've confirmed all elements are integers, so we can cast.
int_components = cast(list[int], target)
return [ComponentId(cid) for cid in int_components]
# If not all are ints, then it must be a list of ComponentCategory
# based on the definition of ClientTargetComponents.
return cast(list[ComponentCategory], target)
11 changes: 5 additions & 6 deletions tests/test_managing_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@
MergeByTypeTarget,
MergeStrategy,
)
from frequenz.dispatch._actor_dispatcher import (
DispatchActorId,
_convert_target_components,
)
from frequenz.dispatch._actor_dispatcher import DispatchActorId
from frequenz.dispatch._bg_service import DispatchScheduler


Expand Down Expand Up @@ -180,7 +177,9 @@ async def test_simple_start_stop(

event = test_env.actor(1).initial_dispatch
assert event.options == {"test": True}
assert event.components == [ComponentId(1), ComponentId(10), ComponentId(15)]
assert event.components == TargetIds(
ComponentId(1), ComponentId(10), ComponentId(15)
)
assert event.dry_run is False

assert test_env.actor(1).is_running is True
Expand Down Expand Up @@ -310,7 +309,7 @@ async def test_dry_run(test_env: _TestEnv, fake_time: time_machine.Coordinates)
event = test_env.actor(1).initial_dispatch

assert event.dry_run is dispatch.dry_run
assert event.components == _convert_target_components(dispatch.target)
assert event.components == dispatch.target
assert event.options == dispatch.payload
assert test_env.actor(1).is_running is True

Expand Down
Loading