Skip to content

Instantly share code, notes, and snippets.

@muscardinus
Last active December 17, 2023 03:04
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save muscardinus/8884801 to your computer and use it in GitHub Desktop.
Save muscardinus/8884801 to your computer and use it in GitHub Desktop.
sunspot tasks for Capistrano 3
namespace :deploy do
before :updated, :setup_solr_data_dir do
on roles(:app) do
unless test "[ -d #{shared_path}/solr/data ]"
execute :mkdir, "-p #{shared_path}/solr/data"
end
end
end
end
namespace :solr do
%w[start stop].each do |command|
desc "#{command} solr"
task command do
on roles(:app) do
solr_pid = "#{shared_path}/pids/sunspot-solr.pid"
if command == "start" or (test "[ -f #{solr_pid} ]" and test "kill -0 $( cat #{solr_pid} )")
within current_path do
with rails_env: fetch(:rails_env, 'production') do
execute :bundle, 'exec', 'sunspot-solr', command, "--port=8983 --data-directory=#{shared_path}/solr/data --pid-dir=#{shared_path}/pids"
end
end
end
end
end
end
desc "restart solr"
task :restart do
invoke 'solr:stop'
invoke 'solr:start'
end
after 'deploy:finished', 'solr:restart'
desc "reindex the whole solr database"
task :reindex do
invoke 'solr:stop'
on roles(:app) do
execute :rm, "-rf #{shared_path}/solr/data"
end
invoke 'solr:start'
on roles(:app) do
within current_path do
with rails_env: fetch(:rails_env, 'production') do
info "Reindexing Solr database"
execute :bundle, 'exec', :rake, 'sunspot:solr:reindex[,,true]'
end
end
end
end
end
@cspeer
Copy link

cspeer commented Apr 24, 2014

Thanks a lot!

@westonplatter
Copy link

@muscardinus thanks for the cap 3 update

@Riddlerrr
Copy link

Thanks!

@DrewDahlman
Copy link

Awesome! Thanks for this!

@yannvery
Copy link

Thanks

@FlowerWrong
Copy link

Hi! I get an error.

rails 4.1.6
ruby 2.1.3

G[d8a6c69d] Command: cd /home/ubuntu/www/thecampus/current && ( RAILS_ENV=production ~/.rvm/bin/rvm default do bundle exec rake sunspot:solr:reindex )
DEBUG[d8a6c69d]     rake aborted!
DEBUG[d8a6c69d]     Errno::ECONNREFUSED: Connection refused - {:data=>"<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><query>type:Activity</query></delete>", :headers=>{"Content-Type"=>"text/xml"}, :method=>:post, :params=>{:wt=>:ruby}, :query=>"wt=ruby", :path=>"update", :uri=>#<URI::HTTP:0x000000039a2448 URL:http://localhost:8983/solr/default/update?wt=ruby>, :open_timeout=>nil, :read_timeout=>nil, :retry_503=>nil, :retry_after_limit=>nil}

Do you know why?
note: when I ssh to the server. I can exec RAILS_ENV=production rake sunspot:solr:reindex was success.

@adrianoresende
Copy link

Thanks a lot! =D

@lalitlogical
Copy link

Thanks, but this solution does not work for me.

This gist have the deprecated part as --data-directory has been replaced with --solr-home.

execute :bundle, 'exec', 'sunspot-solr', command, "--port=8983 --data-directory=#{shared_path}/solr/data --pid-dir=#{shared_path}/pids"

I had replaced it with below as.

execute :bundle, 'exec', 'sunspot-solr', command, "--port=8983 --solr-home=#{shared_path}/solr --pid-dir=#{shared_path}/pids"

But it also not works for me. I have below version for Sunspot, etc.

sunspot_solr 2.2.7
sunspot 2.2.7
sunspot_rails 2.2.7

For Capistrano 3, I have made an updated version: https://gist.github.com/lalitlogical/4c8c047b95fe45da6ae19d1b59411783

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