class Cult::Transaction::Log

Attributes

steps[R]

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/cult/transaction.rb, line 5
def initialize
  @steps = []
  yield self if block_given?
end

Public Instance Methods

protect() { || ... } click to toggle source
# File lib/cult/transaction.rb, line 27
def protect(&block)
  begin
    yield
  rescue Exception => e
    $stderr.puts "Rolling back actions due to: #{e.inspect}\n" +
                 e.backtrace
    unwind
    raise
  end
end
rollback(&block) click to toggle source
# File lib/cult/transaction.rb, line 38
def rollback(&block)
  steps.push([Process.pid, block])
end
unwind() click to toggle source
# File lib/cult/transaction.rb, line 10
def unwind
  begin
    # We stop rolling back when we read entries created in our parent
    # process
    while !steps.empty?
      pid, step = steps.last
      break if pid != Process.pid
      steps.pop
      step.call
    end
  rescue Exception => e
    $stderr.puts "Execption raised while rolling back: #{e.inspect}\n" +
                 e.backtrace
    retry
  end
end