Class: RubyReactor::ContextSerializer
- Inherits:
-
Object
- Object
- RubyReactor::ContextSerializer
- Defined in:
- lib/ruby_reactor/context_serializer.rb
Overview
Utility class for handling context serialization and deserialization
Constant Summary collapse
- MAX_CONTEXT_SIZE =
512MB Redis limit
512 * 1024 * 1024
- SCHEMA_VERSION =
"1.0"
Class Method Summary collapse
- .deserialize(serialized_data) ⇒ Object
- .deserialize_value(value) ⇒ Object
- .serialize(context, job_id: nil, started_at: nil) ⇒ Object
-
.serialize_value(value) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength.
Class Method Details
.deserialize(serialized_data) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ruby_reactor/context_serializer.rb', line 20 def deserialize(serialized_data) decompressed = decompress_if_needed(serialized_data) data = JSON.parse(decompressed, symbolize_names: false) validate_schema_version(data) Context.deserialize_from_retry(data) rescue JSON::ParserError => e raise RubyReactor::Error::DeserializationError, "Failed to parse serialized context: #{e.message}" end |
.deserialize_value(value) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ruby_reactor/context_serializer.rb', line 80 def deserialize_value(value) case value when Hash if value.key?("_type") # Special serialized types (Time, BigDecimal, etc.) case value["_type"] when "Success" RubyReactor::Success(deserialize_value(value["value"])) when "Failure" RubyReactor::Failure(deserialize_value(value["error"]), retryable: value["retryable"]) when "Context" Context.deserialize_from_retry(value["value"]) when "Time" Time.iso8601(value["value"]) when "BigDecimal" BigDecimal(value["value"]) when "Rational" Rational(value["numerator"], value["denominator"]) when "Date" Date.iso8601(value["value"]) when "DateTime" DateTime.iso8601(value["value"]) when "Complex" Complex(value["real"], value["imag"]) when "Range" Range.new(deserialize_value(value["begin"]), deserialize_value(value["end"]), value["exclude_end"]) when "Regexp" Regexp.new(value["source"], value["options"]) when "GlobalID" GlobalID::Locator.locate(value["gid"]) when "Template::Element" RubyReactor::Template::Element.new(value["map_name"], value["path"]) when "Template::Input" RubyReactor::Template::Input.new(value["name"], value["path"]) when "Template::Value" RubyReactor::Template::Value.new(deserialize_value(value["value"])) when "Template::Result" RubyReactor::Template::Result.new(value["step_name"], value["path"]) else value end else # Regular hash - symbolize all keys recursively value.transform_keys(&:to_sym).transform_values { |v| deserialize_value(v) } end when Array value.map { |v| deserialize_value(v) } else value end end |
.serialize(context, job_id: nil, started_at: nil) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/ruby_reactor/context_serializer.rb', line 10 def serialize(context, job_id: nil, started_at: nil) data = context.serialize_for_retry(job_id: job_id, started_at: started_at) data[:schema_version] = SCHEMA_VERSION serialized = JSON.generate(data) validate_size(serialized) compress_if_needed(serialized) end |
.serialize_value(value) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ruby_reactor/context_serializer.rb', line 32 def serialize_value(value) case value when RubyReactor::Success { "_type" => "Success", "value" => serialize_value(value.value) } when RubyReactor::Failure { "_type" => "Failure", "error" => serialize_value(value.error), "retryable" => value.retryable } when RubyReactor::Context { "_type" => "Context", "value" => value.serialize_for_retry } when Time { "_type" => "Time", "value" => value.iso8601 } when BigDecimal { "_type" => "BigDecimal", "value" => value.to_s("F") } when Rational { "_type" => "Rational", "numerator" => value.numerator, "denominator" => value.denominator } when Date { "_type" => "Date", "value" => value.iso8601 } when DateTime { "_type" => "DateTime", "value" => value.iso8601 } when Complex { "_type" => "Complex", "real" => value.real, "imag" => value.imag } when Range { "_type" => "Range", "begin" => serialize_value(value.begin), "end" => serialize_value(value.end), "exclude_end" => value.exclude_end? } when Regexp { "_type" => "Regexp", "source" => value.source, "options" => value. } when ->(v) { v.respond_to?(:to_global_id) } { "_type" => "GlobalID", "gid" => value.to_global_id.to_s } when RubyReactor::Template::Element { "_type" => "Template::Element", "map_name" => value.map_name.to_s, "path" => value.path } when RubyReactor::Template::Input { "_type" => "Template::Input", "name" => value.name.to_s, "path" => value.path } when RubyReactor::Template::Value # Actually Template::Value holds a raw value. We should probably just serialize the raw value if possible, # or keep it as a template if we need to distinguish. # But wait, Template::Value is used to wrap raw values in arguments. # If we serialize it, we should probably deserialize it back to Template::Value. { "_type" => "Template::Value", "value" => serialize_value(value.instance_variable_get(:@value)) } when RubyReactor::Template::Result { "_type" => "Template::Result", "step_name" => value.step_name.to_s, "path" => value.path } when Hash value.transform_keys(&:to_s).transform_values { |v| serialize_value(v) } when Array value.map { |v| serialize_value(v) } else value end end |