class EXEL::Context
The Context
is the shared memory of a running job. It acts as the source of input to processors and the place for them to store their outputs. It can be serialized and deserialized to support remote execution.
Public Class Methods
Given a string representing the URI to a serialized context, downloads and returns the deserialized context
@return [Context]
# File lib/exel/context.rb, line 34 def self.deserialize(uri) file = EXEL::Value.localize(uri) begin context = Marshal.load(file.read) # rubocop:disable Airbnb/UnsafeYamlMarshal ensure file.close end context end
Accepts an optional hash of keys and values to initialize the context with.
# File lib/exel/context.rb, line 11 def initialize(initial_context = {}) super() merge!(initial_context) end
Public Instance Methods
Returns the value referenced by the given key. If it is a remote value, it will be converted to a local value and the local value will be returned.
# File lib/exel/context.rb, line 48 def [](key) convert_value!(key, super(key)) end
Returns a deep copy of this context. The copy and the original will have no shared object references.
@return [Context]
# File lib/exel/context.rb, line 19 def deep_dup Context.deserialize(serialize) end
# File lib/exel/context.rb, line 52 def fetch(key) convert_value!(key, super(key)) end
Serializes this instance to a local file and uses the remote provider to upload it. Returns a URI indicating where the serialized context can be downloaded.
@return [String] A URI such as s3://bucket/file
, file:///path/to/file
, etc.
# File lib/exel/context.rb, line 27 def serialize EXEL::Value.remotize(serialized_context) end
Private Instance Methods
# File lib/exel/context.rb, line 69 def convert_value!(key, value) value = EXEL::Value.localize(value) value = DeferredContextValue.resolve(value, self) self[key] = value end
# File lib/exel/context.rb, line 65 def remotized_table each_with_object({}) { |(key, value), acc| acc[key] = EXEL::Value.remotize(value) } end
# File lib/exel/context.rb, line 58 def serialized_context file = Tempfile.new(SecureRandom.uuid, encoding: 'ascii-8bit') file.write(Marshal.dump(Context.new(remotized_table))) file.rewind file end