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']

Direct Known Subclasses

CompressedSlice, EncryptedSlice

Instance Method Summary collapse

Instance Method Details

#as_attributesObject



137
138
139
140
141
# File 'lib/rocket_job/sliced/slice.rb', line 137

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

#as_documentObject



143
144
145
146
147
# File 'lib/rocket_job/sliced/slice.rb', line 143

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

#current_record_numberObject

Returns [Integer] the record number of the record currently being processed relative to the entire file.



114
115
116
# File 'lib/rocket_job/sliced/slice.rb', line 114

def current_record_number
  first_record_number + (processing_record_number || 1) - 1
end

#fail_on_exception!(re_raise_exceptions = false, &block) ⇒ Object

Fail this slice if an exception occurs during processing.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rocket_job/sliced/slice.rb', line 155

def fail_on_exception!(re_raise_exceptions = false, &block)
  SemanticLogger.named_tagged(slice: id.to_s, &block)
rescue Exception => e
  SemanticLogger.named_tagged(slice: id.to_s) do
    if failed? || !may_fail?
      exception             = JobException.from_exception(e)
      exception.worker_name = worker_name
      save! unless new_record? || destroyed?
    elsif new_record? || destroyed?
      fail(e)
    else
      fail!(e)
    end
    raise e if re_raise_exceptions
  end
end

#failed_recordObject

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



130
131
132
# File 'lib/rocket_job/sliced/slice.rb', line 130

def failed_record
  at(processing_record_number - 1) if exception && processing_record_number
end

#inspectObject



150
151
152
# File 'lib/rocket_job/sliced/slice.rb', line 150

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 `<<`.



99
100
101
# File 'lib/rocket_job/sliced/slice.rb', line 99

def records
  @records ||= []
end

#records=(records) ⇒ Object

Replace the records within this slice

Raises:

  • (ArgumentError)


104
105
106
107
108
# File 'lib/rocket_job/sliced/slice.rb', line 104

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) ⇒ Object

Before Fail save the exception to this slice.



119
120
121
122
123
124
125
126
# File 'lib/rocket_job/sliced/slice.rb', line 119

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