Skip to content

Instantly share code, notes, and snippets.

@jelder
Created March 5, 2014 20:13
Show Gist options
  • Save jelder/9375631 to your computer and use it in GitHub Desktop.
Save jelder/9375631 to your computer and use it in GitHub Desktop.
Prevent ElasticSearch outages from bringing down the rest of your app
module Tire
module Search
class Search
alias :perform_without_fail_fast :perform
def perform
@options[:timeout] = '1900ms'
begin
Timeout::timeout 2 do
perform_without_fail_fast
end
rescue Timeout::Error => e
Rails.logger.error e
@response = Tire::HTTP::Response.new "", 200
@json = {"took"=>1, "timed_out"=>true, "_shards"=>{"total"=>1, "successful"=>1, "failed"=>0}, "hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]}}
@results = Results::Collection.new(@json, @options)
return self
end
end
end
module Multi
class Search
alias :perform_without_fail_fast :perform
def perform
@options[:timeout] = '1900ms'
begin
Timeout::timeout 2 do
perform_without_fail_fast
end
rescue Timeout::Error => e
Rails.logger.error e
@response = Tire::HTTP::Response.new "", 200
@json = {'responses' => @searches.collect { |search| {"took"=>1, "timed_out"=>true, "_shards"=>{"total"=>1, "successful"=>1, "failed"=>0}, "hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]}} } }
@results = Tire::Search::Multi::Results.new @searches, @json['responses']
return self
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment