Skip to content

Instantly share code, notes, and snippets.

@cec
Created May 3, 2013 10:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cec/5508303 to your computer and use it in GitHub Desktop.
Save cec/5508303 to your computer and use it in GitHub Desktop.
Capistrano Tasks to setup and interact with SolR and SunSpot
namespace :deploy do
# Ask whether to reindex before restarting Passenger
task :restart, :roles => :app, :except => {:no_release => true} do
solr.reindex if 'y' == Capistrano::CLI.ui.ask("\n\n Should I reindex all models? (anything but y will cancel)")
run "touch #{File.join(current_path, 'tmp', 'restart.txt')}"
end
desc 'create shared data and pid dirs for Solr'
task :setup_solr_shared_dirs do
# conf dir is not shared as different versions need different configs
%w(data pids).each do |path|
run "mkdir -p #{shared_path}/solr/#{path}"
end
end
desc 'substituses current_path/solr/data and pids with symlinks to the shared dirs'
task :link_to_solr_shared_dirs do
%w(solr/data solr/pids).each do |solr_path|
run "rm -fr #{current_path}/#{solr_path}" #removing might not be necessary with proper .gitignore setup
run "ln -s #{shared_path}/#{solr_path} #{current_path}/#{solr_path}"
end
end
end
after 'deploy:setup', 'deploy:setup_solr_shared_dirs'
# rm and symlinks every time we finished uploading code and symlinking to the new release
after 'deploy:update', 'deploy:link_to_solr_shared_dirs'
# Tasks to interact with Solr and SunSpot
namespace :solr do
desc "start solr"
task :start, :roles => :app, :except => { :no_release => true } do
run "cd #{current_path} && RAILS_ENV=#{rails_env} bundle exec rake sunspot:solr:start"
end
desc "stop solr"
task :stop, :roles => :app, :except => { :no_release => true } do
run "cd #{current_path} && RAILS_ENV=#{rails_env} bundle exec rake sunspot:solr:stop"
end
desc "stop solr, remove data, start solr, reindex all records"
task :hard_reindex, :roles => :app do
stop
run "rm -rf #{shared_path}/solr/data/*"
start
reindex
end
desc "simple reindex" #note the yes | reindex to avoid the nil.chomp error
task :reindex, roles: :app do
run "cd #{current_path} && yes | RAILS_ENV=#{rails_env} bundle exec rake sunspot:solr:reindex"
end
end
@g8d3
Copy link

g8d3 commented May 29, 2013

seems nice, I will try this.

@g8d3
Copy link

g8d3 commented May 29, 2013

My setup task is not being executed, should I add this in config/deploy.rb? how?

@g8d3
Copy link

g8d3 commented May 29, 2013

I had to change this part:

run "mkdir #{current_path}/solr"
%w(solr/data solr/pids).each do |solr_path|
  run "rm -fr #{current_path}/#{solr_path}" #removing might not be necessary with proper .gitignore setup

Also, to because shared folders were not being created I used:

before 'deploy:update', 'deploy:setup_solr_shared_dirs'

@g8d3
Copy link

g8d3 commented May 29, 2013

I am having this:

 Error - RSolr::Error::Http - 500 Internal Server Error - retrying...

when reindexing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment