Restart solar spigot solr when deployed with capistrano in rails 3.1
There are project rails in the working environment 3.1.
Now this is my deploy.rb:
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM lib directory to the load pathe
require "rvm/capistrano" # Load RVM capistrano plugin.
require "bundler/capistrano"
set :rvm_ruby_string, 'ruby-1.9.2-p318@global'
set :rvm_type, :user
set :application, "domain.com"
set :user, 'user'
#set :repository, "#{user}@ip.ip.ip.ip:~/app"
set :repository, "ssh://git@bitbucket.org/user/app.git"
set :keep_releases, 3
set :scm, :git
set :use_sudo, false
set :deploy_to, "~/#{application}"
#set :deploy_via, :copy
set :unicorn_conf, "#{deploy_to}/current/config/unicorn.rb"
set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"
set :deploy_via, :remote_cache
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
role :web, "ip.ip.ip.ip" # Your HTTP server, Apache/etc
role :app, "ip.ip.ip.ip" # This may be the same as your `Web` server
role :db, "ip.ip.ip.ip", :primary => true # This is where Rails migrations will run
namespace :deploy do
task :restart do
run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D; fi"
end
task :start do
run "bundle exec unicorn -c #{unicorn_conf} -E #{rails_env} -D"
end
task :stop do
run "if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -QUIT ` cat #{unicorn_pid}`; fi"
end
end
load 'deploy/assets'
after "deploy:restart", "deploy:cleanup"
I want to do these tasks in capistrano. Now I do these tasks manually:
1ΒΊ I kill the solar patrol with
a) Find pid with ps aux | grep 'solr'
b) Kill the pid with kill pid_number
2ΒΊ Remove solr index in production environment if it exists:
a) rm -r solr/data/production/index
3ΒΊ turn on sunspot with:
a) RAILS_ENV=production rake sunspot:solr:start
4 Β° Reindex models with:
a) RAILS_ENV=production rake sunspot:mongo:reindex
My question is:
How do I add these tasks to my deploy.rb?
Thank!
source to share
This might be a good starting point:
namespace :solr do
task :reindex do
run "cd #{current_path} && #{rake} RAILS_ENV=#{rails_env} sunspot:solr:reindex"
end
end
Can you just summon rake sunspot:solr:stop
instead of killing? I'm not sure if you need to drop the index if you are going to be doing reindex ...
source to share
task :stop_solr do
begin
run("cd #{deploy_to}/current && /usr/bin/env rake sunspot:solr:stop RAILS_ENV=#{rails_env}")
rescue Exception => error
puts "***Unable to stop Solr with error: #{error}"
puts "***Solr may have not been started. Continuing anyway.***"
end
end
#restart and reindex any newly added full search fields:
task :restart_solr do
begin
run("cd #{release_path} && /usr/bin/env rake sunspot:solr:start RAILS_ENV=#{rails_env}")
rescue Exception => error
puts "***Unable to start Solr with error: #{error}."
puts "***Continuing anyway.***"
end
end
task :reindex_solr do
begin
run("cd #{release_path} && /usr/bin/env rake sunspot:reindex RAILS_ENV=#{rails_env}")
rescue Exception => error
puts "***Unable to reindex Solr with error: #{error}"
puts "***Continuing anyway.***"
end
end
Also, as I mentioned in my comment to Chris, you will have problems if you clean up the old Capistrano directories, unless you kill the SOLR process and force it to point to the new index files. One way to avoid this scenario is to install SOLR on a shared directory and reset the symbolic link during deployment.
source to share