Skip to content

Commit 3f90928

Browse files
committed
Make actor_factory function async for more flexibility
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent c0b9494 commit 3f90928

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

src/frequenz/dispatch/_actor_dispatcher.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import logging
88
from collections.abc import Callable
99
from dataclasses import dataclass
10-
from typing import Any
10+
from typing import Any, Awaitable
1111

1212
from frequenz.channels import Broadcast, Receiver
1313
from frequenz.client.dispatch.types import TargetComponents
@@ -136,7 +136,9 @@ async def main():
136136

137137
def __init__(
138138
self,
139-
actor_factory: Callable[[DispatchInfo, Receiver[DispatchInfo]], Actor],
139+
actor_factory: Callable[
140+
[DispatchInfo, Receiver[DispatchInfo]], Awaitable[Actor]
141+
],
140142
running_status_receiver: Receiver[Dispatch],
141143
dispatch_identity: Callable[[Dispatch], int] | None = None,
142144
) -> None:
@@ -189,7 +191,7 @@ async def _start_actor(self, dispatch: Dispatch) -> None:
189191
else:
190192
try:
191193
_logger.info("Starting actor for dispatch type %r", dispatch.type)
192-
actor = self._actor_factory(
194+
actor = await self._actor_factory(
193195
dispatch_update,
194196
self._updates_channel.new_receiver(limit=1, warn_on_overflow=False),
195197
)

src/frequenz/dispatch/_dispatcher.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import asyncio
99
import logging
1010
from asyncio import Event
11-
from typing import Callable
11+
from typing import Awaitable, Callable
1212

1313
from frequenz.channels import Receiver
1414
from frequenz.client.dispatch import Client
@@ -236,7 +236,9 @@ async def start_dispatching(
236236
self,
237237
dispatch_type: str,
238238
*,
239-
actor_factory: Callable[[DispatchInfo, Receiver[DispatchInfo]], Actor],
239+
actor_factory: Callable[
240+
[DispatchInfo, Receiver[DispatchInfo]], Awaitable[Actor]
241+
],
240242
merge_strategy: MergeStrategy | None = None,
241243
) -> None:
242244
"""Manage actors for a given dispatch type.

tests/test_mananging_actor.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ async def _run(self) -> None:
7878
while True:
7979
await asyncio.sleep(1)
8080

81+
@classmethod
82+
async def create(
83+
cls, initial_dispatch: DispatchInfo, receiver: Receiver[DispatchInfo]
84+
) -> "MockActor":
85+
"""Create a new actor."""
86+
actor = cls(initial_dispatch, receiver)
87+
return actor
88+
8189

8290
@dataclass
8391
class TestEnv:
@@ -101,7 +109,7 @@ async def test_env() -> AsyncIterator[TestEnv]:
101109
channel = Broadcast[Dispatch](name="dispatch ready test channel")
102110

103111
actors_service = ActorDispatcher(
104-
actor_factory=MockActor,
112+
actor_factory=MockActor.create,
105113
running_status_receiver=channel.new_receiver(),
106114
dispatch_identity=lambda dispatch: dispatch.id,
107115
)
@@ -296,14 +304,14 @@ async def new_mock_receiver(
296304
):
297305
await dispatcher.start_dispatching(
298306
dispatch_type="MANAGE_TEST",
299-
actor_factory=MockActor,
307+
actor_factory=MockActor.create,
300308
merge_strategy=strategy,
301309
)
302310

303311
# pylint: disable=protected-access
304312
assert "MANAGE_TEST" in dispatcher._actor_dispatchers
305313
actor_manager = dispatcher._actor_dispatchers["MANAGE_TEST"]
306-
assert actor_manager._actor_factory == MockActor
314+
assert actor_manager._actor_factory == MockActor.create
307315

308316
dispatch = Dispatch(
309317
replace(

0 commit comments

Comments
 (0)