Created
October 10, 2012 19:06
-
-
Save nz/3867730 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task :reindex do | |
batch_size = 20 | |
models = [Foo, Bar, Baz] | |
models.each do |model| | |
model.find_in_batches(batch_size).each do |batch| | |
retries = 1 | |
begin | |
Sunspot.index(batch) | |
rescue Exception => e | |
puts "Exception on batch: #{e.message}" | |
if (retries -= 1) >= 0 | |
puts "Retrying..." | |
sleep 1 | |
retry | |
else | |
puts "Reindexing individually" | |
batch.each do |record| | |
begin | |
sleep 1 | |
Sunspot.index(record) | |
rescue Exception => e | |
puts "Second exception on #{record.inspect}: #{e.message}" | |
end | |
end # index each record | |
end | |
end # begin/rescue batch index | |
end # find_in_batches | |
end # each model | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment