From 6144f9b1afc390534c81c9627ef535d7f52d30aa Mon Sep 17 00:00:00 2001 From: algolia-bot Date: Tue, 31 Dec 2024 14:39:57 +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 +- lib/algolia/api/search_client.rb | 106 ++++++++++++++------------ 2 files changed, 57 insertions(+), 51 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/Bug_report.yml b/.github/ISSUE_TEMPLATE/Bug_report.yml index ecf2dfdb..d01e1b39 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/lib/algolia/api/search_client.rb b/lib/algolia/api/search_client.rb index 67d08dea..809d4a5e 100644 --- a/lib/algolia/api/search_client.rb +++ b/lib/algolia/api/search_client.rb @@ -3478,63 +3478,69 @@ def chunked_batch( def replace_all_objects(index_name, objects, batch_size = 1000, request_options = {}) tmp_index_name = index_name + "_tmp_" + rand(10_000_000).to_s - copy_operation_response = operation_index( - index_name, - Search::OperationIndexParams.new( - operation: Search::OperationType::COPY, - destination: tmp_index_name, - scope: [ - Search::ScopeType::SETTINGS, - Search::ScopeType::RULES, - Search::ScopeType::SYNONYMS - ] - ), - request_options - ) + begin + copy_operation_response = operation_index( + index_name, + Search::OperationIndexParams.new( + operation: Search::OperationType::COPY, + destination: tmp_index_name, + scope: [ + Search::ScopeType::SETTINGS, + Search::ScopeType::RULES, + Search::ScopeType::SYNONYMS + ] + ), + request_options + ) - batch_responses = chunked_batch( - tmp_index_name, - objects, - Search::Action::ADD_OBJECT, - true, - batch_size, - request_options - ) + batch_responses = chunked_batch( + tmp_index_name, + objects, + Search::Action::ADD_OBJECT, + true, + batch_size, + request_options + ) - wait_for_task(tmp_index_name, copy_operation_response.task_id) + wait_for_task(tmp_index_name, copy_operation_response.task_id) + + copy_operation_response = operation_index( + index_name, + Search::OperationIndexParams.new( + operation: Search::OperationType::COPY, + destination: tmp_index_name, + scope: [ + Search::ScopeType::SETTINGS, + Search::ScopeType::RULES, + Search::ScopeType::SYNONYMS + ] + ), + request_options + ) - copy_operation_response = operation_index( - index_name, - Search::OperationIndexParams.new( - operation: Search::OperationType::COPY, - destination: tmp_index_name, - scope: [ - Search::ScopeType::SETTINGS, - Search::ScopeType::RULES, - Search::ScopeType::SYNONYMS - ] - ), - request_options - ) + wait_for_task(tmp_index_name, copy_operation_response.task_id) - wait_for_task(tmp_index_name, copy_operation_response.task_id) + move_operation_response = operation_index( + tmp_index_name, + Search::OperationIndexParams.new( + operation: Search::OperationType::MOVE, + destination: index_name + ), + request_options + ) - move_operation_response = operation_index( - tmp_index_name, - Search::OperationIndexParams.new( - operation: Search::OperationType::MOVE, - destination: index_name - ), - request_options - ) + wait_for_task(tmp_index_name, move_operation_response.task_id) - wait_for_task(tmp_index_name, move_operation_response.task_id) + Search::ReplaceAllObjectsResponse.new( + copy_operation_response: copy_operation_response, + batch_responses: batch_responses, + move_operation_response: move_operation_response + ) + rescue Exception => e + delete_index(tmp_index_name) - Search::ReplaceAllObjectsResponse.new( - copy_operation_response: copy_operation_response, - batch_responses: batch_responses, - move_operation_response: move_operation_response - ) + raise e + end end def index_exists?(index_name)