views:

423

answers:

3

I'm working on setting up a production server using CentOS 5.3, Apache, and Phusion Passenger (mod_rails). I have an app that uses the Sphinx search engine and the Thinking Sphinx gem.

According to the Thinking Sphinx docs...

If you actually want to search against the indexed data, then you’ll need Sphinx’s searchd daemon to be running. This can be controlled using the following tasks:

rake thinking_sphinx:start
rake ts:start
rake thinking_sphinx:stop
rake ts:stop

What would be the best way to ensure that this takes place in production? I can deploy my app, then manually run rake thinking_sphinx:start, but I like to set things up so that if I have to bounce the server, everything will come back up.

Should I put a call to that Rake task in an initializer? Or something in rc.local?

+4  A: 

rc.local is a good start, but its not enough. I would pair is with a monit rule to ensure it is running AND more importantly...

Sphinx requires a full-reindex to make all the latest and greatest available. There is some doco on the thinking sphinx site about delta indexing, but if your index is small, an hourly re-index will take care of things and you do not need the delta indexing stuff.

I run this hourly to take care of this:

0 * * * * cd /var/rails/my_site/current/ && RAILS_ENV=production /usr/bin/rake ts:rebuild

Note: for deployment, I will use the built in thinking sphinx capistrano tasks:

In your Capfile add

require 'thinking_sphinx/deploy/capistrano'

I used to chain the re-indexing in the cap task but stopped cause it is really slow, when I make schema changes I will remember to run it or wait for the hourly cron job to fix it up.

Sam Saffron
If a user does a search while the re-indexing is running, wouldn't they get an error?
Ethan
yerp, it makes the search die when you are rebuilding, I suggest you add some retry logic to compensate (retry for up to 20 secs, or show a custom page that says search will be available soon). (probably a good idea for pat to add this to ts)
Sam Saffron
+1  A: 

I haven't done this before with Spinix, so I hope someone can give you a better answer, but you should take a look at monit. Monit is designed for keeping daemons running, just like what you need to do.

A quick Google for spinix monit turned up this link: Capistrano recipes: sphinx:monit. That would be a good place to start.

Luke Francl
A: 

For what it's worth, I'm running

thinking_sphinx:index

... in my cron job, instead of the "rebuild" task. This does not require the searchd process to be offline, but the indices are still rotated when it's done, so new changes are picked up. I think the "rebuild" task is only necessary when you actually change your index structure in your models, which happens very rarely for me.

Scott Brown