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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.binary_formatObject

Returns whether this is a specialized binary slice for creating binary data from each slice that is downloaded without conversion into output files.



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

def self.binary_format
end

.to_binary(_records) ⇒ Object

For binary formats only, format the supplied records into the binary format for this slice

Raises:

  • (NotImplementedError)


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

def self.to_binary(_records)
  raise NotImplementedError
end

Instance Method Details

#as_attributesObject

Returns [Hash] the slice as a Hash for storage purposes Compresses / Encrypts the slice according to the job setting



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

def as_attributes
  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.



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

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.



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

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



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

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

#inspectObject



152
153
154
# File 'lib/rocket_job/sliced/slice.rb', line 152

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



109
110
111
# File 'lib/rocket_job/sliced/slice.rb', line 109

def records
  @records ||= []
end

#records=(records) ⇒ Object

Replace the records within this slice

Raises:

  • (ArgumentError)


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

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.



129
130
131
132
133
134
135
136
# File 'lib/rocket_job/sliced/slice.rb', line 129

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