Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retry failed updates at least once after a 500ms delay.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
sunspot/lib/sunspot/session_proxy/retry_fail_session_proxy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |