Class: ScriptExecutionState

Inherits:
Object
  • Object
show all
Includes:
StateTransitionHelper
Defined in:
lib/help/script_execution_state.rb

Overview

Implements a little state-machine. Usage: for every state you need, extend this class. The method enter() must be implemented for every state you code and return another state.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StateTransitionHelper

#attach_volume, #connect, #copy_distribution, #create_fs, #create_snapshot, #create_volume, #create_volume_from_snapshot, #delete_snapshot, #delete_volume, #detach_volume, #disconnect, #ec2_handler, #ec2_handler=, #launch_instance, #mount_fs, #register_snapshot, #remote_copy, #remote_handler, #remote_handler=, #shut_down_instance, #unmount_fs, #upload_file, #zip_volume

Constructor Details

#initialize(context) ⇒ ScriptExecutionState

Returns a new instance of ScriptExecutionState.



13
14
15
16
17
18
19
20
21
# File 'lib/help/script_execution_state.rb', line 13

def initialize(context)
  @context = context
  @state_change_listeners = []
  @logger = context[:logger]
  if @logger == nil
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::WARN
  end
end

Instance Attribute Details

#contextObject (readonly)

context information for the state (hash)



11
12
13
# File 'lib/help/script_execution_state.rb', line 11

def context
  @context
end

#loggerObject (readonly)

context information for the state (hash)



11
12
13
# File 'lib/help/script_execution_state.rb', line 11

def logger
  @logger
end

Instance Method Details

#done?Boolean

To be implemented. Indicates if the final state is reached.

Returns:

  • (Boolean)


62
63
64
# File 'lib/help/script_execution_state.rb', line 62

def done?
  false
end

#end_stateObject

Returns the state that is reached after execution.



52
53
54
# File 'lib/help/script_execution_state.rb', line 52

def end_state
  @current_state
end

#enterObject

To be implemented. Executes the code for this state.

Raises:

  • (Exception)


57
58
59
# File 'lib/help/script_execution_state.rb', line 57

def enter
  raise Exception.new("TaskExecutionState is abstract")
end

#failed?Boolean

To be implemented. Indicates if the final state is a failure state.

Returns:

  • (Boolean)


67
68
69
# File 'lib/help/script_execution_state.rb', line 67

def failed?
  false
end

#register_state_change_listener(listener) ⇒ Object

Listener should extend #StateChangeListener (or implement a state_changed(state) method). Note: calls are synchronous.



25
26
27
# File 'lib/help/script_execution_state.rb', line 25

def register_state_change_listener(listener)
  @state_change_listeners << listener
end

#start_state_machineObject

Start the state machine using this state as initial state.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/help/script_execution_state.rb', line 30

def start_state_machine
  @current_state = self
  @logger.info "start state machine with #{@current_state.to_s}"
  while !@current_state.done? && !@current_state.failed?
    begin
      @logger.info "state machine: current state = #{@current_state.to_s}"
      @current_state = @current_state.enter()
      notify_state_change_listeners(@current_state)
    rescue Exception => e
      if @context[:result] != nil
        @context[:result][:details] = e.backtrace().join("\n")
      end
      @current_state = FailedState.new(@context, e.to_s, @current_state)
      notify_state_change_listeners(@current_state)
      @logger.warn "StateMachine exception during execution: #{e}"
      @logger.warn "#{e.backtrace.join("\n")}"
    end
  end
  @current_state
end

#to_sObject



71
72
73
74
# File 'lib/help/script_execution_state.rb', line 71

def to_s
  s = self.class.to_s
  s.sub(/.*\:\:/,'')
end