|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2024 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""A highlevel interface for the dispatch API.""" |
| 5 | + |
| 6 | +import abc |
| 7 | +from typing import Protocol, TypeVar |
| 8 | + |
| 9 | +import grpc.aio |
| 10 | +from frequenz.channels import Broadcast, Receiver |
| 11 | +from frequenz.client.dispatch.types import Dispatch |
| 12 | + |
| 13 | +from frequenz.dispatch._event import DispatchEvent |
| 14 | +from frequenz.dispatch.actor import DispatchingActor |
| 15 | + |
| 16 | +ReceivedT = TypeVar("ReceivedT") |
| 17 | +"""The type being received.""" |
| 18 | + |
| 19 | + |
| 20 | +class ReceiverFetcher(Protocol[ReceivedT]): |
| 21 | + """An interface that just exposes a `new_receiver` method.""" |
| 22 | + |
| 23 | + @abc.abstractmethod |
| 24 | + def new_receiver( |
| 25 | + self, name: str | None = None, maxsize: int = 50 |
| 26 | + ) -> Receiver[ReceivedT]: |
| 27 | + """Get a receiver from the channel. |
| 28 | +
|
| 29 | + Args: |
| 30 | + name: A name to identify the receiver in the logs. |
| 31 | + maxsize: The maximum size of the receiver. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + A receiver instance. |
| 35 | + """ |
| 36 | + |
| 37 | + |
| 38 | +class Dispatcher: |
| 39 | + """A highlevel interface for the dispatch API. |
| 40 | +
|
| 41 | + This class provides a highlevel interface to the dispatch API. |
| 42 | + It provides two channels: |
| 43 | +
|
| 44 | + One that sends a dispatch event message whenever a dispatch is created, updated or deleted. |
| 45 | +
|
| 46 | + The other sends a dispatch message whenever a dispatch is ready to be |
| 47 | + executed according to the schedule. |
| 48 | +
|
| 49 | + allows to receive new dispatches and ready dispatches. |
| 50 | +
|
| 51 | + Example: Processing ready-to-execute dispatches |
| 52 | + ```python |
| 53 | + import grpc.aio |
| 54 | +
|
| 55 | + async def run(): |
| 56 | + grpc_channel = grpc.aio.insecure_channel("localhost:50051") |
| 57 | + microgrid_id = 1 |
| 58 | + service_address = "localhost:50051" |
| 59 | +
|
| 60 | + dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address) |
| 61 | + dispatcher.start() # this will start the actor |
| 62 | +
|
| 63 | + ready_receiver = dispatcher.ready_to_execute.new_receiver() |
| 64 | +
|
| 65 | + async for dispatch in ready_receiver: |
| 66 | + print(f"Executing dispatch {dispatch.id}, due on {dispatch.start_time}") |
| 67 | + # execute the dispatch |
| 68 | + ``` |
| 69 | +
|
| 70 | + Example: Getting notification about dispatch lifecycle events |
| 71 | + ```python |
| 72 | + from typing import assert_never |
| 73 | +
|
| 74 | + import grpc.aio |
| 75 | + from frequenz.dispatch import Created, Deleted, Dispatcher, Updated |
| 76 | +
|
| 77 | +
|
| 78 | + async def run(): |
| 79 | + grpc_channel = grpc.aio.insecure_channel("localhost:50051") |
| 80 | + microgrid_id = 1 |
| 81 | + service_address = "localhost:50051" |
| 82 | + dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address) |
| 83 | + dispatcher.start() # this will start the actor |
| 84 | +
|
| 85 | + events_receiver = dispatcher.lifecycle_events.new_receiver() |
| 86 | +
|
| 87 | + async for event in events_receiver: |
| 88 | + match event: |
| 89 | + case Created(dispatch): |
| 90 | + print(f"A dispatch was created: {dispatch}") |
| 91 | + case Deleted(dispatch): |
| 92 | + print(f"A dispatch was deleted: {dispatch}") |
| 93 | + case Updated(dispatch): |
| 94 | + print(f"A dispatch was updated: {dispatch}") |
| 95 | + case _ as unhandled: |
| 96 | + assert_never(unhandled) |
| 97 | + ``` |
| 98 | + """ |
| 99 | + |
| 100 | + def __init__( |
| 101 | + self, microgrid_id: int, grpc_channel: grpc.aio.Channel, svc_addr: str |
| 102 | + ): |
| 103 | + """Initialize the dispatcher. |
| 104 | +
|
| 105 | + Args: |
| 106 | + microgrid_id: The microgrid id. |
| 107 | + grpc_channel: The gRPC channel. |
| 108 | + svc_addr: The service address. |
| 109 | + """ |
| 110 | + self._ready_channel = Broadcast[Dispatch]("ready_dispatches") |
| 111 | + self._updated_channel = Broadcast[DispatchEvent]("new_dispatches") |
| 112 | + self._actor = DispatchingActor( |
| 113 | + microgrid_id, |
| 114 | + grpc_channel, |
| 115 | + svc_addr, |
| 116 | + self._updated_channel.new_sender(), |
| 117 | + self._ready_channel.new_sender(), |
| 118 | + ) |
| 119 | + |
| 120 | + async def start(self) -> None: |
| 121 | + """Start the actor.""" |
| 122 | + self._actor.start() |
| 123 | + |
| 124 | + @property |
| 125 | + def lifecycle_events(self) -> ReceiverFetcher[DispatchEvent]: |
| 126 | + """Return new, updated or deleted dispatches receiver. |
| 127 | +
|
| 128 | + Returns: |
| 129 | + A new receiver for new dispatches. |
| 130 | + """ |
| 131 | + return self._updated_channel |
| 132 | + |
| 133 | + @property |
| 134 | + def ready_to_execute(self) -> ReceiverFetcher[Dispatch]: |
| 135 | + """Return ready dispatches receiver. |
| 136 | +
|
| 137 | + Returns: |
| 138 | + A new receiver for ready dispatches. |
| 139 | + """ |
| 140 | + return self._ready_channel |
0 commit comments