Skip to content
This repository
Browse code

Merge branch 'v1'

Conflicts:
	lib/thinking_sphinx/search.rb
  • Loading branch information...
commit b293abdbdf0c0ccaadff155d66271fed9f45611b 2 parents ecb0854 + 96807c7
Pat Allan authored
5 lib/thinking_sphinx/configuration.rb
@@ -28,6 +28,7 @@ module ThinkingSphinx
28 28 # html remove elements:: ''
29 29 # searchd_binary_name:: searchd
30 30 # indexer_binary_name:: indexer
  31 + # hard_retry_count:: 0
31 32 #
32 33 # If you want to change these settings, create a YAML file at
33 34 # config/sphinx.yml with settings for each environment, in a similar
@@ -64,7 +65,8 @@ class Configuration
64 65
65 66 attr_accessor :searchd_file_path, :allow_star, :app_root,
66 67 :model_directories, :delayed_job_priority, :indexed_models, :use_64_bit,
67   - :touched_reindex_file, :stop_timeout, :version, :shuffle
  68 + :touched_reindex_file, :stop_timeout, :version, :shuffle,
  69 + :hard_retry_count
68 70
69 71 attr_accessor :source_options, :index_options
70 72
@@ -111,6 +113,7 @@ def reset(custom_app_root=nil)
111 113 self.delayed_job_priority = 0
112 114 self.indexed_models = []
113 115 self.shuffle = false
  116 + self.hard_retry_count = 0
114 117
115 118 self.source_options = {}
116 119 self.index_options = {
51 lib/thinking_sphinx/search.rb
@@ -420,27 +420,40 @@ def config
420 420 def populate
421 421 return if @populated
422 422 @populated = true
  423 + retries = hard_retries
423 424
424   - retry_on_stale_index do
425   - begin
426   - log query do
427   - @results = client.query query, indexes, comment
  425 + begin
  426 + retry_on_stale_index do
  427 + begin
  428 + log query do
  429 + @results = client.query query, indexes, comment
  430 + end
  431 + total = @results[:total_found].to_i
  432 + log "Found #{total} result#{'s' unless total == 1}"
  433 +
  434 + log "Sphinx Daemon returned warning: #{warning}" if warning?
  435 +
  436 + if error?
  437 + log "Sphinx Daemon returned error: #{error}"
  438 + raise SphinxError.new(error, @results) unless options[:ignore_errors]
  439 + end
  440 + rescue Errno::ECONNREFUSED => err
  441 + raise ThinkingSphinx::ConnectionError,
  442 + 'Connection to Sphinx Daemon (searchd) failed.'
428 443 end
429   - total = @results[:total_found].to_i
430   - log "Found #{total} result#{'s' unless total == 1}"
431   -
432   - log "Sphinx Daemon returned warning: #{warning}" if warning?
433 444
434   - if error?
435   - log "Sphinx Daemon returned error: #{error}"
436   - raise SphinxError.new(error, @results) unless options[:ignore_errors]
437   - end
438   - rescue Errno::ECONNREFUSED => err
439   - raise ThinkingSphinx::ConnectionError,
440   - 'Connection to Sphinx Daemon (searchd) failed.'
  445 + compose_results
  446 + end
  447 + rescue => e
  448 + log 'Caught Sphinx exception: %s (%s %s left)' % [
  449 + e.message, retries, (retries == 1 ? 'try' : 'tries')
  450 + ]
  451 + retries -= 1
  452 + if retries >= 0
  453 + retry
  454 + else
  455 + raise e
441 456 end
442   -
443   - compose_results
444 457 end
445 458 end
446 459
@@ -861,6 +874,10 @@ def stale_retries
861 874 end
862 875 end
863 876
  877 + def hard_retries
  878 + options[:hard_retry_count] || config.hard_retry_count
  879 + end
  880 +
864 881 def include_for_class(klass)
865 882 includes = options[:include] || klass.sphinx_index_options[:include]
866 883
32 spec/thinking_sphinx/search_spec.rb
@@ -411,6 +411,38 @@
411 411 'baz @foo bar @(foo,bar) baz', :star => true
412 412 ).first
413 413 end
  414 +
  415 + it "should try retry query up to the hard_retry_count option times if it catches an exception" do
  416 + @client.should_receive(:query).exactly(4).and_raise("Test Exception")
  417 +
  418 + expect { ThinkingSphinx::Search.new(:hard_retry_count => 3).first }.
  419 + to raise_error("Test Exception")
  420 + end
  421 +
  422 + it "should not retry query if hard_retry_count option is not set" do
  423 + @client.should_receive(:query).exactly(1).and_raise("Test Exception")
  424 +
  425 + expect { ThinkingSphinx::Search.new.first }.
  426 + to raise_error("Test Exception")
  427 + end
  428 +
  429 + it "should allow the hard_retry_count to be globally set as a configuration option" do
  430 + @config.hard_retry_count = 2
  431 +
  432 + @client.should_receive(:query).exactly(3).and_raise("Test Exception")
  433 +
  434 + expect { ThinkingSphinx::Search.new.first }.
  435 + to raise_error("Test Exception")
  436 + end
  437 +
  438 + it "should give priority to the hard_retry_count search option over the globally configured option" do
  439 + @config.hard_retry_count = 4
  440 +
  441 + @client.should_receive(:query).exactly(2).and_raise("Test Exception")
  442 +
  443 + expect { ThinkingSphinx::Search.new(:hard_retry_count => 1).first }.
  444 + to raise_error("Test Exception")
  445 + end
414 446 end
415 447
416 448 describe 'comment' do

0 comments on commit b293abd

Please sign in to comment.
Something went wrong with that request. Please try again.