Skip to content

Instantly share code, notes, and snippets.

@nz
Created October 10, 2012 19:06
Show Gist options
  • Save nz/3867730 to your computer and use it in GitHub Desktop.
Save nz/3867730 to your computer and use it in GitHub Desktop.
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