Rails sphinx reindex with rakege delayed_job function
I found a bunch of posts on this site and others that gave me tidbits of what I needed, but I was unable to piece them together.
For searching, I run sphinx on my site using the sphinx gem. I need to re-index every time a user adds a new post to the site. I have already run delayed_job on my site, so every time a new post is added, I want to re-index asynchronously using delayed_job. I think I need to use the handle_asynchronously command and call a method in my model that will run the rake task to re-index ... but I read somewhere that the rake should not be run from code.
Bottom line, completely confused. Can anyone advise on how to do this?
I could be completely off, but that's what I'm thinking about.
# in post.rb
def reindex_database
# run rake task
end
# in posts_controller.rb
def add_post
# add_post logic
handle_asynchronously Post.reindex_database
end
source to share
In case anyone is following this, I ended up solving this by adding a system call to my post.rb method:
# in post.rb
def reindexDB
`rake thinking_sphinx:rebuild`
end
.. and then whenever I need to call it, I use this:
Post.delay.reindexDB
It would be nice to know how to do it correctly.
source to share