Class: Sunspot::IndexQueue::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/index_queue/batch.rb

Overview

Batch of entries to be indexed with Solr.

Constant Summary collapse

PASS_THROUGH_EXCEPTIONS =

Errors that cause batch processing to stop and are immediately passed on to the caller. All other are logged on the entry on the assumption that they can be fixed later while other entries can still be processed.

[SystemExit, NoMemoryError, Interrupt, SignalException, Errno::ECONNREFUSED]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue, entries = nil) ⇒ Batch

Returns a new instance of Batch.



12
13
14
15
16
17
# File 'lib/sunspot/index_queue/batch.rb', line 12

def initialize(queue, entries = nil)
  @queue = queue
  @entries = []
  @entries.concat(entries) if entries
  @delete_entries = []
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



5
6
7
# File 'lib/sunspot/index_queue/batch.rb', line 5

def entries
  @entries
end

Instance Method Details

#submit!Object

Submit the entries to solr. If they are successfully committed, the entries will be deleted. Otherwise, any entries that generated errors will be updated with the error messages and set to be processed again in the future.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sunspot/index_queue/batch.rb', line 22

def submit!
  Entry.load_all_records(entries)
  clear_processed(entries)
  begin
    # First try submitting the entries in a batch since that's the most efficient.
    # If there are errors, try each entry individually in case there's a bad document.
    session.batch do
      entries.each do |entry|
        submit_entry(entry)
      end
    end
    commit!
  rescue Exception => e
    @delete_entries.clear
    entries.each{|entry| entry.processed = false}
    if PASS_THROUGH_EXCEPTIONS.include?(e.class)
      raise e
    else
      submit_each_entry
    end
  end
rescue Exception => e
  begin
    clear_processed(entries)
    entries.each{|entry| entry.reset!} if PASS_THROUGH_EXCEPTIONS.include?(e.class)
  ensure
    # Use a more specific error to indicate Solr is down.
    e = SolrNotResponding.new(e.message) if e.is_a?(Errno::ECONNREFUSED)
    raise e
  end
end