@@ -42,12 +42,13 @@ class Dispatcher:
42
42
This class provides a highlevel interface to the dispatch API.
43
43
It provides two channels:
44
44
45
- One that sends a dispatch event message whenever a dispatch is created, updated or deleted.
45
+ Lifecycle events: A channel that sends a dispatch event message whenever a
46
+ dispatch is created, updated or deleted.
46
47
47
- The other sends a dispatch message whenever a dispatch is ready to be
48
- executed according to the schedule or the running status of the dispatch
49
- changed in a way that could potentially require the actor to start, stop or
50
- reconfigure itself.
48
+ Running status change: Sends a dispatch message whenever a dispatch is ready
49
+ to be executed according to the schedule or the running status of the
50
+ dispatch changed in a way that could potentially require the actor to start,
51
+ stop or reconfigure itself.
51
52
52
53
Example: Processing running state change dispatches
53
54
@@ -97,76 +98,79 @@ async def run():
97
98
```
98
99
99
100
Example: Getting notification about dispatch lifecycle events
100
- ```python
101
- import os
102
- from typing import assert_never
103
-
104
- import grpc.aio
105
- from frequenz.dispatch import Created, Deleted, Dispatcher, Updated
106
-
107
- async def run():
108
- host = os.getenv("DISPATCH_API_HOST", "localhost")
109
- port = os.getenv("DISPATCH_API_PORT", "50051")
110
-
111
- service_address = f"{host}:{port}"
112
- grpc_channel = grpc.aio.insecure_channel(service_address)
113
- microgrid_id = 1
114
- dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address)
115
- dispatcher.start() # this will start the actor
116
-
117
- events_receiver = dispatcher.lifecycle_events.new_receiver()
118
-
119
- async for event in events_receiver:
120
- match event:
121
- case Created(dispatch):
122
- print(f"A dispatch was created: {dispatch}")
123
- case Deleted(dispatch):
124
- print(f"A dispatch was deleted: {dispatch}")
125
- case Updated(dispatch):
126
- print(f"A dispatch was updated: {dispatch}")
127
- case _ as unhandled:
128
- assert_never(unhandled)
129
- ```
101
+
102
+ ```python
103
+ import os
104
+ from typing import assert_never
105
+
106
+ import grpc.aio
107
+ from frequenz.dispatch import Created, Deleted, Dispatcher, Updated
108
+
109
+ async def run():
110
+ host = os.getenv("DISPATCH_API_HOST", "localhost")
111
+ port = os.getenv("DISPATCH_API_PORT", "50051")
112
+
113
+ service_address = f"{host}:{port}"
114
+ grpc_channel = grpc.aio.insecure_channel(service_address)
115
+ microgrid_id = 1
116
+ dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address)
117
+ dispatcher.start() # this will start the actor
118
+
119
+ events_receiver = dispatcher.lifecycle_events.new_receiver()
120
+
121
+ async for event in events_receiver:
122
+ match event:
123
+ case Created(dispatch):
124
+ print(f"A dispatch was created: {dispatch}")
125
+ case Deleted(dispatch):
126
+ print(f"A dispatch was deleted: {dispatch}")
127
+ case Updated(dispatch):
128
+ print(f"A dispatch was updated: {dispatch}")
129
+ case _ as unhandled:
130
+ assert_never(unhandled)
131
+ ```
132
+
130
133
Example: Creating a new dispatch and then modifying it. Note that this uses
131
134
the lower-level `Client` class to create and update the dispatch.
132
- ```python
133
- import os
134
- from datetime import datetime, timedelta, timezone
135
-
136
- import grpc.aio
137
- from frequenz.client.common.microgrid.components import ComponentCategory
138
-
139
- from frequenz.dispatch import Dispatcher
140
-
141
- async def run():
142
- host = os.getenv("DISPATCH_API_HOST", "localhost")
143
- port = os.getenv("DISPATCH_API_PORT", "50051")
144
-
145
- service_address = f"{host}:{port}"
146
- grpc_channel = grpc.aio.insecure_channel(service_address)
147
- microgrid_id = 1
148
- dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address)
149
- await dispatcher.start() # this will start the actor
150
-
151
- # Create a new dispatch
152
- new_dispatch = await dispatcher.client.create(
153
- microgrid_id=microgrid_id,
154
- _type="ECHO_FREQUENCY", # replace with your own type
155
- start_time=datetime.now(tz=timezone.utc) + timedelta(minutes=10),
156
- duration=timedelta(minutes=5),
157
- selector=ComponentCategory.INVERTER,
158
- payload={"font": "Times New Roman"}, # Arbitrary payload data
159
- )
160
-
161
- # Modify the dispatch
162
- await dispatcher.client.update(
163
- dispatch_id=new_dispatch.id, new_fields={"duration": timedelta(minutes=10)}
164
- )
165
-
166
- # Validate the modification
167
- modified_dispatch = await dispatcher.client.get(new_dispatch.id)
168
- assert modified_dispatch.duration == timedelta(minutes=10)
169
- ```
135
+
136
+ ```python
137
+ import os
138
+ from datetime import datetime, timedelta, timezone
139
+
140
+ import grpc.aio
141
+ from frequenz.client.common.microgrid.components import ComponentCategory
142
+
143
+ from frequenz.dispatch import Dispatcher
144
+
145
+ async def run():
146
+ host = os.getenv("DISPATCH_API_HOST", "localhost")
147
+ port = os.getenv("DISPATCH_API_PORT", "50051")
148
+
149
+ service_address = f"{host}:{port}"
150
+ grpc_channel = grpc.aio.insecure_channel(service_address)
151
+ microgrid_id = 1
152
+ dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address)
153
+ await dispatcher.start() # this will start the actor
154
+
155
+ # Create a new dispatch
156
+ new_dispatch = await dispatcher.client.create(
157
+ microgrid_id=microgrid_id,
158
+ _type="ECHO_FREQUENCY", # replace with your own type
159
+ start_time=datetime.now(tz=timezone.utc) + timedelta(minutes=10),
160
+ duration=timedelta(minutes=5),
161
+ selector=ComponentCategory.INVERTER,
162
+ payload={"font": "Times New Roman"}, # Arbitrary payload data
163
+ )
164
+
165
+ # Modify the dispatch
166
+ await dispatcher.client.update(
167
+ dispatch_id=new_dispatch.id, new_fields={"duration": timedelta(minutes=10)}
168
+ )
169
+
170
+ # Validate the modification
171
+ modified_dispatch = await dispatcher.client.get(new_dispatch.id)
172
+ assert modified_dispatch.duration == timedelta(minutes=10)
173
+ ```
170
174
"""
171
175
172
176
def __init__ (
0 commit comments