Created
July 28, 2011 23:04
-
-
Save vanstee/1112778 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
def reindex_companies_since(timestamp) | |
conditions = ["updated_at >= ?", timestamp] | |
total = Company.count(:conditions => conditions) | |
indexed = 0 | |
companies = Queue.new | |
indexing = true | |
Thread.abort_on_exception = true | |
producer = Thread.new do | |
Company.find_in_batches(:conditions => conditions) do |batch| | |
sleep 1 and puts companies.length while companies.length < 4 | |
companies.push(batch) | |
indexed += batch.count | |
puts "Indexed #{indexed} of #{total} companies..." | |
end | |
indexing = false | |
end | |
consumers = Array.new(4) do | |
Thread.new do | |
while indexing || !companies.empty? | |
begin | |
Sunspot.index!(*companies.pop) | |
rescue ThreadError => e | |
sleep 1 | |
rescue Exception => e | |
$stderr.puts "ERROR encountered: #{e.message}" | |
$stderr.puts e.backtrace.join("\n") | |
sleep 10 | |
retry | |
end | |
end | |
end | |
end | |
consumers.each(&:join) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment