Repository navigation
Expand file tree
/
Copy pathprocessing_flow.py
More file actions
238 lines (206 loc) · 7.47 KB
/
Copy pathprocessing_flow.py
File metadata and controls
238 lines (206 loc) · 7.47 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
# Copyright (c) 2022-2026 Super Durable, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Runs one request on one spot instance and reports back to its ControllerFlow."""
from __future__ import annotations
from datetime import timedelta
from typing import TYPE_CHECKING, Callable
from dex import (
AsyncClient,
AsyncContext,
Attribute,
Context,
Flow,
FlowNotActiveOrNotFoundError,
PersistenceSchema,
RPCResult,
Step,
StepDecision,
StepList,
Timer,
Wait,
go_to,
graceful_complete,
rpc,
)
from dex_examples.patterns.resource_control.request import Request
if TYPE_CHECKING:
from dex_examples.patterns.resource_control.controller_flow import ControllerFlow
POLL_INTERVAL = timedelta(seconds=5)
STATUS_VALIDATION_STARTED = "validation started"
STATUS_VALIDATION_COMPLETED = "validation completed"
STATUS_PROCESSING_STARTED = "processing started"
STATUS_PROCESSING_COMPLETED = "gpu processing completed"
class Complete(Step[None]):
def __init__(
self,
client_provider: Callable[[], AsyncClient],
controller_flow_provider: Callable[[], ControllerFlow],
parent_flow_id: Attribute[str],
) -> None:
self.client_provider = client_provider
self.controller_flow_provider = controller_flow_provider
self.parent_flow_id = parent_flow_id
async def execute( # type: ignore[override]
self, context: AsyncContext, input: None
) -> StepDecision:
parent_flow_id = self.parent_flow_id.get(context)
if parent_flow_id:
try:
await self.client_provider().invoke_rpc(
self.controller_flow_provider().complete_child_flow,
parent_flow_id,
context.flow_id,
)
except FlowNotActiveOrNotFoundError:
print(
"Parent flow may have completed, possibly a duplicate "
"completion request, ignoring it."
)
return graceful_complete()
class GpuProcessingComplete(Step[None]):
def __init__(
self,
complete: Complete,
instance_id: Attribute[str],
status: Attribute[str],
) -> None:
self.complete = complete
self.instance_id = instance_id
self.status = status
def wait_for(self, context: Context, input: None) -> Wait:
return Wait.until(Timer.by_duration(POLL_INTERVAL))
def execute(self, context: Context, input: None) -> StepDecision:
print(
"check gpu processing in "
f"{self.instance_id.get(context)} by calling the instance API"
)
self.status.set(context, STATUS_PROCESSING_COMPLETED)
return go_to(Complete, None)
class GpuProcessingStart(Step[None]):
def __init__(
self,
gpu_processing_complete: GpuProcessingComplete,
request: Attribute[Request],
instance_id: Attribute[str],
status: Attribute[str],
) -> None:
self.gpu_processing_complete = gpu_processing_complete
self.request = request
self.instance_id = instance_id
self.status = status
def execute(self, context: Context, input: None) -> StepDecision:
print(
f"start processing of request {self.request.get(context)} in "
f"{self.instance_id.get(context)} by calling the instance API"
)
self.status.set(context, STATUS_PROCESSING_STARTED)
return go_to(GpuProcessingComplete, None)
class ValidationComplete(Step[None]):
def __init__(
self,
gpu_processing_start: GpuProcessingStart,
instance_id: Attribute[str],
status: Attribute[str],
) -> None:
self.gpu_processing_start = gpu_processing_start
self.instance_id = instance_id
self.status = status
def wait_for(self, context: Context, input: None) -> Wait:
return Wait.until(Timer.by_duration(POLL_INTERVAL))
def execute(self, context: Context, input: None) -> StepDecision:
print(
"completed validation in "
f"{self.instance_id.get(context)} by calling the instance API"
)
self.status.set(context, STATUS_VALIDATION_COMPLETED)
return go_to(GpuProcessingStart, None)
class ValidationStart(Step[Request]):
def __init__(
self,
validation_complete: ValidationComplete,
request: Attribute[Request],
instance_id: Attribute[str],
status: Attribute[str],
) -> None:
self.validation_complete = validation_complete
self.request = request
self.instance_id = instance_id
self.status = status
def execute(self, context: Context, input: Request) -> StepDecision:
# Persisting the request keeps later Steps from passing it along as input.
self.request.set(context, input)
print(
f"start validation of request {input} in "
f"{self.instance_id.get(context)} by calling the instance API"
)
self.status.set(context, STATUS_VALIDATION_STARTED)
return go_to(ValidationComplete, None)
class ProcessingFlow(Flow[Request]):
DA_PARENT_FLOW_ID = "ParentFlowId"
DA_STATUS = "Status"
DA_INSTANCE_ID = "InstanceId"
DA_REQUEST = "Request"
parent_flow_id = Attribute(DA_PARENT_FLOW_ID, str)
status = Attribute(DA_STATUS, str)
instance_id = Attribute(DA_INSTANCE_ID, str)
request = Attribute(DA_REQUEST, Request)
def __init__(
self,
client_provider: Callable[[], AsyncClient],
controller_flow_provider: Callable[[], ControllerFlow],
) -> None:
self.complete = Complete(
client_provider,
controller_flow_provider,
self.parent_flow_id,
)
self.gpu_processing_complete = GpuProcessingComplete(
self.complete,
self.instance_id,
self.status,
)
self.gpu_processing_start = GpuProcessingStart(
self.gpu_processing_complete,
self.request,
self.instance_id,
self.status,
)
self.validation_complete = ValidationComplete(
self.gpu_processing_start,
self.instance_id,
self.status,
)
self.validation_start = ValidationStart(
self.validation_complete,
self.request,
self.instance_id,
self.status,
)
def get_steps(self) -> StepList[Request]:
return StepList.start_step(self.validation_start).other_steps(
self.validation_complete,
self.gpu_processing_start,
self.gpu_processing_complete,
self.complete,
)
def get_persistence_schema(self) -> PersistenceSchema:
return PersistenceSchema.of(
self.parent_flow_id,
self.status,
self.instance_id,
self.request,
)
@rpc
def describe(self, context: Context) -> RPCResult[str]:
return RPCResult(self.status.get(context) or "")