Class: Cumuli::PS

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

Defined Under Namespace

Classes: Line

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
11
# File 'lib/cumuli/ps.rb', line 7

def initialize(list=self.class.list)
  lines = list.lines
  lines.shift # to remove the header
  @lines = lines.map{|l| Line.new(l) }
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



102
103
104
# File 'lib/cumuli/ps.rb', line 102

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

Instance Method Details

#children(pid = root_pid) ⇒ Object



81
82
83
# File 'lib/cumuli/ps.rb', line 81

def children(pid=root_pid)
  ppid_hash[pid]
end

#family(pid = root_pid) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/cumuli/ps.rb', line 70

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

#foremansObject



40
41
42
# File 'lib/cumuli/ps.rb', line 40

def foremans
  lines.select{|l| l.command.match(/foreman: master/) }.sort
end

#int(pids = [root_pids]) ⇒ Object



44
45
46
47
48
# File 'lib/cumuli/ps.rb', line 44

def int(pids=[root_pids])
  pids.compact.each do |pid|
    Process.kill('INT', pid)
  end
end

#kill(pids = [root_pid]) ⇒ Object



50
51
52
53
54
# File 'lib/cumuli/ps.rb', line 50

def kill(pids=[root_pid])
  pids.compact.each do |pid|
    Process.kill(9, pid)
  end
end

#line(pid = root_pid) ⇒ Object



65
66
67
68
# File 'lib/cumuli/ps.rb', line 65

def line(pid=root_pid)
  line = lines.detect{|l| l.pid == pid }
  line && line.to_s
end

#ppid_hashObject



56
57
58
59
60
61
62
63
# File 'lib/cumuli/ps.rb', line 56

def ppid_hash
  lines
    .inject({}) do |hash, line|
      hash[line.ppid] ||= []
      hash[line.ppid] << line.pid
      hash
    end
end

#report(pid = root_pid) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/cumuli/ps.rb', line 85

def report(pid=root_pid)
  r = [line(pid).to_s]
  family(pid).each do |p|
    l = line(p)
    r << l.to_s
  end
  r.join("\n")
end

#report_allObject



94
95
96
97
98
99
100
# File 'lib/cumuli/ps.rb', line 94

def report_all
  r = []
  foremans.each do |line|
    r << report(line.pid)
  end
  r.join("\n")
end

#rootObject



36
37
38
# File 'lib/cumuli/ps.rb', line 36

def root
  foremans.first
end

#root_pidObject



32
33
34
# File 'lib/cumuli/ps.rb', line 32

def root_pid
  root && root.pid
end