-
-
Save nz/1282013 to your computer and use it in GitHub Desktop.
# app/models/post.rb | |
class Post | |
searchable :auto_index => false, :auto_remove => false do | |
text :title | |
text :body | |
end | |
after_commit :resque_solr_update, :if => :persisted? | |
before_destroy :resque_solr_remove | |
protected | |
def resque_solr_update | |
Resque.enqueue(SolrUpdate, self.class.to_s, id) | |
end | |
def resque_solr_remove | |
Resque.enqueue(SolrRemove, self.class.to_s, id) | |
end | |
end | |
# lib/jobs/solr_update.rb | |
class SolrUpdate | |
@queue = :solr | |
def self.perform(classname, id) | |
classname.constantize.find(id).solr_index | |
end | |
end | |
# lib/jobs/solr_remove.rb | |
class SolrRemove | |
@queue = :solr | |
def self.perform(classname, id) | |
Sunspot.remove_by_id(classname, id) | |
end | |
end |
When removing an index, the object might already be destroyed when the worker calls find.
You can use Sunspot.remove_by_id instead:
class SolrRemove
@queue = :solr
def self.perform(classname, id)
Sunspot.remove_by_id classname, id
end
end
Good point, @gudleik, thanks! Updated.
I just released a gem for doing just this called sunspot-queue (https://github.com/gaffneyc/sunspot-queue).
Great snippet, just what I was looking for, thanks. However, Nick, when is the Sunspot.commit
performed in this scenario?
@semmin, usually best to avoid issuing explicit commits and instead rely on your server's autoCommit
setting in the solrconfig.xml
.
I had to modify line 10 as follows: after_commit :resque_solr_update, if: :persisted?
since resque_solr_update
was triggered on delete operations.
Thanks, @semmin!
What do you think of adding this at the end of the Resque jobs, for specs and local development to work somewhat serially?
unless Rails.env.production?
Sunspot.commit
end
I'll try to do that over the next few days!