Class: RubyReactor::Reactor
- Inherits:
-
Object
- Object
- RubyReactor::Reactor
- Includes:
- Dsl::Lockable, Dsl::Reactor
- Defined in:
- lib/ruby_reactor/reactor.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#execution_trace ⇒ Object
readonly
Returns the value of attribute execution_trace.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#undo_trace ⇒ Object
readonly
Returns the value of attribute undo_trace.
Class Method Summary collapse
- .cancel(id:, reason:) ⇒ Object
- .configuration ⇒ Object
- .continue(id:, payload:, step_name:, idempotency_key: nil) ⇒ Object
- .continue_by_correlation_id(correlation_id:, payload:, step_name:, idempotency_key: nil) ⇒ Object
- .find(id) ⇒ Object
- .find_by_correlation_id(correlation_id) ⇒ Object
- .undo(id) ⇒ Object
Instance Method Summary collapse
- #cancel(reason) ⇒ Object
-
#continue(payload:, step_name:, idempotency_key: nil) ⇒ Object
rubocop:enable Metrics/MethodLength, Metrics/AbcSize.
-
#initialize(context = {}) ⇒ Reactor
constructor
A new instance of Reactor.
-
#run(inputs = {}) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #undo ⇒ Object
- #validate! ⇒ Object
Methods included from Dsl::Lockable
Methods included from Dsl::Reactor
Constructor Details
#initialize(context = {}) ⇒ Reactor
Returns a new instance of Reactor.
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ruby_reactor/reactor.rb', line 73 def initialize(context = {}) @context = context @result = :unexecuted if @context.is_a?(Context) @execution_trace = @context.execution_trace || [] @undo_trace = @execution_trace.select { |e| e[:type] == :undo } @result = reconstruct_result else @undo_trace = [] @execution_trace = [] end end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
9 10 11 |
# File 'lib/ruby_reactor/reactor.rb', line 9 def context @context end |
#execution_trace ⇒ Object (readonly)
Returns the value of attribute execution_trace.
9 10 11 |
# File 'lib/ruby_reactor/reactor.rb', line 9 def execution_trace @execution_trace end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
9 10 11 |
# File 'lib/ruby_reactor/reactor.rb', line 9 def result @result end |
#undo_trace ⇒ Object (readonly)
Returns the value of attribute undo_trace.
9 10 11 |
# File 'lib/ruby_reactor/reactor.rb', line 9 def undo_trace @undo_trace end |
Class Method Details
.cancel(id:, reason:) ⇒ Object
58 59 60 61 |
# File 'lib/ruby_reactor/reactor.rb', line 58 def self.cancel(id:, reason:) reactor = find(id) reactor.cancel(reason) end |
.configuration ⇒ Object
69 70 71 |
# File 'lib/ruby_reactor/reactor.rb', line 69 def self.configuration RubyReactor::Configuration.instance end |
.continue(id:, payload:, step_name:, idempotency_key: nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ruby_reactor/reactor.rb', line 38 def self.continue(id:, payload:, step_name:, idempotency_key: nil) reactor = find(id) result = reactor.continue(payload: payload, step_name: step_name, idempotency_key: idempotency_key) if result.is_a?(RubyReactor::Failure) && result.respond_to?(:invalid_payload?) && result.invalid_payload? # Raise exception to match expected behavior (strict mode for class method) # We do NOT cancel the reactor, allowing the user to retry with valid payload raise Error::InputValidationError, result.error end result end |
.continue_by_correlation_id(correlation_id:, payload:, step_name:, idempotency_key: nil) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/ruby_reactor/reactor.rb', line 51 def self.continue_by_correlation_id(correlation_id:, payload:, step_name:, idempotency_key: nil) reactor = find_by_correlation_id(correlation_id) # We delegate to the class-level continue method to ensure auto-compensation logic applies # by using the context ID found by find_by_correlation_id continue(id: reactor.context.context_id, payload: payload, step_name: step_name, idempotency_key: idempotency_key) end |
.find(id) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ruby_reactor/reactor.rb', line 11 def self.find(id) reactor_class_name = name raw_data = configuration.storage_adapter.retrieve_context(id, reactor_class_name) raise Error::ValidationError, "Context '#{id}' not found" unless raw_data context = case raw_data when String ContextSerializer.deserialize(raw_data) when Hash Context.deserialize_from_retry(raw_data) else raise Error::ValidationError, "Invalid context format for '#{id}'" end new(context) end |
.find_by_correlation_id(correlation_id) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ruby_reactor/reactor.rb', line 27 def self.find_by_correlation_id(correlation_id) reactor_class_name = name context_id = configuration.storage_adapter.retrieve_context_id_by_correlation_id( correlation_id, reactor_class_name ) raise Error::ValidationError, "Correlation ID '#{correlation_id}' not found" unless context_id find(context_id) end |
.undo(id) ⇒ Object
63 64 65 66 67 |
# File 'lib/ruby_reactor/reactor.rb', line 63 def self.undo(id) reactor = find(id) reactor.undo cancel(id: id, reason: "Undo triggered") end |
Instance Method Details
#cancel(reason) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/ruby_reactor/reactor.rb', line 184 def cancel(reason) @context.cancelled = true @context.cancellation_reason = reason @context.status = "cancelled" save_context end |
#continue(payload:, step_name:, idempotency_key: nil) ⇒ Object
rubocop:enable Metrics/MethodLength, Metrics/AbcSize
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 |
# File 'lib/ruby_reactor/reactor.rb', line 142 def continue(payload:, step_name:, idempotency_key: nil) _ = idempotency_key unless @context.current_step raise Error::ValidationError, "Cannot resume: context does not have a current step (was it interrupted?)" end if @context.cancelled raise Error::ValidationError, "Cannot resume: reactor has been cancelled (Reason: #{@context.cancellation_reason})" end validate_continue_step!(step_name) if (failure = validate_continue_payload(payload, step_name)) return failure end target_step = step_name @context.set_result(target_step, payload) # Resume execution executor = Executor.new(self.class, {}, @context) @result = executor.resume_execution @context = executor.context @undo_trace = executor.undo_trace @execution_trace = executor.execution_trace @result rescue Error::InputValidationError => e # This might catch other validations, but here we specifically want payload validation. # The block above handles payload validation explicitly. RubyReactor::Failure(e., invalid_payload: true) end |
#run(inputs = {}) ⇒ Object
rubocop:disable Metrics/MethodLength
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 131 132 133 134 135 136 137 138 139 |
# File 'lib/ruby_reactor/reactor.rb', line 88 def run(inputs = {}) # For all reactors, initialize context first to capture execution ID @context = @context.is_a?(Context) ? @context : Context.new(inputs, self.class) # Validate inputs validation_result = self.class.validate_inputs(inputs) if validation_result.failure? @result = validation_result @context.status = "failed" @context.failure_reason = { message: validation_result.error., validation_errors: validation_result.error.field_errors } save_context return validation_result end if self.class.async? && !@context.inline_async_execution # For async reactors, queue a job for the whole reactor @context.status = :running save_context serialized_context = ContextSerializer.serialize(@context) @result = configuration.async_router.perform_async(serialized_context, self.class.name, intermediate_results: @context.intermediate_results) # Even if it's an AsyncResult, it might have finished inline (e.g. Sidekiq::Testing.inline!) # Check storage to see if it's already finished or paused (interrupted). begin reloaded = self.class.find(@context.context_id) if reloaded.finished? || reloaded.context.status.to_s == "paused" @context = reloaded.context @result = reloaded.result @execution_trace = reloaded.execution_trace @undo_trace = reloaded.undo_trace return @result end rescue StandardError # Ignore if not found or other errors during reload check end else # For sync reactors (potentially with async steps), execute normally context = @context.is_a?(Context) ? @context : nil executor = Executor.new(self.class, inputs, context) @result = executor.execute @context = executor.context @execution_trace = executor.execution_trace @undo_trace = executor.undo_trace end @result end |
#undo ⇒ Object
178 179 180 181 182 |
# File 'lib/ruby_reactor/reactor.rb', line 178 def undo executor = Executor.new(self.class, {}, @context) executor.undo_all executor.save_context end |
#validate! ⇒ Object
191 192 193 194 195 196 |
# File 'lib/ruby_reactor/reactor.rb', line 191 def validate! # Validate reactor configuration validate_steps! validate_return_step! validate_dependencies! end |