class Erlnixify::Node

The process class owns the Running erlang process. It knows how to query it if its active, and kill it if something goes long. This class is the guts of erlnixify

Public Class Methods

new(settings) click to toggle source
# File lib/erlnixify/node.rb, line 17
def initialize(settings)
  @settings = settings

  @command = @settings[:command] % settings.settings if @settings[:command]
  @check_command = self.interpolate_cmd(@settings[:check])
  @halt_command = self.interpolate_cmd(SHUTDOWN_COMMAND)
  @brutal_halt_command = self.interpolate_cmd(BRUTAL_SHUTDOWN_COMMAND)
  @checkregex = Regexp.new @settings[:checkregex]

  @log = Logger.new(STDOUT)
  @log.level = Logger::DEBUG

end

Public Instance Methods

check() click to toggle source
# File lib/erlnixify/node.rb, line 99
def check
  begin
    Timeout.timeout(@settings[:checktimeout]) do
      self.raw_check
    end
  rescue Timeout::Error
    self.halt_nicely
    raise NodeError, "Check command timeout occurred"
  end
end
external_kill() click to toggle source
# File lib/erlnixify/node.rb, line 172
def external_kill
  if self.is_running?
    @log.debug "Killing pid: #{@pid}"
    Process.kill("KILL", @pid) if @pid
  end
end
halt_brutally() click to toggle source
# File lib/erlnixify/node.rb, line 163
def halt_brutally
  if self.is_running?
    @log.debug "Executing halt brutally: #{@brutal_halt_command}"
    `#{@brutal_halt_command}`
    sleep @settings[:checkinterval]
    self.external_kill
  end
end
halt_nicely() click to toggle source
# File lib/erlnixify/node.rb, line 154
def halt_nicely
  if self.is_running?
    @log.debug "Executing halt nicely: #{@halt_command}"
    `#{@halt_command}`
    sleep @settings[:checkinterval]
    self.halt_brutally
  end
end
interpolate_cmd(cmd) click to toggle source
# File lib/erlnixify/node.rb, line 179
def interpolate_cmd(cmd)
  local_settings = @settings.settings.clone
  local_settings[:cmd] = cmd % local_settings
  COMMAND_WRAPPER % local_settings
end
is_running?() click to toggle source
# File lib/erlnixify/node.rb, line 134
def is_running?
  @log.debug "Checking if Pid (#{@pid}) is running"
  if @pid
    begin
      Process.getpgid(@pid)
      true
    rescue Errno::ESRCH
      false
    end
  else
    false
  end
end
monitor() click to toggle source
# File lib/erlnixify/node.rb, line 85
def monitor
  @log.debug "starting monitor of Pid #{@pid}"
  loop do
    if is_running?
      self.check
      sleep @settings[:checkinterval]
    else
      raise NodeError, "Node not running"
    end
    break if @stop
    @log.debug "Node responded correctly, continuing check"
  end
end
raw_check() click to toggle source
# File lib/erlnixify/node.rb, line 121
def raw_check
  @log.debug "#{@check_command} =~ #{@checkregex}"
  result = `#{@check_command}`
  @log.debug "result #{result}"
  if not (result =~ @checkregex)
    @log.info "invalid state"
    self.halt_nicely
    raise NodeError, "Node check failed"
  else
    @log.info "running"
  end
end
raw_start_daemon() click to toggle source
# File lib/erlnixify/node.rb, line 66
def raw_start_daemon
  @log.debug "starting daemon"
  env = {}
  env["HOME"] = @settings[:home] if @settings[:home]

  begin
    @log.debug "spawning command '#{@command}' with #{env}"
    @pid = Process.spawn(env, @command)
    Process.detach @pid
  rescue Errno::ENOENT
    @log.debug "Invalid command provided, raising error"
    raise NodeError, "Command does not exist"
  end

  @log.debug "waiting for #{@settings[:startuptimeout]} seconds for startup"
  sleep @settings[:startuptimeout]
  self.status
end
start() click to toggle source
# File lib/erlnixify/node.rb, line 31
def start
  self.start_daemon

  Signal.trap("TERM") do
    # This is going to propagate to the running erlang
    # node. Unfortunately, there is no way to stop that that I
    # have found yet. Hopefully, in the near future we can resolve
    # that.
    raise NodeError, "SIGTERM recieved, shutting down"
  end

  Signal.trap("INT") do
    # This is going to propagate to the running erlang
    # node. Unfortunately, there is no way to stop that that I
    # have found yet. Hopefully, in the near future we can resolve
    # that.
    raise NodeError, "SIGINT recieved, shutting down"
  end

  at_exit { self.external_kill }

  @log.debug "waiting for #{@settings[:startuptimeout]} seconds for startup"
  sleep @settings[:startuptimeout]
  self.monitor
end
start_daemon() click to toggle source
# File lib/erlnixify/node.rb, line 57
def start_daemon
  begin
    self.status
  rescue NodeError => msg
    return self.raw_start_daemon
  end
  raise NodeError, "Already started"
end
status() click to toggle source
# File lib/erlnixify/node.rb, line 110
def status
  begin
    Timeout.timeout(@settings[:checktimeout]) do
        self.raw_check
    end
  rescue Timeout::Error
    self.halt_nicely
    raise NodeError, "Check command timeout occurred"
  end
end
stop() click to toggle source
# File lib/erlnixify/node.rb, line 148
def stop
  @log.debug "Executing halt nicely: #{@halt_command}"
  `#{@halt_command}`
  sleep @settings[:checkinterval]
end