Created
February 13, 2012 16:55
-
-
Save henrik/1818244 to your computer and use it in GitHub Desktop.
Example of how to handle "ThinkingSphinx::SphinxError: index item_core: index not preread" type errors from Thinking Sphinx. As mentioned in http://groups.google.com/group/thinking-sphinx/browse_thread/thread/4854326d6c388da2
This file contains hidden or 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
class Searcher | |
def search(query) | |
with_retries do | |
Item.search(query, populate: true) | |
end | |
end | |
private | |
MAX_ATTEMPTS = 5 | |
def with_retries | |
attempt = 0 | |
begin | |
yield | |
rescue ThinkingSphinx::SphinxError | |
# We over-eagerly rescue all Sphinx errors. But since we re-raise them | |
# if they still fail after a few attempts, we don't hide anything. | |
attempt += 1 | |
if attempt > MAX_ATTEMPTS | |
raise | |
else | |
retry | |
end | |
end | |
end | |
end |
This file contains hidden or 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
# encoding: utf-8 | |
require 'spec_helper' | |
describe Searcher do | |
# http://groups.google.com/group/thinking-sphinx/browse_thread/thread/4854326d6c388da2 | |
context 'handling "index not preread" errors gracefully', :sphinx do | |
it "should populate early so failures can be handled by this class" do | |
Searcher.new.search("Foo").should be_populated | |
end | |
it "should retry a number of times on failure" do | |
attempt = 1 | |
ThinkingSphinx::Search.any_instance.stub(:error) { | |
if attempt > 4 | |
nil # No errors. | |
else | |
attempt += 1 | |
"index item_core: index not preread" | |
end | |
} | |
item = Item.create!(description: "Foo Barson") | |
index_sphinx # Or however you do it. | |
Searcher.new.search("Foo").map(&:id).should == [ item.id ] | |
end | |
it "should re-raise if the retries run out" do | |
ThinkingSphinx::Search.any_instance.stub(:error).and_return("index item_core: index not preread") | |
-> { | |
Searcher.new.search("Foo").map(&:id) | |
}.should raise_error(ThinkingSphinx::SphinxError) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment