-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexecutor.py
More file actions
1188 lines (1003 loc) · 45.6 KB
/
executor.py
File metadata and controls
1188 lines (1003 loc) · 45.6 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Execution life-cycle logic."""
from __future__ import annotations
import logging
import uuid
from datetime import UTC, datetime
from typing import TYPE_CHECKING
from aws_durable_execution_sdk_python.execution import (
DurableExecutionInvocationInput,
DurableExecutionInvocationOutput,
InvocationStatus,
)
from aws_durable_execution_sdk_python.lambda_service import (
CallbackTimeoutType,
ErrorObject,
Operation,
OperationUpdate,
OperationStatus,
OperationType,
CallbackOptions,
)
from aws_durable_execution_sdk_python_testing.exceptions import (
ExecutionAlreadyStartedException,
IllegalStateException,
InvalidParameterValueException,
ResourceNotFoundException,
)
from aws_durable_execution_sdk_python_testing.execution import Execution
from aws_durable_execution_sdk_python_testing.model import (
CheckpointDurableExecutionResponse,
CheckpointUpdatedExecutionState,
GetDurableExecutionHistoryResponse,
GetDurableExecutionResponse,
GetDurableExecutionStateResponse,
ListDurableExecutionsByFunctionResponse,
ListDurableExecutionsResponse,
SendDurableExecutionCallbackFailureResponse,
SendDurableExecutionCallbackHeartbeatResponse,
SendDurableExecutionCallbackSuccessResponse,
StartDurableExecutionInput,
StartDurableExecutionOutput,
StopDurableExecutionResponse,
TERMINAL_STATUSES,
EventCreationContext,
)
from aws_durable_execution_sdk_python_testing.model import (
Event as HistoryEvent,
)
from aws_durable_execution_sdk_python_testing.model import (
Execution as ExecutionSummary,
)
from aws_durable_execution_sdk_python_testing.observer import ExecutionObserver
from aws_durable_execution_sdk_python_testing.token import CallbackToken
if TYPE_CHECKING:
from collections.abc import Awaitable, Callable
from concurrent.futures import Future
from aws_durable_execution_sdk_python_testing.checkpoint.processor import (
CheckpointProcessor,
)
from aws_durable_execution_sdk_python_testing.invoker import Invoker
from aws_durable_execution_sdk_python_testing.scheduler import Event, Scheduler
from aws_durable_execution_sdk_python_testing.stores.base import ExecutionStore
logger = logging.getLogger(__name__)
class Executor(ExecutionObserver):
MAX_CONSECUTIVE_FAILED_ATTEMPTS: int = 5
RETRY_BACKOFF_SECONDS: int = 5
def __init__(
self,
store: ExecutionStore,
scheduler: Scheduler,
invoker: Invoker,
checkpoint_processor: CheckpointProcessor,
):
self._store = store
self._scheduler = scheduler
self._invoker = invoker
self._checkpoint_processor = checkpoint_processor
self._completion_events: dict[str, Event] = {}
self._callback_timeouts: dict[str, Future] = {}
self._callback_heartbeats: dict[str, Future] = {}
def start_execution(
self,
input: StartDurableExecutionInput, # noqa: A002
) -> StartDurableExecutionOutput:
# Generate invocation_id if not provided
if input.invocation_id is None:
input = StartDurableExecutionInput(
account_id=input.account_id,
function_name=input.function_name,
function_qualifier=input.function_qualifier,
execution_name=input.execution_name,
execution_timeout_seconds=input.execution_timeout_seconds,
execution_retention_period_days=input.execution_retention_period_days,
invocation_id=str(uuid.uuid4()),
trace_fields=input.trace_fields,
tenant_id=input.tenant_id,
input=input.input,
)
execution = Execution.new(input=input)
execution.start()
self._store.save(execution)
completion_event = self._scheduler.create_event()
self._completion_events[execution.durable_execution_arn] = completion_event
# Schedule initial invocation to run immediately
self._invoke_execution(execution.durable_execution_arn)
return StartDurableExecutionOutput(
execution_arn=execution.durable_execution_arn
)
def get_execution(self, execution_arn: str) -> Execution:
"""Get execution by ARN.
Args:
execution_arn: The execution ARN to retrieve
Returns:
Execution: The execution object
Raises:
ResourceNotFoundException: If execution does not exist
"""
try:
return self._store.load(execution_arn)
except KeyError as e:
msg: str = f"Execution {execution_arn} not found"
raise ResourceNotFoundException(msg) from e
def get_execution_details(self, execution_arn: str) -> GetDurableExecutionResponse:
"""Get detailed execution information for web API response.
Args:
execution_arn: The execution ARN to retrieve
Returns:
GetDurableExecutionResponse: Detailed execution information
Raises:
ResourceNotFoundException: If execution does not exist
"""
execution = self.get_execution(execution_arn)
# Extract execution details from the first operation (EXECUTION type)
execution_op = execution.get_operation_execution_started()
status = execution.current_status().value
# Extract result and error from execution result
result = None
error = None
if execution.result:
if execution.result.status == InvocationStatus.SUCCEEDED:
result = execution.result.result
elif execution.result.status == InvocationStatus.FAILED:
error = execution.result.error
return GetDurableExecutionResponse(
durable_execution_arn=execution.durable_execution_arn,
durable_execution_name=execution.start_input.execution_name,
function_arn=f"arn:aws:lambda:us-east-1:123456789012:function:{execution.start_input.function_name}",
status=status,
start_timestamp=execution_op.start_timestamp
if execution_op.start_timestamp
else datetime.now(UTC),
input_payload=execution_op.execution_details.input_payload
if execution_op.execution_details
else None,
result=result,
error=error,
end_timestamp=execution_op.end_timestamp
if execution_op.end_timestamp
else None,
version="1.0",
)
def list_executions(
self,
function_name: str | None = None,
function_version: str | None = None, # noqa: ARG002
execution_name: str | None = None,
status_filter: str | None = None,
started_after: str | None = None,
started_before: str | None = None,
marker: str | None = None,
max_items: int | None = None,
reverse_order: bool = False, # noqa: FBT001, FBT002
) -> ListDurableExecutionsResponse:
"""List executions with filtering and pagination.
Args:
function_name: Filter by function name
function_version: Filter by function version
execution_name: Filter by execution name
status_filter: Filter by status (RUNNING, SUCCEEDED, FAILED)
started_after: Filter executions started after this time
started_before: Filter executions started before this time
marker: Pagination marker
max_items: Maximum items to return (default 50)
reverse_order: Return results in reverse chronological order
Returns:
ListDurableExecutionsResponse: List of executions with pagination
"""
# Convert marker to offset
offset: int = 0
if marker:
try:
offset = int(marker)
except ValueError:
offset = 0
# Query store directly with parameters
executions, next_marker = self._store.query(
function_name=function_name,
execution_name=execution_name,
status_filter=status_filter,
started_after=started_after,
started_before=started_before,
limit=max_items or 50,
offset=offset,
reverse_order=reverse_order,
)
# Convert to ExecutionSummary objects
execution_summaries: list[ExecutionSummary] = [
ExecutionSummary.from_execution(execution, execution.current_status().value)
for execution in executions
]
return ListDurableExecutionsResponse(
durable_executions=execution_summaries, next_marker=next_marker
)
def list_executions_by_function(
self,
function_name: str,
qualifier: str | None = None, # noqa: ARG002
execution_name: str | None = None,
status_filter: str | None = None,
started_after: str | None = None,
started_before: str | None = None,
marker: str | None = None,
max_items: int | None = None,
reverse_order: bool = False, # noqa: FBT001, FBT002
) -> ListDurableExecutionsByFunctionResponse:
"""List executions for a specific function.
Args:
function_name: The function name to filter by
qualifier: Function qualifier/version
execution_name: Filter by execution name
status_filter: Filter by status (RUNNING, SUCCEEDED, FAILED)
started_after: Filter executions started after this time
started_before: Filter executions started before this time
marker: Pagination marker
max_items: Maximum items to return (default 50)
reverse_order: Return results in reverse chronological order
Returns:
ListDurableExecutionsByFunctionResponse: List of executions for the function
"""
# Use the general list_executions method with function_name filter
list_response = self.list_executions(
function_name=function_name,
execution_name=execution_name,
status_filter=status_filter,
started_after=started_after,
started_before=started_before,
marker=marker,
max_items=max_items,
reverse_order=reverse_order,
)
return ListDurableExecutionsByFunctionResponse(
durable_executions=list_response.durable_executions,
next_marker=list_response.next_marker,
)
def stop_execution(
self, execution_arn: str, error: ErrorObject | None = None
) -> StopDurableExecutionResponse:
"""Stop a running execution.
Args:
execution_arn: The execution ARN to stop
error: Optional error to use when stopping the execution
Returns:
StopDurableExecutionResponse: Response containing end timestamp
Raises:
ResourceNotFoundException: If execution does not exist
ExecutionAlreadyStartedException: If execution is already completed
"""
execution = self.get_execution(execution_arn)
if execution.is_complete:
# Context-aware mapping: execution already completed maps to ExecutionAlreadyStartedException
msg: str = f"Execution {execution_arn} is already completed"
raise ExecutionAlreadyStartedException(msg, execution_arn)
# Use provided error or create a default one
stop_error = error or ErrorObject.from_message(
"Execution stopped by user request"
)
# Stop sets TERMINATED close status (different from fail)
logger.exception("[%s] Stopping execution.", execution_arn)
execution.complete_stopped(error=stop_error) # Sets CloseStatus.TERMINATED
self._store.update(execution)
self._complete_events(execution_arn=execution_arn)
return StopDurableExecutionResponse(stop_timestamp=datetime.now(UTC))
def get_execution_state(
self,
execution_arn: str,
checkpoint_token: str | None = None,
marker: str | None = None,
max_items: int | None = None,
) -> GetDurableExecutionStateResponse:
"""Get execution state with operations.
Args:
execution_arn: The execution ARN
checkpoint_token: Checkpoint token for state consistency
marker: Pagination marker
max_items: Maximum items to return
Returns:
GetDurableExecutionStateResponse: Execution state with operations
Raises:
ResourceNotFoundException: If execution does not exist
InvalidParameterValueException: If checkpoint token is invalid
"""
execution = self.get_execution(execution_arn)
# TODO: Validate checkpoint token if provided
if checkpoint_token and checkpoint_token not in execution.used_tokens:
msg: str = f"Invalid checkpoint token: {checkpoint_token}"
raise InvalidParameterValueException(msg)
# Get operations (excluding the initial EXECUTION operation for state)
operations = execution.get_assertable_operations()
# Apply pagination
if max_items is None:
max_items = 100
# Simple pagination - in real implementation would need proper marker handling
start_index = 0
if marker:
try:
start_index = int(marker)
except ValueError:
start_index = 0
end_index = start_index + max_items
paginated_operations = operations[start_index:end_index]
next_marker = None
if end_index < len(operations):
next_marker = str(end_index)
return GetDurableExecutionStateResponse(
operations=paginated_operations, next_marker=next_marker
)
def get_execution_history(
self,
execution_arn: str,
include_execution_data: bool = False, # noqa: FBT001, FBT002
reverse_order: bool = False, # noqa: FBT001, FBT002
marker: str | None = None,
max_items: int | None = None,
) -> GetDurableExecutionHistoryResponse:
"""Get execution history with events.
Args:
execution_arn: The execution ARN
include_execution_data: Whether to include execution data in events
reverse_order: Return events in reverse chronological order
marker: Pagination marker (event_id)
max_items: Maximum items to return
Returns:
GetDurableExecutionHistoryResponse: Execution history with events
Raises:
ResourceNotFoundException: If execution does not exist
"""
execution: Execution = self.get_execution(execution_arn)
# Generate events
all_events: list[HistoryEvent] = []
ops: list[Operation] = execution.operations
updates: list[OperationUpdate] = execution.updates
updates_dict: dict[str, OperationUpdate] = {u.operation_id: u for u in updates}
durable_execution_arn: str = execution.durable_execution_arn
# Generate all events first (without final event IDs)
for op in ops:
operation_update: OperationUpdate | None = updates_dict.get(
op.operation_id, None
)
if op.status is OperationStatus.PENDING:
if (
op.operation_type is not OperationType.CHAINED_INVOKE
or op.start_timestamp is None
):
continue
context: EventCreationContext = EventCreationContext(
op,
0, # Temporary event_id, will be reassigned after sorting
durable_execution_arn,
execution.start_input,
execution.result,
operation_update,
include_execution_data,
)
pending = HistoryEvent.create_chained_invoke_event_pending(context)
all_events.append(pending)
if op.start_timestamp is not None:
context = EventCreationContext(
op,
0, # Temporary event_id, will be reassigned after sorting
durable_execution_arn,
execution.start_input,
execution.result,
operation_update,
include_execution_data,
)
started = HistoryEvent.create_event_started(context)
all_events.append(started)
if op.end_timestamp is not None and op.status in TERMINAL_STATUSES:
context = EventCreationContext(
op,
0, # Temporary event_id, will be reassigned after sorting
durable_execution_arn,
execution.start_input,
execution.result,
operation_update,
include_execution_data,
)
finished = HistoryEvent.create_event_terminated(context)
all_events.append(finished)
# Sort events by timestamp to get correct chronological order
all_events.sort(key=lambda event: event.event_timestamp)
# Reassign event IDs based on chronological order
all_events = [
HistoryEvent.from_event_with_id(event, i)
for i, event in enumerate(all_events, 1)
]
# Apply cursor-based pagination
if max_items is None:
max_items = 100
# Handle pagination marker
if reverse_order:
all_events.reverse()
start_index: int = 0
if marker:
try:
marker_event_id: int = int(marker)
# Find the index of the first event with event_id >= marker
start_index = len(all_events)
for i, e in enumerate(all_events):
is_valid_page_start: bool = (
e.event_id < marker_event_id
if reverse_order
else e.event_id >= marker_event_id
)
if is_valid_page_start:
start_index = i
break
except ValueError:
start_index = 0
# Get paginated events
end_index: int = start_index + max_items
paginated_events: list[HistoryEvent] = all_events[start_index:end_index]
# Generate next marker
next_marker: str | None = None
if end_index < len(all_events):
if reverse_order:
# Next marker is the event_id of the last returned event
next_marker = (
str(paginated_events[-1].event_id) if paginated_events else None
)
else:
# Next marker is the event_id of the next event after the last returned
next_marker = (
str(all_events[end_index].event_id)
if end_index < len(all_events)
else None
)
return GetDurableExecutionHistoryResponse(
events=paginated_events, next_marker=next_marker
)
def checkpoint_execution(
self,
execution_arn: str,
checkpoint_token: str,
updates: list[OperationUpdate] | None = None,
client_token: str | None = None,
) -> CheckpointDurableExecutionResponse:
"""Process checkpoint for an execution.
Args:
execution_arn: The execution ARN
checkpoint_token: Current checkpoint token
updates: List of operation updates to process
client_token: Client token for idempotency
Returns:
CheckpointDurableExecutionResponse: Updated checkpoint token and state
Raises:
ResourceNotFoundException: If execution does not exist
InvalidParameterValueException: If checkpoint token is invalid
"""
execution = self.get_execution(execution_arn)
# Validate checkpoint token
if checkpoint_token not in execution.used_tokens:
msg: str = f"Invalid checkpoint token: {checkpoint_token}"
raise InvalidParameterValueException(msg)
if updates:
checkpoint_output = self._checkpoint_processor.process_checkpoint(
checkpoint_token=checkpoint_token,
updates=updates,
client_token=client_token,
)
new_execution_state = None
if checkpoint_output.new_execution_state:
new_execution_state = CheckpointUpdatedExecutionState(
operations=checkpoint_output.new_execution_state.operations,
next_marker=checkpoint_output.new_execution_state.next_marker,
)
return CheckpointDurableExecutionResponse(
checkpoint_token=checkpoint_output.checkpoint_token,
new_execution_state=new_execution_state,
)
# Save execution state after generating new token
new_checkpoint_token = execution.get_new_checkpoint_token()
self._store.update(execution)
return CheckpointDurableExecutionResponse(
checkpoint_token=new_checkpoint_token,
new_execution_state=None,
)
def send_callback_success(
self,
callback_id: str,
result: bytes | None = None,
) -> SendDurableExecutionCallbackSuccessResponse:
"""Send callback success response.
Args:
callback_id: The callback ID to respond to
result: Optional result data for the callback
Returns:
SendDurableExecutionCallbackSuccessResponse: Empty response
Raises:
InvalidParameterValueException: If callback_id is invalid
ResourceNotFoundException: If callback does not exist
"""
if not callback_id:
msg: str = "callback_id is required"
raise InvalidParameterValueException(msg)
try:
callback_token = CallbackToken.from_str(callback_id)
execution = self.get_execution(callback_token.execution_arn)
execution.complete_callback_success(callback_id, result)
self._store.update(execution)
self._cleanup_callback_timeouts(callback_id)
self._invoke_execution(callback_token.execution_arn)
logger.info("Callback success completed for callback_id: %s", callback_id)
except Exception as e:
msg = f"Failed to process callback success: {e}"
raise ResourceNotFoundException(msg) from e
return SendDurableExecutionCallbackSuccessResponse()
def send_callback_failure(
self,
callback_id: str,
error: ErrorObject | None = None,
) -> SendDurableExecutionCallbackFailureResponse:
"""Send callback failure response.
Args:
callback_id: The callback ID to respond to
error: Optional error object for the callback failure
Returns:
SendDurableExecutionCallbackFailureResponse: Empty response
Raises:
InvalidParameterValueException: If callback_id is invalid
ResourceNotFoundException: If callback does not exist
"""
if not callback_id:
msg: str = "callback_id is required"
raise InvalidParameterValueException(msg)
callback_error: ErrorObject = error or ErrorObject.from_message("")
try:
callback_token: CallbackToken = CallbackToken.from_str(callback_id)
execution: Execution = self.get_execution(callback_token.execution_arn)
execution.complete_callback_failure(callback_id, callback_error)
self._store.update(execution)
self._cleanup_callback_timeouts(callback_id)
self._invoke_execution(callback_token.execution_arn)
logger.info("Callback failure completed for callback_id: %s", callback_id)
except Exception as e:
msg = f"Failed to process callback failure: {e}"
raise ResourceNotFoundException(msg) from e
return SendDurableExecutionCallbackFailureResponse()
def send_callback_heartbeat(
self, callback_id: str
) -> SendDurableExecutionCallbackHeartbeatResponse:
"""Send callback heartbeat to keep callback alive.
Args:
callback_id: The callback ID to send heartbeat for
Returns:
SendDurableExecutionCallbackHeartbeatResponse: Empty response
Raises:
InvalidParameterValueException: If callback_id is invalid
ResourceNotFoundException: If callback does not exist
"""
if not callback_id:
msg: str = "callback_id is required"
raise InvalidParameterValueException(msg)
try:
callback_token: CallbackToken = CallbackToken.from_str(callback_id)
execution: Execution = self.get_execution(callback_token.execution_arn)
# Find callback operation to verify it exists and is active
_, operation = execution.find_callback_operation(callback_id)
if operation.status != OperationStatus.STARTED:
msg = f"Callback {callback_id} is not active"
raise ResourceNotFoundException(msg)
# Reset heartbeat timeout if configured
self._reset_callback_heartbeat_timeout(
callback_id, execution.durable_execution_arn
)
logger.info("Callback heartbeat processed for callback_id: %s", callback_id)
except Exception as e:
msg = f"Failed to process callback heartbeat: {e}"
raise ResourceNotFoundException(msg) from e
return SendDurableExecutionCallbackHeartbeatResponse()
def _validate_invocation_response_and_store(
self,
execution_arn: str,
response: DurableExecutionInvocationOutput,
execution: Execution,
):
"""Validate response status and save it to the store if fine.
Raises:
InvalidParameterValueException: If the response status is invalid.
IllegalStateException: If the response status is valid but the execution is already completed.
"""
if execution.is_complete:
msg_already_complete: str = "Execution already completed, ignoring result"
raise IllegalStateException(msg_already_complete)
if response.status is None:
msg_status_required: str = "Response status is required"
raise InvalidParameterValueException(msg_status_required)
match response.status:
case InvocationStatus.FAILED:
if response.result is not None:
msg_failed_result: str = (
"Cannot provide a Result for FAILED status."
)
raise InvalidParameterValueException(msg_failed_result)
logger.info("[%s] Execution failed", execution_arn)
self._complete_workflow(
execution_arn, result=None, error=response.error
)
case InvocationStatus.SUCCEEDED:
if response.error is not None:
msg_success_error: str = (
"Cannot provide an Error for SUCCEEDED status."
)
raise InvalidParameterValueException(msg_success_error)
logger.info("[%s] Execution succeeded", execution_arn)
self._complete_workflow(
execution_arn, result=response.result, error=None
)
case InvocationStatus.PENDING:
if not execution.has_pending_operations(execution):
msg_pending_ops: str = (
"Cannot return PENDING status with no pending operations."
)
raise InvalidParameterValueException(msg_pending_ops)
logger.info("[%s] Execution pending async work", execution_arn)
case _:
msg_unexpected_status: str = (
f"Unexpected invocation status: {response.status}"
)
raise IllegalStateException(msg_unexpected_status)
def _invoke_handler(self, execution_arn: str) -> Callable[[], Awaitable[None]]:
"""Create a parameterless callable that captures execution arn for the scheduler."""
async def invoke() -> None:
execution: Execution = self._store.load(execution_arn)
# Early exit if execution is already completed - like Java's COMPLETED check
if execution.is_complete:
logger.info(
"[%s] Execution already completed, ignoring result", execution_arn
)
return
try:
invocation_input: DurableExecutionInvocationInput = (
self._invoker.create_invocation_input(execution=execution)
)
self._store.save(execution)
response: DurableExecutionInvocationOutput = self._invoker.invoke(
execution.start_input.function_name, invocation_input
)
# Reload execution after invocation in case it was completed via checkpoint
execution = self._store.load(execution_arn)
if execution.is_complete:
logger.info(
"[%s] Execution completed during invocation, ignoring result",
execution_arn,
)
return
# Process successful received response - validate status and handle accordingly
try:
self._validate_invocation_response_and_store(
execution_arn, response, execution
)
except (InvalidParameterValueException, IllegalStateException) as e:
logger.warning(
"[%s] Lambda output validation failure: %s", execution_arn, e
)
error_obj = ErrorObject.from_exception(e)
self._retry_invocation(execution, error_obj)
except ResourceNotFoundException:
logger.warning(
"[%s] Function No longer exists: %s",
execution_arn,
execution.start_input.function_name,
)
error_obj = ErrorObject.from_message(
message=f"Function not found: {execution.start_input.function_name}"
)
self._fail_workflow(execution_arn, error_obj)
except Exception as e: # noqa: BLE001
# Handle invocation errors (network, function not found, etc.)
logger.warning("[%s] Invocation failed: %s", execution_arn, e)
error_obj = ErrorObject.from_exception(e)
self._retry_invocation(execution, error_obj)
return invoke
def _invoke_execution(self, execution_arn: str, delay: float = 0) -> None:
"""Invoke execution after delay in seconds."""
completion_event = self._completion_events.get(execution_arn)
self._scheduler.call_later(
self._invoke_handler(execution_arn),
delay=delay,
completion_event=completion_event,
)
def _complete_workflow(
self, execution_arn: str, result: str | None, error: ErrorObject | None
):
"""Complete workflow - handles both success and failure with terminal state validation."""
execution = self._store.load(execution_arn)
if execution.is_complete:
msg: str = "Cannot make multiple close workflow decisions."
raise IllegalStateException(msg)
if error is not None:
self.fail_execution(execution_arn, error)
else:
self.complete_execution(execution_arn, result)
def _fail_workflow(self, execution_arn: str, error: ErrorObject):
"""Fail workflow with terminal state validation."""
execution = self._store.load(execution_arn)
if execution.is_complete:
msg: str = "Cannot make multiple close workflow decisions."
raise IllegalStateException(msg)
self.fail_execution(execution_arn, error)
def _retry_invocation(self, execution: Execution, error: ErrorObject):
"""Handle retry logic or fail execution if retries exhausted."""
if (
execution.consecutive_failed_invocation_attempts
> self.MAX_CONSECUTIVE_FAILED_ATTEMPTS
):
# Exhausted retries - fail the execution
self._fail_workflow(
execution_arn=execution.durable_execution_arn, error=error
)
else:
# Schedule retry with backoff
execution.consecutive_failed_invocation_attempts += 1
self._store.save(execution)
self._invoke_execution(
execution_arn=execution.durable_execution_arn,
delay=self.RETRY_BACKOFF_SECONDS,
)
def _complete_events(self, execution_arn: str):
# complete doesn't actually checkpoint explicitly
if event := self._completion_events.get(execution_arn):
event.set()
def wait_until_complete(
self, execution_arn: str, timeout: float | None = None
) -> bool:
"""Block until execution completion. Don't do this unless you actually want to block.
Args
timeout (int|float|None): Wait for event to set until this timeout.
Returns:
True when set. False if the event timed out without being set.
"""
if event := self._completion_events.get(execution_arn):
return event.wait(timeout)
# this really shouldn't happen - implies execution timed out?
msg: str = "execution does not exist."
raise ResourceNotFoundException(msg)
def complete_execution(self, execution_arn: str, result: str | None = None) -> None:
"""Complete execution successfully (COMPLETE_WORKFLOW_EXECUTION decision)."""
logger.debug("[%s] Completing execution with result: %s", execution_arn, result)
execution: Execution = self._store.load(execution_arn=execution_arn)
execution.complete_success(result=result) # Sets CloseStatus.COMPLETED
self._store.update(execution)
if execution.result is None:
msg: str = "Execution result is required"
raise IllegalStateException(msg)
self._complete_events(execution_arn=execution_arn)
def fail_execution(self, execution_arn: str, error: ErrorObject) -> None:
"""Fail execution with error (FAIL_WORKFLOW_EXECUTION decision)."""
logger.error("[%s] Completing execution with error: %s", execution_arn, error)
execution: Execution = self._store.load(execution_arn=execution_arn)
execution.complete_fail(error=error) # Sets CloseStatus.FAILED
self._store.update(execution)
# set by complete_fail
if execution.result is None:
msg: str = "Execution result is required"
raise IllegalStateException(msg)
self._complete_events(execution_arn=execution_arn)
def _on_wait_succeeded(self, execution_arn: str, operation_id: str) -> None:
"""Private method - called when a wait operation completes successfully."""
execution = self._store.load(execution_arn)
if execution.is_complete:
logger.info(
"[%s] Execution already completed, ignoring wait succeeded event",
execution_arn,
)
return
try:
execution.complete_wait(operation_id=operation_id)
self._store.update(execution)
logger.debug(
"[%s] Wait succeeded for operation %s", execution_arn, operation_id
)
except Exception:
logger.exception("[%s] Error processing wait succeeded.", execution_arn)
def _on_retry_ready(self, execution_arn: str, operation_id: str) -> None:
"""Private method - called when a retry delay has elapsed and retry is ready."""
execution = self._store.load(execution_arn)
if execution.is_complete:
logger.info(
"[%s] Execution already completed, ignoring retry", execution_arn
)
return
try:
execution.complete_retry(operation_id=operation_id)
self._store.update(execution)
logger.debug(
"[%s] Retry ready for operation %s", execution_arn, operation_id
)
except Exception:
logger.exception("[%s] Error processing retry ready.", execution_arn)
# region ExecutionObserver
def on_completed(self, execution_arn: str, result: str | None = None) -> None:
"""Complete execution successfully. Observer method triggered by notifier."""
self.complete_execution(execution_arn, result)
def on_failed(self, execution_arn: str, error: ErrorObject) -> None:
"""Fail execution. Observer method triggered by notifier."""
self.fail_execution(execution_arn, error)
def on_timed_out(self, execution_arn: str, error: ErrorObject) -> None:
"""Handle execution timeout (workflow timeout). Observer method triggered by notifier."""
logger.exception("[%s] Execution timed out.", execution_arn)
execution: Execution = self._store.load(execution_arn=execution_arn)
execution.complete_timeout(error=error) # Sets CloseStatus.TIMED_OUT
self._store.update(execution)
self._complete_events(execution_arn=execution_arn)
def on_stopped(self, execution_arn: str, error: ErrorObject) -> None:
"""Handle execution stop. Observer method triggered by notifier."""
# This should not be called directly - stop_execution handles termination
self.fail_execution(execution_arn, error)
def on_wait_timer_scheduled(
self, execution_arn: str, operation_id: str, delay: float
) -> None:
"""Schedule a wait operation. Observer method triggered by notifier."""
logger.debug("[%s] scheduling wait with delay: %d", execution_arn, delay)
def wait_handler() -> None:
self._on_wait_succeeded(execution_arn, operation_id)
self._invoke_execution(execution_arn, delay=0)
completion_event = self._completion_events.get(execution_arn)
self._scheduler.call_later(
wait_handler, delay=delay, completion_event=completion_event
)
def on_step_retry_scheduled(
self, execution_arn: str, operation_id: str, delay: float
) -> None:
"""Schedule a retry a step. Observer method triggered by notifier."""
logger.debug(
"[%s] scheduling retry for %s with delay: %d",
execution_arn,
operation_id,