From 735c4570660902c8df9b911b45b77dc92797cc15 Mon Sep 17 00:00:00 2001 From: algolia-bot Date: Tue, 31 Dec 2024 14:39:55 +0000 Subject: [PATCH] feat(clients): cleanup after replaceAllObjects failure [skip-bc] (generated) https://github.com/algolia/api-clients-automation/pull/3824 Co-authored-by: algolia-bot Co-authored-by: Pierre Millot Co-authored-by: Thomas Raffray --- .github/ISSUE_TEMPLATE/Bug_report.yml | 2 +- algoliasearch/search/client.py | 188 ++++++++++++++------------ 2 files changed, 101 insertions(+), 89 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/Bug_report.yml b/.github/ISSUE_TEMPLATE/Bug_report.yml index ecf2dfdbd..d01e1b390 100644 --- a/.github/ISSUE_TEMPLATE/Bug_report.yml +++ b/.github/ISSUE_TEMPLATE/Bug_report.yml @@ -27,7 +27,7 @@ body: id: client attributes: label: Client - description: Which API are you targetting? + description: Which API are you targeting? options: - All - AB testing diff --git a/algoliasearch/search/client.py b/algoliasearch/search/client.py index b9890ef01..d811244d9 100644 --- a/algoliasearch/search/client.py +++ b/algoliasearch/search/client.py @@ -620,57 +620,63 @@ async def replace_all_objects( """ tmp_index_name = self.create_temporary_name(index_name) - async def _copy() -> UpdatedAtResponse: - return await self.operation_index( - index_name=index_name, - operation_index_params=OperationIndexParams( - operation=OperationType.COPY, - destination=tmp_index_name, - scope=[ - ScopeType("settings"), - ScopeType("rules"), - ScopeType("synonyms"), - ], - ), + try: + + async def _copy() -> UpdatedAtResponse: + return await self.operation_index( + index_name=index_name, + operation_index_params=OperationIndexParams( + operation=OperationType.COPY, + destination=tmp_index_name, + scope=[ + ScopeType("settings"), + ScopeType("rules"), + ScopeType("synonyms"), + ], + ), + request_options=request_options, + ) + + copy_operation_response = await _copy() + + batch_responses = await self.chunked_batch( + index_name=tmp_index_name, + objects=objects, + wait_for_tasks=True, + batch_size=batch_size, request_options=request_options, ) - copy_operation_response = await _copy() - - batch_responses = await self.chunked_batch( - index_name=tmp_index_name, - objects=objects, - wait_for_tasks=True, - batch_size=batch_size, - request_options=request_options, - ) + await self.wait_for_task( + index_name=tmp_index_name, task_id=copy_operation_response.task_id + ) - await self.wait_for_task( - index_name=tmp_index_name, task_id=copy_operation_response.task_id - ) + copy_operation_response = await _copy() + await self.wait_for_task( + index_name=tmp_index_name, task_id=copy_operation_response.task_id + ) - copy_operation_response = await _copy() - await self.wait_for_task( - index_name=tmp_index_name, task_id=copy_operation_response.task_id - ) + move_operation_response = await self.operation_index( + index_name=tmp_index_name, + operation_index_params=OperationIndexParams( + operation=OperationType.MOVE, + destination=index_name, + ), + request_options=request_options, + ) + await self.wait_for_task( + index_name=tmp_index_name, task_id=move_operation_response.task_id + ) - move_operation_response = await self.operation_index( - index_name=tmp_index_name, - operation_index_params=OperationIndexParams( - operation=OperationType.MOVE, - destination=index_name, - ), - request_options=request_options, - ) - await self.wait_for_task( - index_name=tmp_index_name, task_id=move_operation_response.task_id - ) + return ReplaceAllObjectsResponse( + copy_operation_response=copy_operation_response, + batch_responses=batch_responses, + move_operation_response=move_operation_response, + ) + except Exception as e: + await self.delete_index(tmp_index_name) - return ReplaceAllObjectsResponse( - copy_operation_response=copy_operation_response, - batch_responses=batch_responses, - move_operation_response=move_operation_response, - ) + raise e async def index_exists(self, index_name: str) -> bool: """ @@ -5658,57 +5664,63 @@ def replace_all_objects( """ tmp_index_name = self.create_temporary_name(index_name) - def _copy() -> UpdatedAtResponse: - return self.operation_index( - index_name=index_name, - operation_index_params=OperationIndexParams( - operation=OperationType.COPY, - destination=tmp_index_name, - scope=[ - ScopeType("settings"), - ScopeType("rules"), - ScopeType("synonyms"), - ], - ), + try: + + def _copy() -> UpdatedAtResponse: + return self.operation_index( + index_name=index_name, + operation_index_params=OperationIndexParams( + operation=OperationType.COPY, + destination=tmp_index_name, + scope=[ + ScopeType("settings"), + ScopeType("rules"), + ScopeType("synonyms"), + ], + ), + request_options=request_options, + ) + + copy_operation_response = _copy() + + batch_responses = self.chunked_batch( + index_name=tmp_index_name, + objects=objects, + wait_for_tasks=True, + batch_size=batch_size, request_options=request_options, ) - copy_operation_response = _copy() - - batch_responses = self.chunked_batch( - index_name=tmp_index_name, - objects=objects, - wait_for_tasks=True, - batch_size=batch_size, - request_options=request_options, - ) + self.wait_for_task( + index_name=tmp_index_name, task_id=copy_operation_response.task_id + ) - self.wait_for_task( - index_name=tmp_index_name, task_id=copy_operation_response.task_id - ) + copy_operation_response = _copy() + self.wait_for_task( + index_name=tmp_index_name, task_id=copy_operation_response.task_id + ) - copy_operation_response = _copy() - self.wait_for_task( - index_name=tmp_index_name, task_id=copy_operation_response.task_id - ) + move_operation_response = self.operation_index( + index_name=tmp_index_name, + operation_index_params=OperationIndexParams( + operation=OperationType.MOVE, + destination=index_name, + ), + request_options=request_options, + ) + self.wait_for_task( + index_name=tmp_index_name, task_id=move_operation_response.task_id + ) - move_operation_response = self.operation_index( - index_name=tmp_index_name, - operation_index_params=OperationIndexParams( - operation=OperationType.MOVE, - destination=index_name, - ), - request_options=request_options, - ) - self.wait_for_task( - index_name=tmp_index_name, task_id=move_operation_response.task_id - ) + return ReplaceAllObjectsResponse( + copy_operation_response=copy_operation_response, + batch_responses=batch_responses, + move_operation_response=move_operation_response, + ) + except Exception as e: + self.delete_index(tmp_index_name) - return ReplaceAllObjectsResponse( - copy_operation_response=copy_operation_response, - batch_responses=batch_responses, - move_operation_response=move_operation_response, - ) + raise e def index_exists(self, index_name: str) -> bool: """