Class: Philiprehberger::Batch::Chunk
- Inherits:
-
Object
- Object
- Philiprehberger::Batch::Chunk
- Includes:
- Enumerable
- Defined in:
- lib/philiprehberger/batch/chunk.rb
Overview
Represents a single chunk of items within a batch.
Instance Attribute Summary collapse
- #error_callback ⇒ Proc? readonly private
-
#errors ⇒ Array<Hash>
readonly
Errors collected during iteration.
-
#index ⇒ Integer
readonly
Zero-based index of this chunk.
-
#items ⇒ Array
readonly
Items in this chunk.
- #progress_callback ⇒ Proc? readonly private
-
#results ⇒ Array
readonly
Results collected during iteration.
Instance Method Summary collapse
-
#each {|item| ... } ⇒ void
Iterate over items in the chunk, capturing errors per item.
-
#halted? ⇒ Boolean
Whether processing was halted by error handler.
-
#initialize(items:, index:, retries: 0) ⇒ Chunk
constructor
A new instance of Chunk.
-
#on_error {|item, error| ... } ⇒ void
Register an error callback.
-
#on_progress {|info| ... } ⇒ void
Register a progress callback.
Constructor Details
#initialize(items:, index:, retries: 0) ⇒ Chunk
Returns a new instance of Chunk.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/philiprehberger/batch/chunk.rb', line 18 def initialize(items:, index:, retries: 0) @items = items @index = index @retries = retries @progress_callback = nil @error_callback = nil @errors = [] @results = [] @halted = false end |
Instance Attribute Details
#error_callback ⇒ Proc? (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
86 87 88 |
# File 'lib/philiprehberger/batch/chunk.rb', line 86 def error_callback @error_callback end |
#errors ⇒ Array<Hash> (readonly)
Returns errors collected during iteration.
30 31 32 |
# File 'lib/philiprehberger/batch/chunk.rb', line 30 def errors @errors end |
#index ⇒ Integer (readonly)
Returns zero-based index of this chunk.
13 14 15 |
# File 'lib/philiprehberger/batch/chunk.rb', line 13 def index @index end |
#items ⇒ Array (readonly)
Returns items in this chunk.
10 11 12 |
# File 'lib/philiprehberger/batch/chunk.rb', line 10 def items @items end |
#progress_callback ⇒ Proc? (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
82 83 84 |
# File 'lib/philiprehberger/batch/chunk.rb', line 82 def progress_callback @progress_callback end |
#results ⇒ Array (readonly)
Returns results collected during iteration.
33 34 35 |
# File 'lib/philiprehberger/batch/chunk.rb', line 33 def results @results end |
Instance Method Details
#each {|item| ... } ⇒ void
This method returns an undefined value.
Iterate over items in the chunk, capturing errors per item. Failed items are retried up to the configured number of retries with exponential backoff. Only the failed items are retried.
46 47 48 49 50 51 52 53 |
# File 'lib/philiprehberger/batch/chunk.rb', line 46 def each(&block) @items.each do |item| break if @halted result = process_item_with_retries(item, &block) @results << result unless result == :__batch_error__ end end |
#halted? ⇒ Boolean
Returns whether processing was halted by error handler.
36 37 38 |
# File 'lib/philiprehberger/batch/chunk.rb', line 36 def halted? @halted end |
#on_error {|item, error| ... } ⇒ void
This method returns an undefined value.
Register an error callback. If errors have already been collected, the callback is invoked retroactively for each one. Return :halt from the callback to stop processing remaining items.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/philiprehberger/batch/chunk.rb', line 69 def on_error(&block) @error_callback = block @errors.each do |err| signal = block.call(err[:item], err[:error]) if signal == :halt @halted = true break end end end |
#on_progress {|info| ... } ⇒ void
This method returns an undefined value.
Register a progress callback.
59 60 61 |
# File 'lib/philiprehberger/batch/chunk.rb', line 59 def on_progress(&block) @progress_callback = block end |