Class: Philiprehberger::Batch::Chunk

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/philiprehberger/batch/chunk.rb

Overview

Represents a single chunk of items within a batch.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items:, index:, retries: 0) ⇒ Chunk

Returns a new instance of Chunk.

Parameters:

  • items (Array)

    items in this chunk

  • index (Integer)

    chunk index

  • retries (Integer) (defaults to: 0)

    max retries for failed items



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_callbackProc? (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.

Returns:

  • (Proc, nil)


86
87
88
# File 'lib/philiprehberger/batch/chunk.rb', line 86

def error_callback
  @error_callback
end

#errorsArray<Hash> (readonly)

Returns errors collected during iteration.

Returns:

  • (Array<Hash>)

    errors collected during iteration



30
31
32
# File 'lib/philiprehberger/batch/chunk.rb', line 30

def errors
  @errors
end

#indexInteger (readonly)

Returns zero-based index of this chunk.

Returns:

  • (Integer)

    zero-based index of this chunk



13
14
15
# File 'lib/philiprehberger/batch/chunk.rb', line 13

def index
  @index
end

#itemsArray (readonly)

Returns items in this chunk.

Returns:

  • (Array)

    items in this chunk



10
11
12
# File 'lib/philiprehberger/batch/chunk.rb', line 10

def items
  @items
end

#progress_callbackProc? (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.

Returns:

  • (Proc, nil)


82
83
84
# File 'lib/philiprehberger/batch/chunk.rb', line 82

def progress_callback
  @progress_callback
end

#resultsArray (readonly)

Returns results collected during iteration.

Returns:

  • (Array)

    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.

Yields:

  • (item)

    each item in the chunk



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.

Returns:

  • (Boolean)

    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.

Yields:

  • (item, error)

    called when an item fails



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.

Yields:

  • (info)

    called after the chunk is processed



59
60
61
# File 'lib/philiprehberger/batch/chunk.rb', line 59

def on_progress(&block)
  @progress_callback = block
end