Class: RightScale::CloudApi::Routine

Inherits:
Object
  • Object
show all
Defined in:
lib/base/routines/routine.rb

Overview

This is a parent class for all the other routines.

The routine is a very simple object that does a simple task in the API call processing stack and exits. In most cases a single routine knows nothing about any other routines. It just takes incoming params from @data hash, processes it and stores back to the @adata attribute.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



34
35
36
# File 'lib/base/routines/routine.rb', line 34

def data
  @data
end

Instance Method Details

#cloud_api_loggerCloudApiLogger

Current logger.

Returns:



67
68
69
# File 'lib/base/routines/routine.rb', line 67

def cloud_api_logger
  options[:cloud_api_logger]
end

#execute(data) ⇒ Object

Initialize and process the routine. Is usially called from unit tests.



50
51
52
53
# File 'lib/base/routines/routine.rb', line 50

def execute(data)
  reset(data)
  process
end

#invoke_callback_method(proc, *args) ⇒ Object

A helper method for invoking callbacks.

The method checks if the given Proc exists and invokes it with the given set of arguments. In the case when proc==nil the method does nothing.

Parameters:

  • proc (Proc)

    The callback.

  • args (Any)

    A set of callback method arguments.



92
93
94
# File 'lib/base/routines/routine.rb', line 92

def invoke_callback_method(proc, *args) # :nodoc:
  proc.call(*args) if proc.is_a?(Proc)
end

#optionsHash

Current options.

Returns:



59
60
61
# File 'lib/base/routines/routine.rb', line 59

def options
  @data[:options]
end

#processObject

Main entry point. The method must be overriden by sub-classes.

Raises:



45
46
47
# File 'lib/base/routines/routine.rb', line 45

def process
  raise Error::new("This method should be implemented by a subclass")
end

#reset(data = nil) ⇒ Object

Initializes the @data attribute. Is called before process method.

Parameters:

  • data (Hash) (defaults to: nil)

    See ApiManager for better explanation what data is.



40
41
42
# File 'lib/base/routines/routine.rb', line 40

def reset(data=nil)
  @data = data
end

#with_timer(description = 'Timer', log_key = :timer, &block) ⇒ Object

The method takes a block of code and logs how much time the given block took to execute.

Parameters:

  • description (String) (defaults to: 'Timer')

    The prefix that is added to every logged line.

  • log_key (Symbol) (defaults to: :timer)

    The log key (see CloudApiLogger).



76
77
78
79
80
81
82
# File 'lib/base/routines/routine.rb', line 76

def with_timer(description = 'Timer', log_key = :timer, &block)
  cloud_api_logger.log("#{description} started...",:timer)
  start  = Time::now
  result = block.call
  cloud_api_logger.log("#{description} completed (#{'%.6f' % (Time::now - start)} sec)", log_key)
  result
end