Sha256: 8748243790ad679e463ccb60ff5ec0c735a962e4aa78155c3b02bef3b94c7a9b

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

module Datacenter
  class Process

    ATTRIBUTES = [
      :command, 
      :status, 
      :memory, 
      :virtual_memory,
      :cpu,
      :user,
      :name,
      :cpu_usage,
      :mem_usage
    ]

    attr_reader :pid

    def initialize(pid, shell=nil)
      @pid = pid
      @shell = shell || Shell::Local.new
      @cache = Cache.new Datacenter.process_cache_expiration
      @mutex = Mutex.new
    end

    ATTRIBUTES.each do |attribute|
      define_method attribute do
        info[attribute]
      end
    end

    def alive?
      alive = !shell.run("ps -p #{pid} | grep #{pid}").empty?
      Datacenter.logger.info(self.class) { "pid: #{pid} - ALIVE: #{alive}" } if !alive
      alive
    end

    def send_signal(signal)
      shell.run "kill -s #{signal} #{pid}"
    end

    private

    attr_reader :shell

    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

    def proc_file(name)
      filename = File.join '/proc', pid.to_s, name.to_s
      shell.run "cat #{filename}"
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
datacenter-0.4.3 lib/datacenter/process.rb
datacenter-0.4.2 lib/datacenter/process.rb
datacenter-0.4.0 lib/datacenter/process.rb
datacenter-0.3.1 lib/datacenter/process.rb