Class: Cumuli::PS

Inherits:
Object
  • Object
show all
Defined in:
lib/cumuli/ps.rb

Constant Summary collapse

REGEX =
/ruby|resque|foreman|rvm|node/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list = self.class.list) ⇒ PS

Returns a new instance of PS.



7
8
9
10
# File 'lib/cumuli/ps.rb', line 7

def initialize(list=self.class.list)
  @lines = list.lines
  @lines.shift # to remove header line
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



5
6
7
# File 'lib/cumuli/ps.rb', line 5

def lines
  @lines
end

Class Method Details

.listObject



68
69
70
# File 'lib/cumuli/ps.rb', line 68

def self.list
  `ps axo pid,ppid,comm,args,user`
end

Instance Method Details

#children(pid) ⇒ Object



58
59
60
# File 'lib/cumuli/ps.rb', line 58

def children(pid)
  ppid_hash[pid]
end

#killObject



18
19
20
21
22
# File 'lib/cumuli/ps.rb', line 18

def kill
  pids.each do |pid|
    Process.kill("SIGINT", pid)
  end
end

#line(pid) ⇒ Object



43
44
45
# File 'lib/cumuli/ps.rb', line 43

def line(pid)
  lines.detect{|l| l.match(/^#{pid} /)}
end

#matching(regex = REGEX) ⇒ Object



12
13
14
15
16
# File 'lib/cumuli/ps.rb', line 12

def matching(regex = REGEX)
  @matching ||= lines
    .select{|l| l.match(regex)}
    .select{|l| !l.match(/^#{Process.pid} /) }
end

#pids(regex = REGEX) ⇒ Object



24
25
26
27
28
29
# File 'lib/cumuli/ps.rb', line 24

def pids(regex = REGEX)
  matching(regex)
    .map(&:split)
    .map(&:first)
    .map(&:to_i)
end

#ppid_hashObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cumuli/ps.rb', line 31

def ppid_hash
  lines
    .map(&:split)
    .inject({}) do |hash, line_values|
      pid = line_values[0].to_i
      ppid = line_values[1].to_i
      hash[ppid] ||= []
      hash[ppid] << pid
      hash
    end
end

#report(pid) ⇒ Object



62
63
64
65
66
# File 'lib/cumuli/ps.rb', line 62

def report(pid)
  tree(pid)
  puts "\n"
  puts matching
end

#tree(pid) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/cumuli/ps.rb', line 47

def tree(pid)
  collection = []
  if kids = children(pid)
    collection += kids
    kids.each do |k|
      collection += tree(k)
    end
  end
  collection
end