@@ -57,10 +57,10 @@ def raise_if_failed(self):
5757 self .failure_details )
5858
5959
60+ @dataclass
6061class PurgeInstancesResult :
61- def __init__ (self , deleted_instance_count : int , is_complete : bool ):
62- self .deleted_instance_count = deleted_instance_count
63- self .is_complete = is_complete
62+ deleted_instance_count : int
63+ is_complete : bool
6464
6565
6666class OrchestrationFailedError (Exception ):
@@ -183,7 +183,7 @@ def get_orchestration_state_by(self,
183183 _continuation_token : Optional [pb2 .StringValue ] = None
184184 ) -> List [OrchestrationState ]:
185185 if max_instance_count is None :
186- # DTS backend does not behave well with max_instance_count = None, so we set to max 32-bit signed value
186+ # Some backends do not behave well with max_instance_count = None, so we set to max 32-bit signed value
187187 max_instance_count = (1 << 31 ) - 1
188188
189189 self ._logger .info (f"Querying orchestration instances with filters - "
@@ -194,29 +194,31 @@ def get_orchestration_state_by(self,
194194 f"fetch_inputs_and_outputs={ fetch_inputs_and_outputs } , "
195195 f"continuation_token={ _continuation_token .value if _continuation_token else None } " )
196196
197- req = pb .QueryInstancesRequest (
198- query = pb .InstanceQuery (
199- runtimeStatus = [status .value for status in runtime_status ] if runtime_status else None ,
200- createdTimeFrom = helpers .new_timestamp (created_time_from ) if created_time_from else None ,
201- createdTimeTo = helpers .new_timestamp (created_time_to ) if created_time_to else None ,
202- maxInstanceCount = max_instance_count ,
203- fetchInputsAndOutputs = fetch_inputs_and_outputs ,
204- continuationToken = _continuation_token
205- )
206- )
207- resp : pb .QueryInstancesResponse = self ._stub .QueryInstances (req )
208- states = [parse_orchestration_state (res ) for res in resp .orchestrationState ]
209- # Check the value for continuationToken - none or "0" indicates that there are no more results.
210- if resp .continuationToken and resp .continuationToken .value and resp .continuationToken .value != "0" :
211- self ._logger .info (f"Received continuation token with value { resp .continuationToken .value } , fetching next list of instances..." )
212- states += self .get_orchestration_state_by (
213- created_time_from ,
214- created_time_to ,
215- runtime_status ,
216- max_instance_count ,
217- fetch_inputs_and_outputs ,
218- _continuation_token = resp .continuationToken
197+ states = []
198+
199+ while True :
200+ req = pb .QueryInstancesRequest (
201+ query = pb .InstanceQuery (
202+ runtimeStatus = [status .value for status in runtime_status ] if runtime_status else None ,
203+ createdTimeFrom = helpers .new_timestamp (created_time_from ) if created_time_from else None ,
204+ createdTimeTo = helpers .new_timestamp (created_time_to ) if created_time_to else None ,
205+ maxInstanceCount = max_instance_count ,
206+ fetchInputsAndOutputs = fetch_inputs_and_outputs ,
207+ continuationToken = _continuation_token
208+ )
219209 )
210+ resp : pb .QueryInstancesResponse = self ._stub .QueryInstances (req )
211+ states += [parse_orchestration_state (res ) for res in resp .orchestrationState ]
212+ # Check the value for continuationToken - none or "0" indicates that there are no more results.
213+ if resp .continuationToken and resp .continuationToken .value and resp .continuationToken .value != "0" :
214+ self ._logger .info (f"Received continuation token with value { resp .continuationToken .value } , fetching next list of instances..." )
215+ if _continuation_token and _continuation_token .value and _continuation_token .value == resp .continuationToken .value :
216+ self ._logger .warning (f"Received the same continuation token value { resp .continuationToken .value } again, stopping to avoid infinite loop." )
217+ break
218+ _continuation_token = resp .continuationToken
219+ else :
220+ break
221+
220222 states = [state for state in states if state is not None ] # Filter out any None values
221223 return states
222224
@@ -377,28 +379,29 @@ def get_entities_by(self,
377379 f"include_state={ include_state } , "
378380 f"include_transient={ include_transient } , "
379381 f"page_size={ page_size } " )
380- query_request = pb .QueryEntitiesRequest (
381- query = pb .EntityQuery (
382- instanceIdStartsWith = helpers .get_string_value (instance_id_starts_with ),
383- lastModifiedFrom = helpers .new_timestamp (last_modified_from ) if last_modified_from else None ,
384- lastModifiedTo = helpers .new_timestamp (last_modified_to ) if last_modified_to else None ,
385- includeState = include_state ,
386- includeTransient = include_transient ,
387- pageSize = helpers .get_int_value (page_size ),
388- continuationToken = _continuation_token
389- )
390- )
391- resp : pb .QueryEntitiesResponse = self ._stub .QueryEntities (query_request )
392- entities = [EntityMetadata .from_entity_metadata (entity , query_request .query .includeState ) for entity in resp .entities ]
393- if resp .continuationToken and resp .continuationToken .value != "0" :
394- self ._logger .info (f"Received continuation token with value { resp .continuationToken .value } , fetching next page of entities..." )
395- entities += self .get_entities_by (
396- instance_id_starts_with = instance_id_starts_with ,
397- last_modified_from = last_modified_from ,
398- last_modified_to = last_modified_to ,
399- include_state = include_state ,
400- include_transient = include_transient ,
401- page_size = page_size ,
402- _continuation_token = resp .continuationToken
382+
383+ entities = []
384+
385+ while True :
386+ query_request = pb .QueryEntitiesRequest (
387+ query = pb .EntityQuery (
388+ instanceIdStartsWith = helpers .get_string_value (instance_id_starts_with ),
389+ lastModifiedFrom = helpers .new_timestamp (last_modified_from ) if last_modified_from else None ,
390+ lastModifiedTo = helpers .new_timestamp (last_modified_to ) if last_modified_to else None ,
391+ includeState = include_state ,
392+ includeTransient = include_transient ,
393+ pageSize = helpers .get_int_value (page_size ),
394+ continuationToken = _continuation_token
395+ )
403396 )
397+ resp : pb .QueryEntitiesResponse = self ._stub .QueryEntities (query_request )
398+ entities += [EntityMetadata .from_entity_metadata (entity , query_request .query .includeState ) for entity in resp .entities ]
399+ if resp .continuationToken and resp .continuationToken .value and resp .continuationToken .value != "0" :
400+ self ._logger .info (f"Received continuation token with value { resp .continuationToken .value } , fetching next page of entities..." )
401+ if _continuation_token and _continuation_token .value and _continuation_token .value == resp .continuationToken .value :
402+ self ._logger .warning (f"Received the same continuation token value { resp .continuationToken .value } again, stopping to avoid infinite loop." )
403+ break
404+ _continuation_token = resp .continuationToken
405+ else :
406+ break
404407 return entities
0 commit comments