Class: Mutx::Support::Processes

Inherits:
Object
  • Object
show all
Defined in:
lib/mutx/support/processes.rb

Class Method Summary collapse

Class Method Details

.fork_this(command) ⇒ Object



93
94
95
# File 'lib/mutx/support/processes.rb', line 93

def self.fork_this command
  fork{exec("#{command}")}
end

.kill_all_these(pids = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/mutx/support/processes.rb', line 77

def self.kill_all_these pids=nil
  if pids
    until pids.empty?
      pid = pids.pop
      if process_running? pid
        begin
          kill_dash_nine(pid)
        rescue Errno::ESRCH
          puts "Could not find process id #{pid} to kill"
        end
      end
    end
  end
end

.kill_by_result_id(result_id) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mutx/support/processes.rb', line 6

def self.kill_by_result_id(result_id)

  pid = pid_for(result_id)

  return false if pid.nil?

  childs = process_childs(pid)

  unless pid.zero?
    begin
      childs.each{|ch_pid| kill_p(ch_pid)} unless childs.empty?
      kill_p(pid)
      true # Process killed
    rescue Errno::EPERM # Denied permission to kill process
      begin
        kill_dash_nine(pid) and true # Process killed
      rescue
        false
      end
    rescue Errno::ESRCH # Process did not exist
      false
    end
  else
      puts "Cannot find process id"
  end
end

.kill_dash_nine(pid) ⇒ Object



38
39
40
41
42
# File 'lib/mutx/support/processes.rb', line 38

def self.kill_dash_nine(pid)
  pid = pid.to_i if pid.respond_to? :to_i
  puts "kill -9 #{pid}" if $DEBUG
  Mutx::Support::Console.execute "kill -9 #{pid}"
end

.kill_p(pid) ⇒ Object



33
34
35
36
# File 'lib/mutx/support/processes.rb', line 33

def self.kill_p(pid)
  pid = pid.to_i if pid.respond_to? :to_i
  Process.kill('INT', pid)
end

.mutx_pidsObject



66
67
68
69
# File 'lib/mutx/support/processes.rb', line 66

def self.mutx_pids
  res = Mutx::Support::Console.execute "ps -fea | grep 'unicorn.rb -p #{Mutx::Support::Configuration.port}'"
  res.split("\n").select{|lines| !lines.include? "grep"}.map{|line| line.split[1]}
end

.pid_for(result_id) ⇒ Object



44
45
46
47
# File 'lib/mutx/support/processes.rb', line 44

def self.pid_for(result_id)
  result = Mutx::Results::Result.get(result_id)
  result.pid.to_i
end

.process_childs(pid) ⇒ Object

Recibe un PID y devuelve un array con los PID de sus hijos



60
61
62
63
64
# File 'lib/mutx/support/processes.rb', line 60

def self.process_childs(pid)
  res = Mutx::Support::Console.execute("ps -fea | grep #{pid} | grep -v grep | awk '$2!=#{pid} && $8!~/awk/ && $3==#{pid}{print $2}'")
  File.open("#{__FILE__}#{__LINE__}","a+"){|f| f.write res}
  res
end

.process_running?(pid) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
# File 'lib/mutx/support/processes.rb', line 49

def self.process_running? pid
  unless pid.nil?
    res = Mutx::Support::Console.execute("ps -p #{pid}").split("\n").last
    unless res.nil?
      res.include? "#{pid}" and !res.include? "<defunct>"
    end
  end
end

.sidekiq_pidsObject



71
72
73
74
75
# File 'lib/mutx/support/processes.rb', line 71

def self.sidekiq_pids
  res = Mutx::Support::Console.execute "ps -fea | grep 'sidekiq'"

  res.split("\n").select{|lines| !lines.include? "grep"}.map{|line| line.split[1]}
end