Class: Cult::Transaction::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/cult/transaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Log

Returns a new instance of Log.

Yields:

  • (_self)

Yield Parameters:



5
6
7
8
# File 'lib/cult/transaction.rb', line 5

def initialize
  @steps = []
  yield self if block_given?
end

Instance Attribute Details

#stepsObject (readonly)

Returns the value of attribute steps.



4
5
6
# File 'lib/cult/transaction.rb', line 4

def steps
  @steps
end

Instance Method Details

#protect(&block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



38
39
40
# File 'lib/cult/transaction.rb', line 38

def rollback(&block)
  steps.push([Process.pid, block])
end

#unwindObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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