From d706b559da30d046df7200c17293ece6d4af6ea2 Mon Sep 17 00:00:00 2001 From: "Robin H. Johnson" Date: Wed, 25 Feb 2015 15:47:54 -0800 Subject: Refactor to simplify for a quick bug I hit. Signed-off-by: Robin H. Johnson --- ag | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/ag b/ag index b3fdb18..f3c106e 100755 --- a/ag +++ b/ag @@ -154,12 +154,8 @@ Ag::Utils.proc_count = $options.jobs def do_full abort "Wrong argument type: #{$options.argmode.to_s}" unless $options.argmode == :dir - begin - Ag::Storage.delete_index($options.name) unless $options.readonly - rescue Elasticsearch::Transport::Transport::Errors::NotFound => e - $stderr.puts "Index did not exist yet. Creating." if $options.debug - end - Ag::Storage.create_index($options.name) + do_delete_index(ignore_missing: true) unless $options.readonly + do_create_index(ignore_exists: true) messages = $maildir.list(:cur) @@ -184,6 +180,7 @@ end def do_incremental abort "Wrong argument type: #{$options.argmode.to_s}" unless $options.argmode == :dir messages = $maildir.list(:new) + do_create_index(ignore_exists: true) opts = { :in_processes => Ag::Utils.proc_count, @@ -218,18 +215,24 @@ def do_delete_msg end end -def do_delete_index +def do_delete_index(ignore_missing: false) begin Ag::Storage.delete_index($options.name) rescue Elasticsearch::Transport::Transport::Errors::NotFound => e - $stderr.puts "Index does not exist: #{e}" + $stderr.puts "Index does not exist: #{e}" unless ignore_missing rescue => e $stderr.puts "Cannot delete index: #{e}" end end -def do_create_index - Ag::Storage.create_index($options.name) +def do_create_index(ignore_exists: false) + begin + Ag::Storage.create_index($options.name) + rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e + if ignore_exists and e !~ /IndexAlreadyExistsException/ + raise e + end + end end def do_reindex -- cgit v1.2.3-65-gdbad