The ultrasphinx plugin adds a number of handy rake tasks for use in development. But what about running them on the production server? In my first foray into custom Capistrano tasks, I arrived at the following code:

   1  namespace :ultrasphinx do
   2    [:bootstrap, :configure].each do |t|
   3      desc "run the ultrasphinx:#{t} rake task on the production server"
   4      task t, :roles => :app do
   5        run "cd #{current_path} && rake ultrasphinx:#{t} RAILS_ENV=production"
   6      end
   7    end
   8    
   9    namespace :daemon do
  10      [:restart, :start, :status, :stop].each do |t|
  11        desc "run the ultrasphinx:daemon:#{t} rake task on the production server"
  12        task t, :roles => :app do
  13          run "cd #{current_path} && rake ultrasphinx:daemon:#{t} RAILS_ENV=production"
  14        end
  15      end
  16    end
  17    
  18    [:delta, :main, :merge].each do |t|
  19      desc "run the ultrasphinx:index:#{t} rake task on the production server"
  20      task t, :roles => :app do
  21        run "cd #{current_path} && rake ultrasphinx:index:#{t} RAILS_ENV=production"
  22      end
  23    end
  24    
  25    desc "run the ultrasphinx:index rake task on the production server"
  26    task :index, :roles => :app do
  27      run "cd #{current_path} && rake ultrasphinx:index RAILS_ENV=production"
  28    end
  29    
  30    namespace :spelling do
  31      desc "run the ultrasphinx:spelling:build rake task on the production server"
  32      task :build, :roles => :app do
  33        run "cd #{current_path} && rake ultrasphix:spelling:build RAILS_ENV=production"
  34      end
  35    end
  36  end

You’ll want to take note of the subtle inconsistency your Capistrano tasks will have compared to your rake tasks. Specifically, the tasks under the `ultrasphinx:index` namespace will be accessible in the `ultrashinx` Capistrano namespace. This was due to `ultrasphinx:index` serving as both a task and a namespace in the rake tasks, and my lack of finding a way duplicate that with Capistrano. (Honestly, I didn’t try that hard. I am pretty satisfied with this solution.)

Hopefully someone will find this helpful for managing ultrasphinx in a production environment, although I guess this post also serves as a pretty basic example of writing custom Capistrano tasks.