Skip to content

Commit

Permalink
Retry failed updates at least once after a 500ms delay.
Browse files Browse the repository at this point in the history
  • Loading branch information
nz committed Mar 15, 2012
1 parent c290d35 commit 0a15955
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions sunspot/lib/sunspot/session_proxy/retry_fail_session_proxy.rb
@@ -0,0 +1,45 @@
require File.join(File.dirname(__FILE__), 'abstract_session_proxy')

module Sunspot
module SessionProxy
class RetryFailSessionProxy < AbstractSessionProxy

attr_reader :search_session

delegate :new_search, :search, :config,
:new_more_like_this, :more_like_this,
:delete_dirty, :delete_dirty?,
:to => :search_session

def initialize(search_session = Sunspot.session)
@search_session = search_session
end

def rescued_exception(method, e)
$stderr.puts("Exception in #{method}: #{e.message}")
end

SUPPORTED_METHODS = [
:batch, :commit, :commit_if_dirty, :commit_if_delete_dirty, :dirty?,
:index!, :index, :optimize, :remove!, :remove, :remove_all!, :remove_all,
:remove_by_id!, :remove_by_id
]

SUPPORTED_METHODS.each do |method|
module_eval(<<-RUBY)
def #{method}(*args, &block)
retry_count = 1
begin
search_session.#{method}(*args, &block)
rescue => e
sleep 0.5
self.rescued_exception(:#{method}, e)
retry if (retry_count -= 1) >= 0
end
end
RUBY
end

end
end
end

0 comments on commit 0a15955

Please sign in to comment.