11 Jul 2009, 11:56pm
Ruby on Rails:
by

4 comments

Use a Cron Job to Automate Sphinx Index Refresh from Rails Rake Task

If using Sphinx, you need to refresh indexes when you add new content to your database. This is fairly easy to do by hand

rake thinking_sphinx:index RAILS_ENV=production

But if you want to automate this and use a cron, remember to set the PATH, SHELL and RAILS_ENV variables for your job. The environment isn’t the same when you’re doing it by hand and your index will fail silently :(

You find your specific PATH and SHELL info like so

echo $PATH
echo $SHELL

To get into the cron and set your schedule

crontab -e
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
SHELL=/bin/bash
RAILS_ENV=production

# re-index production sphinx every 15 minutes
*/15 * * * * root cd /var/www/rails_app && /usr/local/bin/rake thinking_sphinx:index >> /dev/null 2>&1

More information available here

http://heimdull.blogspot.com/2009/05/journey-with-thinking-sphinx-and-crond.html

http://groups.google.com/group/thinking-sphinx/browse_thread/thread/5451458fae7d6124

  • http://aerotovary.ru Slony

    Thank you for this post! I was hitting my head against the wall trying to find out why searchd repeatedly shuts down :) Setting environment vars solved the problem :)

  • JP

    OR:

    Call the sphinx indexer directly, also eases the load (you don’t need to fire up rails), eg:

    /usr/local/bin/indexer –config /data/releases/site/current/config/production.sphinx.conf –all –rotate

    • admin

      that’s a much better solution. thanks!

    • http://flexlearner.com Ezhil

      that command worked perfectly could you tell me the one for delta indexing,because your command runs the main indexing..
      eagerly waiting for your reply..