-
-
Save RafeHatfield/6691963 to your computer and use it in GitHub Desktop.
Example to queue sunspot solr updates using sidekiq instead of resque
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
# app/models/post.rb | |
class Post | |
searchable :auto_index => false, :auto_remove => false do | |
text :title | |
text :body | |
end | |
after_commit :sidekiq_solr_update, :if => :persisted? | |
before_destroy :sidekiq_solr_remove | |
protected | |
def sidekiq_solr_update | |
SolrUpdate.perform_async(self.class.to_s, id) | |
end | |
def sidekiq_solr_remove | |
SolrRemove.perform_async(self.class.to_s, id) | |
end | |
end | |
# app/workers/solr_update.rb | |
class SolrUpdate | |
include Sidekiq::Worker | |
sidekiq_options :queue => :solr | |
def perform(classname, id) | |
classname.constantize.find(id).solr_index | |
end | |
end | |
# app/workers/solr_remove.rb | |
class SolrRemove | |
include Sidekiq::Worker | |
sidekiq_options :queue => :solr | |
def perform(classname, id) | |
Sunspot.remove_by_id(classname, id) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment