33from __future__ import annotations
44
55import logging
6- import time
76from typing import TYPE_CHECKING , TypeVar
87
98from aws_durable_execution_sdk_python .exceptions import (
109 FatalError ,
11- TimedSuspendExecution ,
1210)
13- from aws_durable_execution_sdk_python .lambda_service import ErrorObject , OperationUpdate
11+ from aws_durable_execution_sdk_python .lambda_service import (
12+ ErrorObject ,
13+ OperationUpdate ,
14+ )
1415from aws_durable_execution_sdk_python .logger import LogInfo
1516from aws_durable_execution_sdk_python .serdes import deserialize , serialize
17+ from aws_durable_execution_sdk_python .suspend import (
18+ suspend_with_optional_timeout ,
19+ suspend_with_optional_timestamp ,
20+ )
1621from aws_durable_execution_sdk_python .types import WaitForConditionCheckContext
1722
1823if TYPE_CHECKING :
2429 )
2530 from aws_durable_execution_sdk_python .identifier import OperationIdentifier
2631 from aws_durable_execution_sdk_python .logger import Logger
27- from aws_durable_execution_sdk_python .state import ExecutionState
32+ from aws_durable_execution_sdk_python .state import (
33+ CheckpointedResult ,
34+ ExecutionState ,
35+ )
2836
2937
3038T = TypeVar ("T" )
@@ -49,7 +57,9 @@ def wait_for_condition_handler(
4957 operation_identifier .name ,
5058 )
5159
52- checkpointed_result = state .get_checkpoint_result (operation_identifier .operation_id )
60+ checkpointed_result : CheckpointedResult = state .get_checkpoint_result (
61+ operation_identifier .operation_id
62+ )
5363
5464 # Check if already completed
5565 if checkpointed_result .is_succeeded ():
@@ -70,6 +80,13 @@ def wait_for_condition_handler(
7080 if checkpointed_result .is_failed ():
7181 checkpointed_result .raise_callable_error ()
7282
83+ if checkpointed_result .is_pending ():
84+ scheduled_timestamp = checkpointed_result .get_next_attempt_timestamp ()
85+ suspend_with_optional_timestamp (
86+ msg = f"wait_for_condition { operation_identifier .name or operation_identifier .operation_id } will retry at timestamp { scheduled_timestamp } " ,
87+ datetime_timestamp = scheduled_timestamp ,
88+ )
89+
7390 attempt : int = 1
7491 if checkpointed_result .is_started_or_ready ():
7592 # This is a retry - get state from previous checkpoint
@@ -164,7 +181,10 @@ def wait_for_condition_handler(
164181
165182 state .create_checkpoint (operation_update = retry_operation )
166183
167- _suspend_execution (operation_identifier , decision )
184+ suspend_with_optional_timeout (
185+ msg = f"wait_for_condition { operation_identifier .name or operation_identifier .operation_id } will retry in { decision .delay_seconds } seconds" ,
186+ timeout_seconds = decision .delay_seconds ,
187+ )
168188
169189 except Exception as e :
170190 # Mark as failed - waitForCondition doesn't have its own retry logic for errors
@@ -184,14 +204,3 @@ def wait_for_condition_handler(
184204
185205 msg : str = "wait_for_condition should never reach this point"
186206 raise FatalError (msg )
187-
188-
189- def _suspend_execution (
190- operation_identifier : OperationIdentifier , decision : WaitForConditionDecision
191- ) -> None :
192- scheduled_timestamp = time .time () + (decision .delay_seconds or 0 )
193- msg = f"wait_for_condition { operation_identifier .name or operation_identifier .operation_id } will retry in { decision .delay_seconds } seconds"
194- raise TimedSuspendExecution (
195- msg ,
196- scheduled_timestamp = scheduled_timestamp ,
197- )
0 commit comments