class Datacenter::Process

Constants

ATTRIBUTES

Attributes

pid[R]
shell[R]

Public Class Methods

new(pid, shell=nil) click to toggle source
# File lib/datacenter/process.rb, line 18
def initialize(pid, shell=nil)
  @pid = pid
  @shell = shell || Shell::Local.new
  @cache = Cache.new Datacenter.process_cache_expiration
  @mutex = Mutex.new
end

Public Instance Methods

alive?() click to toggle source
# File lib/datacenter/process.rb, line 31
def alive?
  command = %Q{
    if [ -d "#{proc_dir}" ]; then 
      echo -n "true"
    else
      echo -n "false"
    fi
  }

  alive = shell.run(command) == 'true'

  Datacenter.logger.info(self.class) { "pid: #{pid} - ALIVE: #{alive}" } if !alive
  alive
end
send_signal(signal) click to toggle source
# File lib/datacenter/process.rb, line 46
def send_signal(signal)
  shell.run "kill -s #{signal} #{pid}"
end

Private Instance Methods

info() click to toggle source
# File lib/datacenter/process.rb, line 54
def info
  @mutex.synchronize do
    @cache.fetch(:info) do
      status = Hash[proc_file(:status).split("\n").map{ |s| s.split(':').map(&:strip) }]
      ps = shell.run("ps -p #{pid} -o user,pid,pcpu,%mem,vsize,rss,stat,command").split("\n")[1].split
      Hash.new.tap do |info|
        info[:name] = status['Name']
        info[:user] = ps[0]
        info[:pid]  = ps[1]
        info[:cpu_usage] = ps[2].to_f
        info[:mem_usage] = ps[3].to_f
        info[:virtual_memory] = ps[4].to_i / 1024.0
        info[:memory] = ps[5].to_i / 1024.0
        info[:status] = ps[6]
        info[:command] = ps[7..-1].reduce { |acum,e| "#{acum} #{e}" }
      end
    end
  end
end
proc_dir() click to toggle source
# File lib/datacenter/process.rb, line 74
def proc_dir
  File.join '/proc', pid.to_s
end
proc_file(name) click to toggle source
# File lib/datacenter/process.rb, line 78
def proc_file(name)
  filename = File.join proc_dir, name.to_s
  shell.run "cat #{filename}"
end