1111
1212from cadence .api .v1 .history_pb2 import HistoryEvent
1313from cadence .api .v1 .service_worker_pb2 import PollForDecisionTaskResponse
14- from cadence .client import Client
15- from cadence ._internal .workflow .history_event_iterator import iterate_history_events
1614
1715
1816@dataclass
@@ -55,99 +53,36 @@ class DecisionEventsIterator:
5553 into decision iterations for proper workflow replay and execution.
5654 """
5755
58- def __init__ (self , decision_task : PollForDecisionTaskResponse , client : Client ):
59- self ._client = client
56+ def __init__ (
57+ self , decision_task : PollForDecisionTaskResponse , events : List [HistoryEvent ]
58+ ):
6059 self ._decision_task = decision_task
61- self ._events : List [HistoryEvent ] = []
62- self ._event_index = 0
60+ self ._events : List [HistoryEvent ] = events
6361 self ._decision_task_started_event : Optional [HistoryEvent ] = None
6462 self ._next_decision_event_id = 1
6563 self ._replay = True
6664 self ._replay_current_time_milliseconds : Optional [int ] = None
67- self ._initialized = False
68-
69- @staticmethod
70- def _is_decision_task_started (event : HistoryEvent ) -> bool :
71- """Check if event is DecisionTaskStarted."""
72- return hasattr (
73- event , "decision_task_started_event_attributes"
74- ) and event .HasField ("decision_task_started_event_attributes" )
75-
76- @staticmethod
77- def _is_decision_task_completed (event : HistoryEvent ) -> bool :
78- """Check if event is DecisionTaskCompleted."""
79- return hasattr (
80- event , "decision_task_completed_event_attributes"
81- ) and event .HasField ("decision_task_completed_event_attributes" )
82-
83- @staticmethod
84- def _is_decision_task_failed (event : HistoryEvent ) -> bool :
85- """Check if event is DecisionTaskFailed."""
86- return hasattr (
87- event , "decision_task_failed_event_attributes"
88- ) and event .HasField ("decision_task_failed_event_attributes" )
89-
90- @staticmethod
91- def _is_decision_task_timed_out (event : HistoryEvent ) -> bool :
92- """Check if event is DecisionTaskTimedOut."""
93- return hasattr (
94- event , "decision_task_timed_out_event_attributes"
95- ) and event .HasField ("decision_task_timed_out_event_attributes" )
96-
97- @staticmethod
98- def _is_marker_recorded (event : HistoryEvent ) -> bool :
99- """Check if event is MarkerRecorded."""
100- return hasattr (event , "marker_recorded_event_attributes" ) and event .HasField (
101- "marker_recorded_event_attributes"
102- )
10365
104- @staticmethod
105- def _is_decision_task_completion (event : HistoryEvent ) -> bool :
106- """Check if event is any kind of decision task completion."""
107- return (
108- DecisionEventsIterator ._is_decision_task_completed (event )
109- or DecisionEventsIterator ._is_decision_task_failed (event )
110- or DecisionEventsIterator ._is_decision_task_timed_out (event )
111- )
112-
113- async def _ensure_initialized (self ):
114- """Initialize events list using the existing iterate_history_events."""
115- if not self ._initialized :
116- # Use existing iterate_history_events function
117- events_iterator = iterate_history_events (self ._decision_task , self ._client )
118- self ._events = [event async for event in events_iterator ]
119- self ._initialized = True
120-
121- # Find first decision task started event
122- for i , event in enumerate (self ._events ):
123- if self ._is_decision_task_started (event ):
124- self ._event_index = i
125- break
66+ self ._event_index = 0
67+ # Find first decision task started event
68+ for i , event in enumerate (self ._events ):
69+ if _is_decision_task_started (event ):
70+ self ._event_index = i
71+ break
12672
12773 async def has_next_decision_events (self ) -> bool :
128- """Check if there are more decision events to process."""
129- await self ._ensure_initialized ()
130-
13174 # Look for the next DecisionTaskStarted event from current position
13275 for i in range (self ._event_index , len (self ._events )):
133- if self . _is_decision_task_started (self ._events [i ]):
76+ if _is_decision_task_started (self ._events [i ]):
13477 return True
13578
13679 return False
13780
13881 async def next_decision_events (self ) -> DecisionEvents :
139- """
140- Get the next set of decision events.
141-
142- This method processes events starting from a DecisionTaskStarted event
143- until the corresponding DecisionTaskCompleted/Failed/TimedOut event.
144- """
145- await self ._ensure_initialized ()
146-
14782 # Find next DecisionTaskStarted event
14883 start_index = None
14984 for i in range (self ._event_index , len (self ._events )):
150- if self . _is_decision_task_started (self ._events [i ]):
85+ if _is_decision_task_started (self ._events [i ]):
15186 start_index = i
15287 break
15388
@@ -182,9 +117,9 @@ async def next_decision_events(self) -> DecisionEvents:
182117 decision_events .events .append (event )
183118
184119 # Categorize the event
185- if self . _is_marker_recorded (event ):
120+ if _is_marker_recorded (event ):
186121 decision_events .markers .append (event )
187- elif self . _is_decision_task_completion (event ):
122+ elif _is_decision_task_completion (event ):
188123 # This marks the end of this decision iteration
189124 self ._process_decision_completion_event (event , decision_events )
190125 current_index += 1 # Move past this event
@@ -206,7 +141,7 @@ async def next_decision_events(self) -> DecisionEvents:
206141 # Check directly without calling has_next_decision_events to avoid recursion
207142 has_more = False
208143 for i in range (self ._event_index , len (self ._events )):
209- if self . _is_decision_task_started (self ._events [i ]):
144+ if _is_decision_task_started (self ._events [i ]):
210145 has_more = True
211146 break
212147
@@ -261,16 +196,16 @@ async def __anext__(self) -> DecisionEvents:
261196def is_decision_event (event : HistoryEvent ) -> bool :
262197 """Check if an event is a decision-related event."""
263198 return (
264- DecisionEventsIterator . _is_decision_task_started (event )
265- or DecisionEventsIterator . _is_decision_task_completed (event )
266- or DecisionEventsIterator . _is_decision_task_failed (event )
267- or DecisionEventsIterator . _is_decision_task_timed_out (event )
199+ _is_decision_task_started (event )
200+ or _is_decision_task_completed (event )
201+ or _is_decision_task_failed (event )
202+ or _is_decision_task_timed_out (event )
268203 )
269204
270205
271206def is_marker_event (event : HistoryEvent ) -> bool :
272207 """Check if an event is a marker event."""
273- return DecisionEventsIterator . _is_marker_recorded (event )
208+ return _is_marker_recorded (event )
274209
275210
276211def extract_event_timestamp_millis (event : HistoryEvent ) -> Optional [int ]:
@@ -279,3 +214,47 @@ def extract_event_timestamp_millis(event: HistoryEvent) -> Optional[int]:
279214 seconds = getattr (event .event_time , "seconds" , 0 )
280215 return seconds * 1000 if seconds > 0 else None
281216 return None
217+
218+
219+ def _is_decision_task_started (event : HistoryEvent ) -> bool :
220+ """Check if event is DecisionTaskStarted."""
221+ return hasattr (event , "decision_task_started_event_attributes" ) and event .HasField (
222+ "decision_task_started_event_attributes"
223+ )
224+
225+
226+ def _is_decision_task_completed (event : HistoryEvent ) -> bool :
227+ """Check if event is DecisionTaskCompleted."""
228+ return hasattr (
229+ event , "decision_task_completed_event_attributes"
230+ ) and event .HasField ("decision_task_completed_event_attributes" )
231+
232+
233+ def _is_decision_task_failed (event : HistoryEvent ) -> bool :
234+ """Check if event is DecisionTaskFailed."""
235+ return hasattr (event , "decision_task_failed_event_attributes" ) and event .HasField (
236+ "decision_task_failed_event_attributes"
237+ )
238+
239+
240+ def _is_decision_task_timed_out (event : HistoryEvent ) -> bool :
241+ """Check if event is DecisionTaskTimedOut."""
242+ return hasattr (
243+ event , "decision_task_timed_out_event_attributes"
244+ ) and event .HasField ("decision_task_timed_out_event_attributes" )
245+
246+
247+ def _is_marker_recorded (event : HistoryEvent ) -> bool :
248+ """Check if event is MarkerRecorded."""
249+ return hasattr (event , "marker_recorded_event_attributes" ) and event .HasField (
250+ "marker_recorded_event_attributes"
251+ )
252+
253+
254+ def _is_decision_task_completion (event : HistoryEvent ) -> bool :
255+ """Check if event is any kind of decision task completion."""
256+ return (
257+ _is_decision_task_completed (event )
258+ or _is_decision_task_failed (event )
259+ or _is_decision_task_timed_out (event )
260+ )
0 commit comments