Skip to content

Instantly share code, notes, and snippets.

@hakunin
Last active August 29, 2015 14:18
Show Gist options
  • Save hakunin/32c2f01e27784b59764a to your computer and use it in GitHub Desktop.
Save hakunin/32c2f01e27784b59764a to your computer and use it in GitHub Desktop.
Work around Errno::ECONNREFUSED when SOLR is not running
=begin
1) include following ruby file from your config/application.rb like so:
require File.expand_path('../../lib/ext/active_record', __FILE__)
2) add auto_commit_after_request: false to your config/sunspot.yml
development:
solr:
...
auto_commit_after_request: false
=end
class SolrWrapRailtie < Rails::Railtie
initializer 'after_sunspot', after: 'sunspot_rails.init' do
ActiveSupport.on_load(:active_record) do
include(ThrowAwaySolrConnectionErrors)
end
end
end
module ThrowAwaySolrConnectionErrors
class <<self
def included(base)
base.module_eval do
class << self
alias_method(:original_searchable, :searchable)
end
extend(ClassMethods)
end
end
end
module ClassMethods
def searchable *a, &block
original_searchable(*a, &block)
unless respond_to? :original_perform_index_tasks
alias_method(
:original_perform_index_tasks,
:perform_index_tasks
)
alias_method(
:original_remove_from_index,
:remove_from_index
)
end
define_method :perform_index_tasks do
begin
original_perform_index_tasks
rescue Errno::ECONNREFUSED => e
if Rails.env.development?
Rails.logger.warn(e)
else
Rollbar.error(e)
end
end
end
define_method :remove_from_index do
begin
original_remove_from_index
rescue Errno::ECONNREFUSED => e
if Rails.env.development?
Rails.logger.warn(e)
else
Rollbar.error(e)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment