Class: RocketJob::Sliced::Slice

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Plugins::Document, Plugins::StateMachine
Defined in:
lib/rocket_job/sliced/slice.rb

Overview

A slice is an Array of Records, along with meta-data that is used or set during processing of the individual records

Note: Do not create instances of this model directly, go via Slice#new

so that the correct collection name is used.

Example:

slice = RocketJob::Sliced::Slice.new
slice << 'first'
slice << 'second'
second = slice.at(1)

# The [] operator is for retrieving attributes:
slice['state']

Instance Method Summary collapse

Instance Method Details

#as_attributesObject



133
134
135
136
137
# File 'lib/rocket_job/sliced/slice.rb', line 133

def as_attributes
  attrs            = super
  attrs['records'] = serialize_records if @records
  attrs
end

#as_documentObject



139
140
141
142
143
# File 'lib/rocket_job/sliced/slice.rb', line 139

def as_document
  attrs            = super
  attrs['records'] = serialize_records if @records
  attrs
end

#failed_recordObject

Returns the failed record. Returns [nil] if there is no failed record



124
125
126
127
128
# File 'lib/rocket_job/sliced/slice.rb', line 124

def failed_record
  if exception && (record_number = exception.record_number)
    at(record_number - 1)
  end
end

#inspectObject



146
147
148
# File 'lib/rocket_job/sliced/slice.rb', line 146

def inspect
  "#{super[0...-1]}, records: #{@records.inspect}, collection_name: #{collection_name.inspect}>"
end

#recordsObject

‘records` array has special handling so that it can be modified in place instead of having to replace the entire array every time. For example, when appending lines with `<<`.



97
98
99
# File 'lib/rocket_job/sliced/slice.rb', line 97

def records
  @records ||= []
end

#records=(records) ⇒ Object

Replace the records within this slice

Raises:

  • (ArgumentError)


102
103
104
105
106
# File 'lib/rocket_job/sliced/slice.rb', line 102

def records=(records)
  raise(ArgumentError, "Cannot assign type: #{records.class.name} to records") unless records.is_a?(Array)

  @records = records
end

#set_exception(exc = nil, record_number = nil) ⇒ Object

Fail this slice, along with the exception that caused the failure



112
113
114
115
116
117
118
119
120
# File 'lib/rocket_job/sliced/slice.rb', line 112

def set_exception(exc = nil, record_number = nil)
  if exc
    self.exception          = JobException.from_exception(exc)
    exception.worker_name   = worker_name
    exception.record_number = record_number
  end
  self.failure_count = failure_count.to_i + 1
  self.worker_name   = nil
end