Search code examples
ruby-on-railscapistranosunspotsunspot-solrrvm-capistrano

Reindex Sunspot Solr in Capistrano


I'm following this snippet for adding Sunspot Solr in Capistrano https://gist.github.com/doitian/1795439 and it works, but when I run cap solr:reindex it shows a question (as expected) asking if I want to drop all indexes, so I answer with "yes" but the terminal seems to be waiting for a response.

This is the code for the reindex:

  desc "reindex the whole database"
  task :reindex, :roles => :app do
    stop
    run "rm -rf #{shared_path}/solr/data"
    start
    run "cd #{current_path} && #{rake} RAILS_ENV=#{rails_env} sunspot:solr:reindex" 
  end

This is the message:

  * executing "cd /home/user/rapps/app/current && bundle exec rake RAILS_ENV=production sunspot:solr:reindex"
    servers: ["9.9.9.9"]
    [9.9.9.9] executing command
 ** [out :: 9.9.9.9] *Note: the reindex task will remove your current indexes and start from scratch.
 ** [out :: 9.9.9.9] If you have a large dataset, reindexing can take a very long time, possibly weeks.
 ** [out :: 9.9.9.9] This is not encouraged if you have anywhere near or over 1 million rows.
 ** [out :: 9.9.9.9] Are you sure you want to drop your indexes and completely reindex? (y/n)
y

How can avoid that message? Also, I will like to run this rake with Cron so how could I previously give the answer or set a default configuration? And what is the best/efficient way to reindex?

Thanks in advance.


Solution

  • After looking at the sunspot_rails source, there currently is no way to force your way past this message. See the rake task code defined here

    There has been a pull request submitted (a month ago) (Pull request here) for the rake task to support a silent option, but it has not been merged yet.

    I would either:

    1. Fork the sunspot gem Repo located here and make the changes yourself to the rake task (then change your environment Gemfile to use your forked gem).
    2. Add a sleep command to your Capistrano task (something like 10 seconds, longer then it should ever take for this command to appear), and then provide the standard input with "y"