Created
February 24, 2010 23:32
-
-
Save look/314024 to your computer and use it in GitHub Desktop.
This file contains 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
# | |
# Silently fail instead of raising an exception when an error occurs while writing to Solr. | |
# NOTE: does not fail for reads; you should catch those exceptions, for example in a rescue_from statement. | |
# | |
# To configure, add this to an initializer: | |
# Sunspot.session = SilentFailSessionProxy.new(session_or_proxy) | |
# | |
# You can get the existing session with Sunspot.send(:session) (it's a private method in Sunspot 0.18, but not 1.0) | |
# | |
# This is for Sunspot 0.18 and would need to be changed a little bit for Sunspot 1.0. | |
# | |
class SilentFailSessionProxy | |
attr_reader :session | |
delegate :new_search, :search, :configuration, :to => :session | |
[:index, :index!, :commit, :remove, :remove!, :remove_by_id, | |
:remove_by_id!, :remove_all, :remove_all!, :dirty?, :commit_if_dirty, :batch].each do |method| | |
module_eval(<<-RUBY) | |
def #{method}(*args, &block) | |
begin | |
session.#{method}(*args, &block) | |
rescue => e | |
Rails.logger.error(e.message) | |
end | |
end | |
RUBY | |
end | |
def initialize(session) | |
@session = session | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment