Skip to content

Instantly share code, notes, and snippets.

@RafeHatfield
Forked from nz/sunspot_resque.rb
Last active December 23, 2015 20:49
Show Gist options
  • Save RafeHatfield/6691963 to your computer and use it in GitHub Desktop.
Save RafeHatfield/6691963 to your computer and use it in GitHub Desktop.
Example to queue sunspot solr updates using sidekiq instead of resque
# 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