66particularly focusing on decision-related events for replay and execution.
77"""
88
9- from dataclasses import dataclass , field
10- from typing import Iterator , List , Optional , Tuple
9+ from dataclasses import dataclass
10+ from typing import Iterator , List , Optional
1111
1212from cadence ._internal .workflow .history_event_iterator import HistoryEventsIterator
1313from cadence .api .v1 .history_pb2 import HistoryEvent
@@ -43,7 +43,8 @@ class DecisionEventsIterator(Iterator[DecisionEvents]):
4343 """
4444
4545 def __init__ (
46- self , decision_task : PollForDecisionTaskResponse ,
46+ self ,
47+ decision_task : PollForDecisionTaskResponse ,
4748 events : List [HistoryEvent ],
4849 ):
4950 self ._decision_task = decision_task
@@ -55,7 +56,6 @@ def __iter__(self):
5556 return self
5657
5758 def __next__ (self ) -> DecisionEvents :
58-
5959 """
6060 Process the next decision batch.
6161 1. Find the next valid decision task started event during replay or last scheduled decision task events for non-replay
@@ -73,12 +73,15 @@ def __next__(self) -> DecisionEvents:
7373 next_event = self ._events .peek ()
7474
7575 # latest event, not replay, assign started event as decision event insteaad
76- if next_event == None :
76+ if next_event is None :
7777 decision_event = event
7878 break
7979
8080 match next_event .WhichOneof ("attributes" ):
81- case "decision_task_failed_event_attributes" | "decision_task_timed_out_event_attributes" :
81+ case (
82+ "decision_task_failed_event_attributes"
83+ | "decision_task_timed_out_event_attributes"
84+ ):
8285 # skip failed / timed out decision tasks and continue searching
8386 next (self ._events )
8487 continue
@@ -87,25 +90,31 @@ def __next__(self) -> DecisionEvents:
8790 decision_event = next (self ._events )
8891 break
8992 case _:
90- raise ValueError (f"unexpected event type after decision task started event: { next_event } " )
93+ raise ValueError (
94+ f"unexpected event type after decision task started event: { next_event } "
95+ )
9196
9297 case _:
9398 decision_input_events .append (event )
9499
95100 if not decision_event :
96- raise StopIteration (f "no decision event found" )
101+ raise StopIteration ("no decision event found" )
97102
98103 # collect decision output events
99104 while self ._events .has_next ():
100- if not is_decision_event (self ._events .peek ()):
105+ nxt = self ._events .peek () if self ._events .has_next () else None
106+ if nxt and not is_decision_event (nxt ):
101107 break
102108 decision_output_events .append (next (self ._events ))
103109
104110 replay_current_time_milliseconds = decision_event .event_time .ToMilliseconds ()
105111
106- replay : bool
107- next_decision_event_id : int
108- if decision_event .WhichOneof ("attributes" ) == "decision_task_completed_event_attributes" : # completed decision task
112+ replay : bool
113+ next_decision_event_id : int
114+ if (
115+ decision_event .WhichOneof ("attributes" )
116+ == "decision_task_completed_event_attributes"
117+ ): # completed decision task
109118 replay = True
110119 next_decision_event_id = decision_event .event_id + 1
111120 else :
@@ -124,25 +133,32 @@ def __next__(self) -> DecisionEvents:
124133 next_decision_event_id = next_decision_event_id ,
125134 )
126135
136+
127137def is_decision_event (event : HistoryEvent ) -> bool :
128138 """Check if an event is a decision output event."""
129- return event != None and event .WhichOneof ("attributes" ) in set ([
130- "activity_task_scheduled_event_attributes" ,
131- "start_child_workflow_execution_initiated_event_attributes" ,
132- "timer_started_event_attributes" ,
133- "workflow_execution_completed_event_attributes" ,
134- "workflow_execution_failed_event_attributes" ,
135- "workflow_execution_canceled_event_attributes" ,
136- "workflow_execution_continued_as_new_event_attributes" ,
137- "activity_task_cancel_requested_event_attributes" ,
138- "request_cancel_activity_task_failed_event_attributes" ,
139- "timer_canceled_event_attributes" ,
140- "cancel_timer_failed_event_attributes" ,
141- "request_cancel_external_workflow_execution_initiated_event_attributes" ,
142- "marker_recorded_event_attributes" ,
143- "signal_external_workflow_execution_initiated_event_attributes" ,
144- "upsert_workflow_search_attributes_event_attributes" ,
145- ])
139+ return event is not None and event .WhichOneof ("attributes" ) in set (
140+ [
141+ "activity_task_scheduled_event_attributes" ,
142+ "start_child_workflow_execution_initiated_event_attributes" ,
143+ "timer_started_event_attributes" ,
144+ "workflow_execution_completed_event_attributes" ,
145+ "workflow_execution_failed_event_attributes" ,
146+ "workflow_execution_canceled_event_attributes" ,
147+ "workflow_execution_continued_as_new_event_attributes" ,
148+ "activity_task_cancel_requested_event_attributes" ,
149+ "request_cancel_activity_task_failed_event_attributes" ,
150+ "timer_canceled_event_attributes" ,
151+ "cancel_timer_failed_event_attributes" ,
152+ "request_cancel_external_workflow_execution_initiated_event_attributes" ,
153+ "marker_recorded_event_attributes" ,
154+ "signal_external_workflow_execution_initiated_event_attributes" ,
155+ "upsert_workflow_search_attributes_event_attributes" ,
156+ ]
157+ )
158+
146159
147160def is_marker_event (event : HistoryEvent ) -> bool :
148- return event != None and event .WhichOneof ("attributes" ) == "marker_recorded_event_attributes"
161+ return bool (
162+ event is not None
163+ and event .WhichOneof ("attributes" ) == "marker_recorded_event_attributes"
164+ )
0 commit comments