Class: ScriptExecutionState

Inherits:
Object
  • Object
show all
Includes:
StateTransitionHelper, VCloudTransitionHelper
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_image_from_instance, #create_labeled_fs, #create_security_group_with_rules, #create_snapshot, #create_volume, #create_volume_from_snapshot, #delete_security_group, #delete_snapshot, #delete_volume, #describe_instance, #detach_volume, #determine_file, #disable_ssh_tty, #disconnect, #ec2_handler, #ec2_handler=, #enable_ssh_tty, #get_aws_kernel_image_aki, #get_aws_region_from_endpoint, #get_partition_count, #get_partition_fs_type, #get_partition_fs_type_and_label, #get_partition_label, #get_partition_table, #get_root_device_name, #get_root_partition_fs_type, #get_root_partition_fs_type_and_label, #get_root_partition_label, #get_root_volume_id, #launch_instance, #local_decompress_and_dump_file_to_device, #local_dump_and_compress_device_to_file, #local_dump_device_to_file, #local_dump_file_to_device, #mount_fs, #mount_fs_old, #register_snapshot, #remote_copy, #remote_copy_old, #remote_handler, #remote_handler=, #retrieve_instances, #retrieve_security_groups, #set_partition_table, #shut_down_instance, #snapshot_accessible, #start_instance, #stop_instance, #unmount_fs, #upload_file, #zip_volume

Methods included from VCloudTransitionHelper

#retrieve_ip_services

Constructor Details

#initialize(context) ⇒ ScriptExecutionState

Returns a new instance of ScriptExecutionState.



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

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)



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

def context
  @context
end

#loggerObject (readonly)

context information for the state (hash)



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

def logger
  @logger
end

Instance Method Details

#done?Boolean

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

Returns:

  • (Boolean)


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

def done?
  false
end

#end_stateObject

Returns the state that is reached after execution.



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

def end_state
  @current_state
end

#enterObject

To be implemented. Executes the code for this state.

Raises:

  • (Exception)


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

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)


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

def failed?
  false
end

#get_superclass_nameObject



77
78
79
# File 'lib/help/script_execution_state.rb', line 77

def get_superclass_name
  self.class.name.split('::').first()
end

#register_state_change_listener(listener) ⇒ Object

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



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

def register_state_change_listener(listener)
  @state_change_listeners << listener
end

#start_state_machineObject

Start the state machine using this state as initial state.



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

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



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

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