hyperrjas
hyperrjas

Reputation: 10744

restart sunspot solr when make a deploy with capistrano in rails 3.1

I have a project rails 3.1 in production environment.

This is my deploy.rb now:

$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load pathe
require "rvm/capistrano"                  # Load RVM's 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://[email protected]/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 make these tasks in capistrano. Now I perform these tasks manually:

1º I kill sunspot solr pid with:

a) Find the pid with ps aux | grep 'solr'

b) Kill pid with kill pid_number

2º Remove index solr in production environment if exist with:

a) rm -r solr/data/production/index

3º turn on sunspot solr 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 can I add these tasks to my deploy.rb?

Thank you!

Upvotes: 3

Views: 3689

Answers (2)

yuяi
yuяi

Reputation: 2715

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 Kris's answer, you'll have issues if you clean up old Capistrano directories, unless you kill the SOLR process and force it to point to new index files. One way to avoid this scenario is to set up SOLR in a shared directory and reset the symlink during deployment.

Upvotes: 0

Kris
Kris

Reputation: 19938

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 

You can just call the rake sunspot:solr:stop instead of kill? I'm not sure you need to remove the index if you are going to do a reindex...

Upvotes: 3

Related Questions