Class: VCloudCloud::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud/vcloud/steps.rb

Overview

Transactional step execution engine

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, client) ⇒ Transaction

Returns a new instance of Transaction.



25
26
27
28
29
30
31
# File 'lib/cloud/vcloud/steps.rb', line 25

def initialize(name, client)
  @name = name
  @client = client
  @logger = client.logger
  @state = {}
  @steps = []
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



23
24
25
# File 'lib/cloud/vcloud/steps.rb', line 23

def client
  @client
end

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/cloud/vcloud/steps.rb', line 23

def name
  @name
end

#stateObject (readonly)

Returns the value of attribute state.



23
24
25
# File 'lib/cloud/vcloud/steps.rb', line 23

def state
  @state
end

Class Method Details

.perform(name, client = nil, options = {}, &block) ⇒ Object



57
58
59
# File 'lib/cloud/vcloud/steps.rb', line 57

def self.perform(name, client = nil, options = {}, &block)
  new(name, client).perform options, &block
end

Instance Method Details

#next(step_class, *args, &block) ⇒ Object



33
34
35
36
37
38
# File 'lib/cloud/vcloud/steps.rb', line 33

def next(step_class, *args, &block)
  step = step_class.new @state, @client
  @steps << step
  @logger.debug "STEP #{step_class.to_s}"
  step.perform *args, &block
end

#perform(options = {}, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cloud/vcloud/steps.rb', line 40

def perform(options = {}, &block)
  with_thread_name @name do
    # Perform all steps guarded by exception handler
    begin
      block.call self
    rescue => ex
      @logger.error "FAIL #{@name} #{ex}"
      reverse_steps :rollback
      raise ex unless options[:no_throws]
      @state[:error] = ex
    ensure
      reverse_steps :cleanup
    end
  end
  @state
end