Class: RubyReactor::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_reactor/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inputs = {}, reactor_class = nil) ⇒ Context

Returns a new instance of Context.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_reactor/context.rb', line 9

def initialize(inputs = {}, reactor_class = nil)
  @context_id = SecureRandom.uuid
  @inputs = inputs
  @intermediate_results = {}
  @private_data = {}
  @composed_contexts = {}
  @map_operations = {}
  @map_metadata = nil
  @current_step = nil
  @retry_count = 0
  @concurrency_key = nil
  @retry_context = RetryContext.new
  @reactor_class = reactor_class
  @execution_trace = []
  @inline_async_execution = false # Flag to prevent nested async calls
  @undo_stack = [] # Initialize the undo stack
  @test_mode = false
  @parent_context = nil
  @root_context = nil
end

Instance Attribute Details

#composed_contextsObject

Returns the value of attribute composed_contexts.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def composed_contexts
  @composed_contexts
end

#concurrency_keyObject

Returns the value of attribute concurrency_key.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def concurrency_key
  @concurrency_key
end

#context_idObject

Returns the value of attribute context_id.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def context_id
  @context_id
end

#current_stepObject

Returns the value of attribute current_step.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def current_step
  @current_step
end

#execution_traceObject

Returns the value of attribute execution_trace.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def execution_trace
  @execution_trace
end

#inline_async_executionObject

Returns the value of attribute inline_async_execution.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def inline_async_execution
  @inline_async_execution
end

#inputsObject

Returns the value of attribute inputs.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def inputs
  @inputs
end

#intermediate_resultsObject

Returns the value of attribute intermediate_results.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def intermediate_results
  @intermediate_results
end

#map_metadataObject

Returns the value of attribute map_metadata.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def 
  @map_metadata
end

#map_operationsObject

Returns the value of attribute map_operations.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def map_operations
  @map_operations
end

#parent_contextObject

Returns the value of attribute parent_context.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def parent_context
  @parent_context
end

#private_dataObject

Returns the value of attribute private_data.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def private_data
  @private_data
end

#reactor_classObject

Returns the value of attribute reactor_class.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def reactor_class
  @reactor_class
end

#retry_contextObject

Returns the value of attribute retry_context.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def retry_context
  @retry_context
end

#retry_countObject

Returns the value of attribute retry_count.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def retry_count
  @retry_count
end

#root_contextObject

Returns the value of attribute root_context.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def root_context
  @root_context
end

#test_modeObject

Returns the value of attribute test_mode.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def test_mode
  @test_mode
end

#undo_stackObject

Returns the value of attribute undo_stack.



5
6
7
# File 'lib/ruby_reactor/context.rb', line 5

def undo_stack
  @undo_stack
end

Class Method Details

.deserialize_from_retry(data) ⇒ Object



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
# File 'lib/ruby_reactor/context.rb', line 102

def self.deserialize_from_retry(data)
  context = new
  context.context_id = data["context_id"] if data["context_id"]
  context.reactor_class = data["reactor_class"] ? Object.const_get(data["reactor_class"]) : nil
  context.inputs = ContextSerializer.deserialize_value(data["inputs"]) || {}
  context.intermediate_results = ContextSerializer.deserialize_value(data["intermediate_results"]) || {}
  context.private_data = ContextSerializer.deserialize_value(data["private_data"]) || {}
  context.composed_contexts = ContextSerializer.deserialize_value(data["composed_contexts"]) || {}
  context.map_operations = ContextSerializer.deserialize_value(data["map_operations"]) || {}
  context. = ContextSerializer.deserialize_value(data["map_metadata"])
  context.current_step = data["current_step"]&.to_sym
  context.retry_count = data["retry_count"] || 0
  context.concurrency_key = data["concurrency_key"]
  context.retry_context = RetryContext.deserialize_from_retry(data["retry_context"] || {})
  context.execution_trace = ContextSerializer.deserialize_value(data["execution_trace"]) || []
  context.undo_stack = deserialize_undo_stack(data["undo_stack"] || [], context.reactor_class)
  context.test_mode = data["test_mode"] || false

  # Reconstruct parent/root relationships if nested contexts exist in private_data
  # This is tricky because private_data is just a hash.
  # We rely on the fact that nested contexts are stored in private_data by ComposeStep
  # But here we just deserialize the values.

  context
end

Instance Method Details

#get_input(name, path = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_reactor/context.rb', line 30

def get_input(name, path = nil)
  value = @inputs[name.to_sym] || @inputs[name.to_s]
  return nil if value.nil?

  if path
    extract_path(value, path)
  else
    value
  end
end

#get_result(step_name, path = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_reactor/context.rb', line 41

def get_result(step_name, path = nil)
  value = @intermediate_results[step_name.to_sym] || @intermediate_results[step_name.to_s]
  return nil if value.nil?

  if path
    extract_path(value, path)
  else
    value
  end
end

#serialize_for_retry(job_id: nil, started_at: nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby_reactor/context.rb', line 80

def serialize_for_retry(job_id: nil, started_at: nil)
  {
    job_id: job_id,
    context_id: @context_id,
    started_at: (started_at || Time.now).iso8601,
    reactor_class: @reactor_class&.name,
    inputs: ContextSerializer.serialize_value(@inputs),
    intermediate_results: ContextSerializer.serialize_value(@intermediate_results),
    private_data: ContextSerializer.serialize_value(@private_data),
    composed_contexts: ContextSerializer.serialize_value(@composed_contexts),
    map_operations: ContextSerializer.serialize_value(@map_operations),
    map_metadata: ContextSerializer.serialize_value(@map_metadata),
    current_step: @current_step,
    retry_count: @retry_count,
    concurrency_key: @concurrency_key,
    retry_context: @retry_context.serialize_for_retry,
    execution_trace: ContextSerializer.serialize_value(@execution_trace),
    undo_stack: serialize_undo_stack,
    test_mode: @test_mode
  }
end

#set_result(step_name, value) ⇒ Object



52
53
54
# File 'lib/ruby_reactor/context.rb', line 52

def set_result(step_name, value)
  @intermediate_results[step_name.to_sym] = value
end

#to_hObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_reactor/context.rb', line 64

def to_h
  {
    inputs: @inputs,
    intermediate_results: @intermediate_results,
    composed_contexts: @composed_contexts,
    map_operations: @map_operations,
    map_metadata: @map_metadata,
    current_step: @current_step,
    retry_count: @retry_count,
    retry_context: @retry_context,
    reactor_class: @reactor_class,
    execution_trace: @execution_trace,
    test_mode: @test_mode
  }
end

#with_step(step_name) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/ruby_reactor/context.rb', line 56

def with_step(step_name)
  old_step = @current_step
  @current_step = step_name
  yield
ensure
  @current_step = old_step
end