forked from frequenz-floss/frequenz-dispatch-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_dispatcher.py
More file actions
261 lines (209 loc) · 9.49 KB
/
_dispatcher.py
File metadata and controls
261 lines (209 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# License: MIT
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
"""A highlevel interface for the dispatch API."""
from frequenz.channels import Receiver
from frequenz.client.dispatch import Client
from ._bg_service import DispatchScheduler, MergeStrategy
from ._dispatch import Dispatch
from ._event import DispatchEvent
class Dispatcher:
"""A highlevel interface for the dispatch API.
This class provides a highlevel interface to the dispatch API.
It provides two receiver functions:
* [Lifecycle events receiver][frequenz.dispatch.Dispatcher.new_lifecycle_events_receiver]:
Receives an event whenever a dispatch is created, updated or deleted.
* [Running status change
receiver][frequenz.dispatch.Dispatcher.new_running_state_event_receiver]:
Receives an event whenever the running status of a dispatch changes.
The running status of a dispatch can change due to a variety of reasons,
such as but not limited to the dispatch being started, stopped, modified
or deleted or reaching its scheduled start or end time.
Any change that could potentially require the consumer to start, stop or
reconfigure itself will cause a message to be sent.
Example: Processing running state change dispatches
```python
import os
from frequenz.dispatch import Dispatcher
from unittest.mock import MagicMock
async def run():
url = os.getenv("DISPATCH_API_URL", "grpc://fz-0004.frequenz.io:50051")
key = os.getenv("DISPATCH_API_KEY", "some-key")
microgrid_id = 1
dispatcher = Dispatcher(
microgrid_id=microgrid_id,
server_url=url,
key=key
)
await dispatcher.start()
actor = MagicMock() # replace with your actor
changed_running_status = dispatcher.new_running_state_event_receiver("DISPATCH_TYPE")
async for dispatch in changed_running_status:
if dispatch.started:
print(f"Executing dispatch {dispatch.id}, due on {dispatch.start_time}")
if actor.is_running:
actor.reconfigure(
components=dispatch.target,
run_parameters=dispatch.payload, # custom actor parameters
dry_run=dispatch.dry_run,
until=dispatch.until,
) # this will reconfigure the actor
else:
# this will start a new actor with the given components
# and run it for the duration of the dispatch
actor.start(
components=dispatch.target,
run_parameters=dispatch.payload, # custom actor parameters
dry_run=dispatch.dry_run,
until=dispatch.until,
)
else:
actor.stop() # this will stop the actor
```
Example: Getting notification about dispatch lifecycle events
```python
import os
from typing import assert_never
from frequenz.dispatch import Created, Deleted, Dispatcher, Updated
async def run():
url = os.getenv("DISPATCH_API_URL", "grpc://fz-0004.frequenz.io:50051")
key = os.getenv("DISPATCH_API_KEY", "some-key")
microgrid_id = 1
dispatcher = Dispatcher(
microgrid_id=microgrid_id,
server_url=url,
key=key
)
await dispatcher.start() # this will start the actor
events_receiver = dispatcher.new_lifecycle_events_receiver("DISPATCH_TYPE")
async for event in events_receiver:
match event:
case Created(dispatch):
print(f"A dispatch was created: {dispatch}")
case Deleted(dispatch):
print(f"A dispatch was deleted: {dispatch}")
case Updated(dispatch):
print(f"A dispatch was updated: {dispatch}")
case _ as unhandled:
assert_never(unhandled)
```
Example: Creating a new dispatch and then modifying it.
Note that this uses the lower-level `Client` class to create and update the dispatch.
```python
import os
from datetime import datetime, timedelta, timezone
from frequenz.client.common.microgrid.components import ComponentCategory
from frequenz.dispatch import Dispatcher
async def run():
url = os.getenv("DISPATCH_API_URL", "grpc://fz-0004.frequenz.io:50051")
key = os.getenv("DISPATCH_API_KEY", "some-key")
microgrid_id = 1
dispatcher = Dispatcher(
microgrid_id=microgrid_id,
server_url=url,
key=key
)
await dispatcher.start() # this will start the actor
# Create a new dispatch
new_dispatch = await dispatcher.client.create(
microgrid_id=microgrid_id,
type="ECHO_FREQUENCY", # replace with your own type
start_time=datetime.now(tz=timezone.utc) + timedelta(minutes=10),
duration=timedelta(minutes=5),
target=ComponentCategory.INVERTER,
payload={"font": "Times New Roman"}, # Arbitrary payload data
)
# Modify the dispatch
await dispatcher.client.update(
microgrid_id=microgrid_id,
dispatch_id=new_dispatch.id,
new_fields={"duration": timedelta(minutes=10)}
)
# Validate the modification
modified_dispatch = await dispatcher.client.get(
microgrid_id=microgrid_id, dispatch_id=new_dispatch.id
)
assert modified_dispatch.duration == timedelta(minutes=10)
```
"""
def __init__(
self,
*,
microgrid_id: int,
server_url: str,
key: str,
):
"""Initialize the dispatcher.
Args:
microgrid_id: The microgrid id.
server_url: The URL of the dispatch service.
key: The key to access the service.
"""
self._client = Client(server_url=server_url, key=key)
self._bg_service = DispatchScheduler(
microgrid_id,
self._client,
)
async def start(self) -> None:
"""Start the local dispatch service."""
self._bg_service.start()
@property
def client(self) -> Client:
"""Return the client."""
return self._client
def new_lifecycle_events_receiver(
self, dispatch_type: str
) -> Receiver[DispatchEvent]:
"""Return new, updated or deleted dispatches receiver.
Args:
dispatch_type: The type of the dispatch to listen for.
Returns:
A new receiver for new dispatches.
"""
return self._bg_service.new_lifecycle_events_receiver(dispatch_type)
async def new_running_state_event_receiver(
self,
dispatch_type: str,
*,
merge_strategy: MergeStrategy | None = None,
) -> Receiver[Dispatch]:
"""Return running state event receiver.
This receiver will receive a message whenever the current running
status of a dispatch changes.
Usually, one message per scheduled run is to be expected.
However, things get complicated when a dispatch was modified:
If it was currently running and the modification now says
it should not be running or running with different parameters,
then a message will be sent.
In other words: Any change that is expected to make an actor start, stop
or adjust itself according to new dispatch options causes a message to be
sent.
A non-exhaustive list of possible changes that will cause a message to be sent:
- The normal scheduled start_time has been reached
- The duration of the dispatch has been modified
- The start_time has been modified to be in the future
- The component selection changed
- The active status changed
- The dry_run status changed
- The payload changed
- The dispatch was deleted
`merge_strategy` is an instance of a class derived from
[`MergeStrategy`][frequenz.dispatch.MergeStrategy] Available strategies
are:
* [`MergeByType`][frequenz.dispatch.MergeByType] — merges all dispatches
of the same type
* [`MergeByTypeTarget`][frequenz.dispatch.MergeByTypeTarget] — merges all
dispatches of the same type and target
* `None` — no merging, just send all events (default)
Running intervals from multiple dispatches will be merged, according to
the chosen strategy.
While merging, stop events are ignored as long as at least one
merge-criteria-matching dispatch remains active.
Args:
dispatch_type: The type of the dispatch to listen for.
merge_strategy: The type of the strategy to merge running intervals.
Returns:
A new receiver for dispatches whose running status changed.
"""
return await self._bg_service.new_running_state_event_receiver(
dispatch_type, merge_strategy=merge_strategy
)