Class: RubyReactor::Executor::CompensationManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ CompensationManager

Returns a new instance of CompensationManager.



6
7
8
9
10
# File 'lib/ruby_reactor/executor/compensation_manager.rb', line 6

def initialize(context)
  @context = context
  @undo_stack = []
  @undo_trace = []
end

Instance Attribute Details

#undo_stackObject (readonly)

Returns the value of attribute undo_stack.



12
13
14
# File 'lib/ruby_reactor/executor/compensation_manager.rb', line 12

def undo_stack
  @undo_stack
end

#undo_traceObject (readonly)

Returns the value of attribute undo_trace.



12
13
14
# File 'lib/ruby_reactor/executor/compensation_manager.rb', line 12

def undo_trace
  @undo_trace
end

Instance Method Details

#add_to_undo_stack(step_info) ⇒ Object



14
15
16
# File 'lib/ruby_reactor/executor/compensation_manager.rb', line 14

def add_to_undo_stack(step_info)
  @undo_stack << step_info
end

#handle_step_failure(step_config, error, arguments) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_reactor/executor/compensation_manager.rb', line 18

def handle_step_failure(step_config, error, arguments)
  # Try compensation
  compensation_result = compensate_step(step_config, error, arguments)
  case compensation_result
  when RubyReactor::Success
    # Compensation succeeded, continue with rollback
    rollback_completed_steps
    RubyReactor.Failure("Step '#{step_config.name}' failed: #{error}")
  when RubyReactor::Failure
    # Compensation failed, this is more serious
    rollback_completed_steps
    raise Error::CompensationError.new(
      "Compensation for step '#{step_config.name}' failed: #{compensation_result.error}",
      step: step_config.name,
      context: @context,
      original_error: error
    )
  end
end

#rollback_completed_stepsObject



38
39
40
41
42
43
44
45
# File 'lib/ruby_reactor/executor/compensation_manager.rb', line 38

def rollback_completed_steps
  @undo_stack.reverse_each do |step_info|
    result = undo_step(step_info[:step], step_info[:result], step_info[:arguments])
    @undo_trace << { type: :undo, step: step_info[:step].name, result: result,
                     arguments: step_info[:arguments] }
  end
  @undo_stack.clear
end