The link Florian provided has code by Amit Solanki that works!
Here is what I did to get this to work with Capistrano:
Install gems
Create a file called script/delayed_delta
with the contents:
#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'
dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
daemon_options = {
:multiple => false,
:dir_mode => :normal,
:dir => File.join(dir, 'tmp', 'pids'),
:backtrace => true
}
puts 'delayed_delta'
Daemons.run_proc('job_runner', daemon_options) do
if ARGV.include?('--')
ARGV.slice! 0..ARGV.index('--')
else
ARGV.clear
end
Dir.chdir dir
RAILS_ENV = ARGV.first || ENV['RAILS_ENV'] || 'development'
require File.join('config', 'environment')
Delayed::Worker.new(
:min_priority => ENV['MIN_PRIORITY'],
:max_priority => ENV['MAX_PRIORITY']
).start
end
Configure Capistrano
Capistrano needs to start Sphinx and job_runner (with our script/delayed_delta
).
Add something like this to the deploy.rb
:
deploy.task :restart, :roles => :app do
run "export RAILS_ENV=production && cd #{deploy_to}/current && /usr/bin/rake ts:rebuild"
run "export RAILS_ENV=production && cd #{current_path} && /usr/bin/ruby script/delayed_delta start"
end
Configure whenever
gem
In your config/schedule.rb
add lines to update Sphinx's index and start job_runner if it isn't already running
every 30.minutes do
command "export RAILS_ENV=production && cd /path/to/rails/production && /usr/bin/rake ts:index && /usr/bin/ruby script/delayed_delta start"
end
This gets converted to a crontab that is run every 30 minutes to update sphinx
Final Notes and Lessons Learned
The script/delayed_delta
uses the daemon_generator gem to start the job_runner background worker script. This is equivalent to running rake thinking_sphinx:delayed_deltas
on the console, but persistent.
Make sure only one job_runner or rake thinking_sphinx:delayed_deltas
process is running at one time
Let Capistrano start both Sphinx (rake ts:rebuild) and script/delayed_delta
. I had problem when I started sphinx and delayed_deltas from different users or different environments