1
1
import traceback
2
2
import typing
3
3
from dataclasses import dataclass
4
- from typing import Union
4
+ from typing import List , Union
5
5
6
6
from iwf .command_request import _to_idl_command_request
7
7
from iwf .command_results import from_idl_command_results
25
25
from iwf .registry import Registry
26
26
from iwf .search_attributes import SearchAttributes
27
27
from iwf .state_decision import StateDecision , _to_idl_state_decision
28
+ from iwf .state_execution_locals import StateExecutionLocals
28
29
from iwf .utils .iwf_typing import assert_not_unset , unset_to_none
29
30
from iwf .workflow_context import WorkflowContext , _from_idl_context
30
31
from iwf .workflow_state import get_input_type
@@ -99,8 +100,13 @@ def handle_workflow_worker_rpc(
99
100
search_attributes = SearchAttributes (
100
101
search_attributes_types , unset_to_none (request .search_attributes )
101
102
)
103
+ state_execution_locals = StateExecutionLocals (
104
+ to_map (None ), self ._options .object_encoder
105
+ )
102
106
103
- persistence = Persistence (data_attributes , search_attributes )
107
+ persistence = Persistence (
108
+ data_attributes , search_attributes , state_execution_locals
109
+ )
104
110
105
111
communication = Communication (
106
112
internal_channel_types ,
@@ -145,6 +151,9 @@ def handle_workflow_worker_rpc(
145
151
)
146
152
if upsert_sas :
147
153
response .upsert_search_attributes = upsert_sas
154
+ record_events = state_execution_locals .get_record_events ()
155
+ if len (record_events ) > 0 :
156
+ response .record_events = record_events
148
157
if len (communication .get_to_trigger_state_movements ()) > 0 :
149
158
movements = communication .get_to_trigger_state_movements ()
150
159
decision = StateDecision .multi_next_states (* movements )
@@ -188,8 +197,13 @@ def handle_workflow_state_wait_until(
188
197
search_attributes = SearchAttributes (
189
198
search_attributes_types , unset_to_none (request .search_attributes )
190
199
)
200
+ state_execution_locals = StateExecutionLocals (
201
+ to_map (None ), self ._options .object_encoder
202
+ )
191
203
192
- persistence = Persistence (data_attributes , search_attributes )
204
+ persistence = Persistence (
205
+ data_attributes , search_attributes , state_execution_locals
206
+ )
193
207
194
208
communication = Communication (
195
209
internal_channel_types ,
@@ -211,13 +225,20 @@ def handle_workflow_state_wait_until(
211
225
search_attributes .get_upsert_to_server_string_array_attribute_map (),
212
226
)
213
227
228
+ upsert_state_locals = (
229
+ state_execution_locals .get_upsert_state_execution_local_attributes ()
230
+ )
231
+ record_events = state_execution_locals .get_record_events ()
232
+
214
233
response = WorkflowStateWaitUntilResponse (
215
234
command_request = _to_idl_command_request (command_request ),
216
235
publish_to_inter_state_channel = pubs ,
217
236
upsert_data_objects = [
218
237
KeyValue (k , v )
219
238
for (k , v ) in data_attributes .get_updated_values_to_return ().items ()
220
239
],
240
+ upsert_state_locals = upsert_state_locals ,
241
+ record_events = record_events ,
221
242
)
222
243
223
244
if upsert_sas :
@@ -257,8 +278,13 @@ def handle_workflow_state_execute(
257
278
search_attributes = SearchAttributes (
258
279
search_attributes_types , unset_to_none (request .search_attributes )
259
280
)
281
+ state_execution_locals = StateExecutionLocals (
282
+ to_map (request .state_locals ), self ._options .object_encoder
283
+ )
260
284
261
- persistence = Persistence (data_attributes , search_attributes )
285
+ persistence = Persistence (
286
+ data_attributes , search_attributes , state_execution_locals
287
+ )
262
288
263
289
communication = Communication (
264
290
internal_channel_types ,
@@ -288,6 +314,10 @@ def handle_workflow_state_execute(
288
314
search_attributes .get_upsert_to_server_double_attribute_map (),
289
315
search_attributes .get_upsert_to_server_string_array_attribute_map (),
290
316
)
317
+ upsert_state_locals = (
318
+ state_execution_locals .get_upsert_state_execution_local_attributes ()
319
+ )
320
+ record_events = state_execution_locals .get_record_events ()
291
321
292
322
response = WorkflowStateExecuteResponse (
293
323
state_decision = _to_idl_state_decision (
@@ -301,6 +331,8 @@ def handle_workflow_state_execute(
301
331
KeyValue (k , v )
302
332
for (k , v ) in data_attributes .get_updated_values_to_return ().items ()
303
333
],
334
+ upsert_state_locals = upsert_state_locals ,
335
+ record_events = record_events ,
304
336
)
305
337
306
338
if upsert_sas :
@@ -367,3 +399,15 @@ def _create_upsert_search_attributes(
367
399
sas .append (sa )
368
400
369
401
return sas
402
+
403
+
404
+ def to_map (key_values : Union [None , Unset , List [KeyValue ]]) -> dict [str , EncodedObject ]:
405
+ key_values = unset_to_none (key_values ) or []
406
+ kvs = {}
407
+ for kv in key_values :
408
+ k = unset_to_none (kv .key )
409
+ v = unset_to_none (kv .value )
410
+ if k and v :
411
+ kvs [k ] = v
412
+
413
+ return kvs
0 commit comments