class OpenShift::Runtime::Utils::ApplicationState

Class to maintain persistent application state

Attributes

uuid[R]

Public Class Methods

new(container) click to toggle source
# File lib/openshift-origin-node/utils/application_state.rb, line 42
def initialize(container)
  @container = container
  @uuid = @container.uuid

  @state_file = File.join(@container.container_dir, "app-root", "runtime", ".state")
end

Public Instance Methods

value() click to toggle source

Public: Fetch application state from gear.

@return [String] application state or State::UNKNOWN on failure

# File lib/openshift-origin-node/utils/application_state.rb, line 73
def value
  begin
    File.open(@state_file) { |input| input.read.chomp }
  rescue => e
    msg = "Failed to get state: #{@uuid} [#{@state_file}]: "
    case e
      when SystemCallError
        # This catches filesystem level errors
        # We split the message because it contains the filename
        msg << e.message.split(' - ').first
      else
        msg << e.message
    end
    NodeLogger.logger.info( msg )

    State::UNKNOWN
  end
end
value=(new_state) click to toggle source

Public: Sets the application state.

@param [String] new_state - From Openshift::State. @return [Object] self for chaining calls

# File lib/openshift-origin-node/utils/application_state.rb, line 53
def value=(new_state)
  new_state_val = nil
  begin
    new_state_val = ::OpenShift::Runtime::State.const_get new_state.upcase.intern
  rescue
    raise ArgumentError, "Invalid state '#{new_state}' specified"
  end

  File.open(@state_file, File::WRONLY|File::TRUNC|File::CREAT, 0640) { |file|
    file.write "#{new_state_val}\n"
  }

  @container.set_rw_permission(@state_file)
  self
end