@@ -42,18 +42,20 @@ 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
54
55
```python
55
56
import os
56
57
import grpc.aio
58
+ from frequenz.dispatch import Dispatcher, RunningState
57
59
from unittest.mock import MagicMock
58
60
59
61
async def run():
@@ -97,76 +99,79 @@ async def run():
97
99
```
98
100
99
101
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
- ```
102
+
103
+ ```python
104
+ import os
105
+ from typing import assert_never
106
+
107
+ import grpc.aio
108
+ from frequenz.dispatch import Created, Deleted, Dispatcher, Updated
109
+
110
+ async def run():
111
+ host = os.getenv("DISPATCH_API_HOST", "localhost")
112
+ port = os.getenv("DISPATCH_API_PORT", "50051")
113
+
114
+ service_address = f"{host}:{port}"
115
+ grpc_channel = grpc.aio.insecure_channel(service_address)
116
+ microgrid_id = 1
117
+ dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address)
118
+ dispatcher.start() # this will start the actor
119
+
120
+ events_receiver = dispatcher.lifecycle_events.new_receiver()
121
+
122
+ async for event in events_receiver:
123
+ match event:
124
+ case Created(dispatch):
125
+ print(f"A dispatch was created: {dispatch}")
126
+ case Deleted(dispatch):
127
+ print(f"A dispatch was deleted: {dispatch}")
128
+ case Updated(dispatch):
129
+ print(f"A dispatch was updated: {dispatch}")
130
+ case _ as unhandled:
131
+ assert_never(unhandled)
132
+ ```
133
+
130
134
Example: Creating a new dispatch and then modifying it. Note that this uses
131
135
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
- ```
136
+
137
+ ```python
138
+ import os
139
+ from datetime import datetime, timedelta, timezone
140
+
141
+ import grpc.aio
142
+ from frequenz.client.common.microgrid.components import ComponentCategory
143
+
144
+ from frequenz.dispatch import Dispatcher
145
+
146
+ async def run():
147
+ host = os.getenv("DISPATCH_API_HOST", "localhost")
148
+ port = os.getenv("DISPATCH_API_PORT", "50051")
149
+
150
+ service_address = f"{host}:{port}"
151
+ grpc_channel = grpc.aio.insecure_channel(service_address)
152
+ microgrid_id = 1
153
+ dispatcher = Dispatcher(microgrid_id, grpc_channel, service_address)
154
+ await dispatcher.start() # this will start the actor
155
+
156
+ # Create a new dispatch
157
+ new_dispatch = await dispatcher.client.create(
158
+ microgrid_id=microgrid_id,
159
+ _type="ECHO_FREQUENCY", # replace with your own type
160
+ start_time=datetime.now(tz=timezone.utc) + timedelta(minutes=10),
161
+ duration=timedelta(minutes=5),
162
+ selector=ComponentCategory.INVERTER,
163
+ payload={"font": "Times New Roman"}, # Arbitrary payload data
164
+ )
165
+
166
+ # Modify the dispatch
167
+ await dispatcher.client.update(
168
+ dispatch_id=new_dispatch.id, new_fields={"duration": timedelta(minutes=10)}
169
+ )
170
+
171
+ # Validate the modification
172
+ modified_dispatch = await dispatcher.client.get(new_dispatch.id)
173
+ assert modified_dispatch.duration == timedelta(minutes=10)
174
+ ```
170
175
"""
171
176
172
177
def __init__ (
0 commit comments