class SrvManager::Process

Attributes

command[R]
id[R]

Public Class Methods

new(command) click to toggle source
# File lib/srv_manager/process.rb, line 7
def initialize(command)
  @command = command
end

Public Instance Methods

alive?() click to toggle source
# File lib/srv_manager/process.rb, line 39
def alive?
  started? && ::Process.kill(0, id) ? true : false
rescue Errno::ESRCH
  false
end
kill() click to toggle source
# File lib/srv_manager/process.rb, line 24
def kill
  send_signal 'KILL'
  LOGGER.info "Killed process #{@id}"
  @id = nil
end
restart() click to toggle source
# File lib/srv_manager/process.rb, line 30
def restart
  stop if alive?
  start
end
start() click to toggle source
# File lib/srv_manager/process.rb, line 11
def start
  return if alive?
  command.rvm ? rvm_start : default_start
  @id = wait_for_pid(command.pidfile) if command.pidfile
  LOGGER.info "Started process #{@id}"
end
started?() click to toggle source
# File lib/srv_manager/process.rb, line 35
def started?
  !id.nil?
end
stop() click to toggle source
# File lib/srv_manager/process.rb, line 18
def stop
  send_signal 'TERM'
  LOGGER.info "Stoped process #{@id}"
  @id = nil
end

Private Instance Methods

default_start() click to toggle source
# File lib/srv_manager/process.rb, line 47
def default_start
  @id = ::Process.spawn command.env, 
                        command.text, 
                        chdir: command.dir, 
                        out: '/dev/null', 
                        err: '/dev/null'
  ::Process.detach @id
end
rvm_start() click to toggle source
# File lib/srv_manager/process.rb, line 56
def rvm_start
  pid_file = File.expand_path "#{self.object_id}_#{Time.now.to_i}.pid", TMP_PATH
  params = {
    'SRV_COMMAND' => command.text, 
    'SRV_PIDFILE' => pid_file, 
    'CHDIR' => command.dir
  }
  rvm_pid = ::Process.spawn command.env.merge(params), 
                            RVM_RUNNER, 
                            out: '/dev/null', 
                            err: '/dev/null'
  ::Process.detach rvm_pid
  @id = wait_for_pid pid_file, true
end
send_signal(signal) click to toggle source
# File lib/srv_manager/process.rb, line 71
def send_signal(signal)
  return unless alive?
  begin
    ::Process.kill signal, @id
  rescue Errno::ESRCH
  end
end
wait_for_pid(filename, delete=false) click to toggle source
# File lib/srv_manager/process.rb, line 79
def wait_for_pid(filename, delete=false)
  Timeout.timeout(60) do
    loop do
      if File.exists? filename
        pid = File.read(filename).to_i
        File.delete filename if delete
        return pid
      end
      sleep 0.1
    end
  end
rescue Timeout::Error
  nil
end