Class: RocketJob::Sliced::Slice
- Inherits:
-
Object
- Object
- RocketJob::Sliced::Slice
- 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
BZip2OutputSlice, CompressedSlice, EncryptedBZip2OutputSlice, EncryptedSlice
Class Method Summary collapse
-
.binary_format ⇒ Object
Returns whether this is a specialized binary slice for creating binary data from each slice that is downloaded without conversion into output files.
-
.to_binary(_records) ⇒ Object
For binary formats only, format the supplied records into the binary format for this slice.
Instance Method Summary collapse
-
#as_attributes ⇒ Object
Returns [Hash] the slice as a Hash for storage purposes Compresses / Encrypts the slice according to the job setting.
-
#current_record_number ⇒ Object
Returns [Integer] the record number of the record currently being processed relative to the entire file.
-
#fail_on_exception!(re_raise_exceptions = false, &block) ⇒ Object
Fail this slice if an exception occurs during processing.
-
#failed_record ⇒ Object
Returns the failed record.
- #inspect ⇒ Object
-
#records ⇒ Object
‘records` array has special handling so that it can be modified in place instead of having to replace the entire array every time.
-
#records=(records) ⇒ Object
Replace the records within this slice.
-
#set_exception(exc = nil) ⇒ Object
Before Fail save the exception to this slice.
Class Method Details
.binary_format ⇒ Object
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
103 104 105 |
# File 'lib/rocket_job/sliced/slice.rb', line 103 def self.to_binary(_records) raise NotImplementedError end |
Instance Method Details
#as_attributes ⇒ Object
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_number ⇒ Object
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_record ⇒ Object
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 |
#inspect ⇒ Object
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 |
#records ⇒ Object
‘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
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 |