Skip to content

Commit

Permalink
[FIX] Rescue HTTP client specific connection errors in MyModel#create…
Browse files Browse the repository at this point in the history
…_elasticsearch_index

Added a `__host_unreachable_exceptions` for each HTTP client (adapter), so we can
rescue connection related errors in `create_elasticsearch_index`.

Closes karmi#729
  • Loading branch information
karmi committed Jun 5, 2013
1 parent e235a91 commit 3dc8cb7
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/tire/http/client.rb
Expand Up @@ -47,6 +47,10 @@ def self.head(url)
Response.new e.http_body, e.http_code
end

def self.__host_unreachable_exceptions
[Errno::ECONNREFUSED, ::RestClient::ServerBrokeConnection, ::RestClient::RequestTimeout]
end

private

def self.perform(response)
Expand Down
4 changes: 4 additions & 0 deletions lib/tire/http/clients/curb.rb
Expand Up @@ -56,6 +56,10 @@ def self.head(url)
Response.new client.body_str, client.response_code
end

def self.__host_unreachable_exceptions
[::Curl::Err::HostResolutionError, ::Curl::Err::ConnectionFailedError]
end

end

end
Expand Down
10 changes: 7 additions & 3 deletions lib/tire/http/clients/faraday.rb
Expand Up @@ -13,14 +13,14 @@
# require 'tire/http/clients/faraday'
#
# Tire.configure do |config|
#
#
# # Unless specified, tire will use Faraday.default_adapter and no middleware
# Tire::HTTP::Client::Faraday.faraday_middleware = Proc.new do |builder|
# builder.adapter :typhoeus
# end
#
#
# config.client(Tire::HTTP::Client::Faraday)
#
#
# end
#
#
Expand Down Expand Up @@ -58,6 +58,10 @@ def head(url)
request(:head, url)
end

def __host_unreachable_exceptions
[::Faraday::Error::ConnectionFailed, ::Faraday::Error::TimeoutError]
end

private
def request(method, url, data = nil)
conn = ::Faraday.new( &(faraday_middleware || DEFAULT_MIDDLEWARE) )
Expand Down
3 changes: 2 additions & 1 deletion lib/tire/model/indexing.rb
Expand Up @@ -112,7 +112,8 @@ def create_elasticsearch_index
result
end
end
rescue Errno::ECONNREFUSED => e

rescue *Tire::Configuration.client.__host_unreachable_exceptions => e
STDERR.puts "Skipping index creation, cannot connect to Elasticsearch",
"(The original exception was: #{e.inspect})"
false
Expand Down
8 changes: 8 additions & 0 deletions test/unit/http_client_test.rb
Expand Up @@ -43,6 +43,10 @@ class ClientTest < Test::Unit::TestCase
end
end

should "have __host_unreachable_exceptions" do
assert_respond_to Client::RestClient, :__host_unreachable_exceptions
end

end

if defined?(Curl)
Expand Down Expand Up @@ -81,6 +85,10 @@ class ClientTest < Test::Unit::TestCase
threads.each { |t| t.join() }
end

should "have __host_unreachable_exceptions" do
assert_respond_to Client::RestClient, :__host_unreachable_exceptions
end

end

end
Expand Down
47 changes: 43 additions & 4 deletions test/unit/model_initialization_test.rb
Expand Up @@ -12,17 +12,56 @@ class ModelWithIncorrectMapping
end
end

class MyModelForIndexCreate
extend ActiveModel::Naming
include Tire::Model::Search
end

module Tire
module Model

class ModelInitializationTest < Test::Unit::TestCase

context "Model initialization" do

should "display a warning when creating the index fails" do
STDERR.expects(:puts)
result = ModelWithIncorrectMapping.create_elasticsearch_index
assert ! result, result.inspect
should "display a warning and not raise exception when creating the index fails" do
assert_nothing_raised do
STDERR.expects(:puts)
result = ModelWithIncorrectMapping.create_elasticsearch_index
assert ! result, result.inspect
end
end

should "re-raise non-connection related exceptions" do
Tire::Index.any_instance.expects(:exists?).raises(ZeroDivisionError)

assert_raise(ZeroDivisionError) do
result = MyModelForIndexCreate.create_elasticsearch_index
assert ! result, result.inspect
end
end

unless defined?(Curl)

should "display a warning and not raise exception when cannot connect to Elasticsearch (default client)" do
Tire::Index.any_instance.expects(:exists?).raises(Errno::ECONNREFUSED)
assert_nothing_raised do
STDERR.expects(:puts)
result = MyModelForIndexCreate.create_elasticsearch_index
assert ! result, result.inspect
end
end

else
should "display a warning and not raise exception when cannot connect to Elasticsearch (Curb client)" do
Tire::Index.any_instance.expects(:exists?).raises(::Curl::Err::HostResolutionError)
assert_nothing_raised do
STDERR.expects(:puts)
result = MyModelForIndexCreate.create_elasticsearch_index
assert ! result, result.inspect
end
end

end

end
Expand Down

0 comments on commit 3dc8cb7

Please sign in to comment.