Class: Kaya::Support::Processes

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

Class Method Summary collapse

Class Method Details

.fork_this(command) ⇒ Object



90
91
92
# File 'lib/kaya/support/processes.rb', line 90

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

.kaya_pidsObject



62
63
64
65
66
# File 'lib/kaya/support/processes.rb', line 62

def self.kaya_pids
  res = Kaya::Support::Console.execute "ps -fea | grep 'unicorn.rb -p #{Kaya::Support::Configuration.port}'"

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

.kill_all_these(pids = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/kaya/support/processes.rb', line 74

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



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

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



36
37
38
39
40
# File 'lib/kaya/support/processes.rb', line 36

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

.kill_p(pid) ⇒ Object



31
32
33
34
# File 'lib/kaya/support/processes.rb', line 31

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

.pid_for(result_id) ⇒ Object



42
43
44
45
# File 'lib/kaya/support/processes.rb', line 42

def self.pid_for(result_id)
  result = Kaya::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



58
59
60
# File 'lib/kaya/support/processes.rb', line 58

def self.process_childs(pid)
  Kaya::Support::Console.execute("ps -fea | grep #{pid} | grep -v grep | awk '$2!=#{pid} && $8!~/awk/ && $3==#{pid}{print $2}'").split("\n")
end

.process_running?(pid) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/kaya/support/processes.rb', line 47

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

.sidekiq_pidsObject



68
69
70
71
72
# File 'lib/kaya/support/processes.rb', line 68

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

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