From fd330a9278f897ceae62516ee5c71c81e3747634 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Wed, 6 Feb 2019 00:26:25 +0100 Subject: [PATCH] Split the alias update call in two calls --- lib/searchyll/indexer.rb | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/searchyll/indexer.rb b/lib/searchyll/indexer.rb index 63884c9..b212437 100644 --- a/lib/searchyll/indexer.rb +++ b/lib/searchyll/indexer.rb @@ -241,12 +241,27 @@ def finalize_replication(http) # hot swap the index into the canonical alias def finalize_aliases(http) update_aliases = http_post('/_aliases') + + # perform removal and addition in two different calls so that + # the second one is performed even if the first one fails + if !old_indices.empty? + update_aliases.body = { + actions: [ + { remove: { + index: old_indices.join(','), + alias: configuration.elasticsearch_index_base_name + } } + ] + }.to_json + res = http.request(update_aliases) + unless res.kind_of?(Net::HTTPSuccess) + Jekyll.logger.error("Elasticsearch returned an error when removing old aliases: #{res.message} #{res.body}") + exit + end + end + update_aliases.body = { actions: [ - { remove: { - index: old_indices.join(','), - alias: configuration.elasticsearch_index_base_name - } }, { add: { index: elasticsearch_index_name, alias: configuration.elasticsearch_index_base_name @@ -254,8 +269,8 @@ def finalize_aliases(http) ] }.to_json res = http.request(update_aliases) - if !res.kind_of?(Net::HTTPSuccess) - $stderr.puts "Elasticsearch returned an error when updating aliases: " + res.message + " " + res.body + unless res.kind_of?(Net::HTTPSuccess) + Jekyll.logger.error("Elasticsearch returned an error when assigning the new alias: #{res.message} #{res.body}") exit end end