Created
March 7, 2013 08:35
-
-
Save amasses/5106480 to your computer and use it in GitHub Desktop.
I'm considering this modification as occasionally we see Errno::ECONNRESET if there are communication issues between the rails app and ES. While the indexing failing isn't good, the AR operation failing is worse (to me). Using Thread.new without joining should mean that the exception isn't raised back to the caller, but I'm not sure if in certai…
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
module Tire | |
module Model | |
# Main module containing the infrastructure for automatic updating | |
# of the _ElasticSearch_ index on model instance create, update or delete. | |
# | |
# Include it in your model: `include Tire::Model::Callbacks` | |
# | |
# The model must respond to `after_save` and `after_destroy` callbacks | |
# (ActiveModel and ActiveRecord models do so, by default). | |
# | |
module Callbacks | |
# A hook triggered by the `include Tire::Model::Callbacks` statement in the model. | |
# | |
def self.included(base) | |
# Update index on model instance change or destroy. | |
# | |
if base.respond_to?(:after_save) && base.respond_to?(:after_destroy) | |
base.send :after_save, lambda do | |
Thread.new { tire.update_index } | |
end | |
base.send :after_destroy, lambda do | |
Thread.new { tire.update_index } | |
end | |
end | |
# Add neccessary infrastructure for the model, when missing in | |
# some half-baked ActiveModel implementations. | |
# | |
if base.respond_to?(:before_destroy) && !base.instance_methods.map(&:to_sym).include?(:destroyed?) | |
base.class_eval do | |
before_destroy { @destroyed = true } | |
def destroyed?; !!@destroyed; end | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment