class Epi::ProcessStatus

Public Class Methods

last() click to toggle source

The last snapshot taken by {#take} @return [self]

# File lib/epi/process_status.rb, line 27
def last
  @last ||= take!
end
new() click to toggle source
# File lib/epi/process_status.rb, line 51
def initialize

  # Cached running processes
  @running_processes = {}

  # Run `ps`
  result = %x(ps ax -o #{RunningProcess::PS_FORMAT})

  # Split into lines, and get rid of the first (heading) line
  @lines = result.lines[1..-1].map { |line| [line.lstrip.split(/\s/, 2).first.to_i, line] }.to_h

end
now() click to toggle source

Current running processes @return [self]

# File lib/epi/process_status.rb, line 15
def now
  new
end
reset!() click to toggle source
# File lib/epi/process_status.rb, line 9
def reset!
  @last = nil
end
take!() click to toggle source

Take a snapshot of current running processes @return [self]

# File lib/epi/process_status.rb, line 21
def take!
  @last = now
end

Public Instance Methods

[](pid) click to toggle source

Lookup a running process by its PID @param pid [String|Numeric] PID of the process to lookup @return [RunningProcess|NilClass]

# File lib/epi/process_status.rb, line 38
def [](pid)
  pid = pid.to_i
  @running_processes[pid] ||= find_by_pid(pid)
end
pids() click to toggle source

Get a list of PIDs of running processes @return [Array] An array of PIDs as ‘Fixnum`s

# File lib/epi/process_status.rb, line 45
def pids
  @pids ||= @lines.keys
end

Private Instance Methods

find_by_pid(pid) click to toggle source
# File lib/epi/process_status.rb, line 64
def find_by_pid(pid)
  line = @lines[pid]
  RunningProcess.new(pid, ps_line: line) if line
end