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_hash(data) ⇒ Object
Deserialize from an already-parsed Hash (e.g. what the storage adapter's
retrieve_contextreturns). - .deserialize_value(value) ⇒ Object
- .enrich_failure_for_api(hash) ⇒ Object
- .failure_payload?(hash) ⇒ Boolean
- .flatten_typed_failure(hash) ⇒ Object
- .resolve_failure_location(hash) ⇒ Object
- .serialize(context, job_id: nil, started_at: nil) ⇒ Object
-
.serialize_value(value) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength.
-
.simplify_for_api(value) ⇒ Object
Simplifies data for public API usage (removes wrappers, flattens types).
Class Method Details
.deserialize(serialized_data) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/ruby_reactor/context_serializer.rb', line 20 def deserialize(serialized_data) decompressed = decompress_if_needed(serialized_data) deserialize_hash(JSON.parse(decompressed, symbolize_names: false)) rescue JSON::ParserError => e raise RubyReactor::Error::DeserializationError, "Failed to parse serialized context: #{e.}" end |
.deserialize_hash(data) ⇒ Object
Deserialize from an already-parsed Hash (e.g. what the storage adapter's
retrieve_context returns). Lets the rehydrate-by-id worker path avoid a
second JSON parse while still schema-validating. Schema validation lives
here so both the string and Hash entry points enforce it.
31 32 33 34 |
# File 'lib/ruby_reactor/context_serializer.rb', line 31 def deserialize_hash(data) validate_schema_version(data) Context.deserialize_from_retry(data) end |
.deserialize_value(value) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ruby_reactor/context_serializer.rb', line 114 def deserialize_value(value) case value when Hash if value.key?("_type") # Special serialized types (Time, BigDecimal, etc.) case value["_type"] when "Halt" RubyReactor::Halt.new( reason: value["reason"], period_key: value["period_key"], step_name: value["step_name"] ) when "Skipped" RubyReactor::Skipped.new(deserialize_value(value["value"]), reason: value["reason"]) when "Success" RubyReactor::Success(deserialize_value(value["value"])) when "Failure" RubyReactor::Failure.new( deserialize_value(value["error"]), retryable: value["retryable"], step_name: value["step_name"], inputs: deserialize_value(value["inputs"]), backtrace: value["backtrace"], reactor_name: value["reactor_name"], step_arguments: deserialize_value(value["step_arguments"]), exception_class: value["exception_class"], file_path: value["file_path"], line_number: value["line_number"], code_snippet: deserialize_value(value["code_snippet"]), validation_errors: deserialize_value(value["validation_errors"]) ) when "Context" Context.deserialize_from_retry(value["value"]) when "Symbol" value["value"].to_sym 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" locate_global_id(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"]) when "Map::ResultEnumerator" RubyReactor::Map::ResultEnumerator.new( value["map_id"], value["reactor_class_name"], strict_ordering: value["strict_ordering"], batch_size: value["batch_size"] ) else # Unknown type wrapper, return as is (but deserialize values) value.transform_values { |v| deserialize_value(v) } end else # Regular Hash value.transform_keys(&:to_sym).transform_values { |v| deserialize_value(v) } end when Array value.map { |v| deserialize_value(v) } else value end end |
.enrich_failure_for_api(hash) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/ruby_reactor/context_serializer.rb', line 219 def enrich_failure_for_api(hash) return hash unless hash.is_a?(Hash) return hash unless failure_payload?(hash) hash = flatten_typed_failure(hash) if hash["_type"] == "Failure" return hash if hash["code_snippet"].is_a?(Array) && !hash["code_snippet"].empty? file_path, line_number = resolve_failure_location(hash) return hash unless file_path && line_number snippet = RubyReactor::Utils::CodeExtractor.extract(file_path, line_number) return hash unless snippet normalized_snippet = snippet.map { |line| line.transform_keys(&:to_s) } hash.merge( "file_path" => file_path, "line_number" => line_number, "code_snippet" => normalized_snippet ) end |
.failure_payload?(hash) ⇒ Boolean
242 243 244 |
# File 'lib/ruby_reactor/context_serializer.rb', line 242 def failure_payload?(hash) hash["_type"] == "Failure" || (hash.key?("step_name") && hash["backtrace"].is_a?(Array)) end |
.flatten_typed_failure(hash) ⇒ Object
246 247 248 249 250 |
# File 'lib/ruby_reactor/context_serializer.rb', line 246 def flatten_typed_failure(hash) flattened = hash.dup flattened.delete("_type") flattened.merge("message" => hash["error"] || hash["message"]) end |
.resolve_failure_location(hash) ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/ruby_reactor/context_serializer.rb', line 252 def resolve_failure_location(hash) file_path = hash["file_path"] line_number = hash["line_number"] return [file_path, line_number] if file_path && line_number RubyReactor::Utils::BacktraceLocation.extract(hash["backtrace"]) 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
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 79 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 |
# File 'lib/ruby_reactor/context_serializer.rb', line 37 def serialize_value(value) case value when RubyReactor::Halt { "_type" => "Halt", "reason" => value.reason, "period_key" => value.period_key, "step_name" => value.step_name } when RubyReactor::Skipped { "_type" => "Skipped", "value" => serialize_value(value.value), "reason" => value.reason } when RubyReactor::Success { "_type" => "Success", "value" => serialize_value(value.value) } when RubyReactor::Failure { "_type" => "Failure", "error" => serialize_value(value.error), "retryable" => value.retryable, "step_name" => value.step_name, "inputs" => serialize_value(value.inputs), "backtrace" => value.backtrace, "reactor_name" => value.reactor_name, "step_arguments" => serialize_value(value.step_arguments), "exception_class" => value.exception_class, "file_path" => value.file_path, "line_number" => value.line_number, "code_snippet" => serialize_value(value.code_snippet), "validation_errors" => serialize_value(value.validation_errors) } when RubyReactor::Context { "_type" => "Context", "value" => value.serialize_for_retry } when Symbol { "_type" => "Symbol", "value" => value.to_s } 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 RubyReactor::Map::ResultEnumerator { "_type" => "Map::ResultEnumerator", "map_id" => value.map_id, "reactor_class_name" => value.reactor_class_name, "strict_ordering" => value.strict_ordering, "batch_size" => value.batch_size } 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 |
.simplify_for_api(value) ⇒ Object
Simplifies data for public API usage (removes wrappers, flattens types)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/ruby_reactor/context_serializer.rb', line 199 def simplify_for_api(value) case value when RubyReactor::Map::ResultEnumerator RubyReactor::Map::ResultSummary.build(value) when Hash simplified = value.each_with_object({}) do |(k, v), hash| hash[k.to_s] = simplify_for_api(v) end enrich_failure_for_api(simplified) when Array value.map { |v| simplify_for_api(v) } when Success, Failure, Context enrich_failure_for_api(simplify_for_api(value.to_h)) when Symbol value.to_s else value end end |