Skip to content

Instantly share code, notes, and snippets.

@iain
Created March 10, 2011 22:17
Show Gist options
  • Save iain/865051 to your computer and use it in GitHub Desktop.
Save iain/865051 to your computer and use it in GitHub Desktop.
Sunspot helpers
# Tasks for capistrano
def bundler_rake(task_name)
task task_name, :roles => [:app] do
run "cd #{current_path} && bundle exec rake RAILS_ENV=#{rails_env} #{task_name}", :env => {'RAILS_ENV' => rails_env}
end
end
desc "Start Solr"
bundler_rake "solr:start"
desc "Stop Solr"
bundler_rake "solr:stop"
desc "Restart Solr"
bundler_rake "solr:restart"
desc "Reindex Solr"
bundler_rake "solr:reindex"
desc "Check if Solr is running"
bundler_rake "solr:check"
namespace :solr do
desc "Update the Solr directories after a deploy"
task :symlink, :except => { :no_release => true } do
run "ln -nfs #{shared_path}/solr/data #{current_path}/solr"
run "ln -nfs #{shared_path}/solr/lib #{current_path}/solr"
end
end
after "deploy:symlink", "solr:symlink"
after "deploy:migrate", "solr:reindex"
# For cucumber
# Sunspot Solr goodness
$original_sunspot_session = Sunspot.session
# if not search tag is set, stub sunspot
Before("~@search") do
Sunspot.session = Sunspot::Rails::StubSessionProxy.new($original_sunspot_session)
end
# if search tag is set, call sunspot server
Before("@search") do
unless $sunspot
$sunspot = Sunspot::Rails::Server.new
pid = fork do
STDERR.reopen('/dev/null')
STDOUT.reopen('/dev/null')
$sunspot.run
end
# shut down the Solr server
at_exit { Process.kill('TERM', pid) }
# wait for solr to start
startspot = Startspot.new
startspot.run
end
Sunspot.session = $original_sunspot_session
PrimaryReport.remove_all_from_index
SecondaryReport.remove_all_from_index
ThirdPartyContact.remove_all_from_index
Document.remove_all_from_index
end
# Rake tasks
require 'lib/startspot'
namespace :solr do
desc "Wait for Solr to become active. This might take a while"
task :wait do
startspot = Startspot.new
puts "Waiting for Solr #{startspot.info}"
startspot.run do
print '.'
end
puts "", startspot.log
end
desc "Start Solr, but don't raise an error if it's already running"
task :start do
startspot = Startspot.new
if startspot.listening?
puts "Solr is already up #{startspot.info}"
else
puts "Staring Solr #{startspot.info}"
Rake::Task['sunspot:solr:start'].invoke
Rake::Task['solr:wait'].invoke
end
end
desc "Reindex Solr, starting it if needed"
task :reindex do
startspot = Startspot.new
unless startspot.listening?
puts "Solr wasn't running, so I'll try to start it before reindexing."
Rake::Task['solr:start'].invoke
end
puts "Reindexing Solr #{startspot.info}"
Rake::Task['sunspot:solr:reindex'].invoke
end
desc "Stop Solr, if running."
task :stop do
startspot = Startspot.new
if startspot.listening?
puts "Stopping Solr #{startspot.info}"
Rake::Task['sunspot:solr:stop'].invoke
sleep 1 while startspot.listening?
else
puts "Solr wasn't running, so I didn't even try to stop it. #{startspot.info}"
end
end
desc "Restart Solr safely"
task :restart => [ :stop, :start ]
desc "Check if Solr is running"
task :check do
startspot = Startspot.new
if startspot.listening?
puts "OK #{startspot.info}"
else
puts "FAIL #{startspot.info}"
exit 1
end
end
end
# lib/startspot.rb
require 'yaml'
class Startspot
attr_accessor :config_file, :time
def initialize(config_file = nil)
self.config_file = config_file || 'config/sunspot.yml'
end
def config
@config ||= YAML.load(File.read(config_file))
end
def port
@port ||= config[environment]['solr']['port']
end
def listening?
`netstat -an` =~ /\b#{port}\b/m
end
def info
"(#{environment} environment on port #{port})"
end
def log
"Solr is up and listening after %0.2f seconds" % time.to_f
end
def run
timer do
wait do
yield if block_given?
end
end
end
def wait
until listening?
yield
sleep 1
end
end
def timer
start = Time.now
result = yield
self.time = Time.now - start
result
end
def environment
Rails.env.to_s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment