Skip to content

Instantly share code, notes, and snippets.

@vanstee
Created July 28, 2011 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanstee/1112778 to your computer and use it in GitHub Desktop.
Save vanstee/1112778 to your computer and use it in GitHub Desktop.
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