Class: MultiDaemons::Pid

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_daemons/pid.rb

Constant Summary collapse

KILL_TIMEOUT =
30

Class Method Summary collapse

Class Method Details

.default_timeoutObject


50
51
52
# File 'lib/multi_daemons/pid.rb', line 50

def self.default_timeout
  KILL_TIMEOUT
end

.force_kill(pids, timeout = KILL_TIMEOUT) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/multi_daemons/pid.rb', line 18

def self.force_kill(pids, timeout = KILL_TIMEOUT)
  Timeout.timeout(force_kill_timeout(timeout), Timeout::Error) do
    pids.each do |pid|
      sleep(0.5) while Pid.running?(pid)
    end
  end
  true
rescue Timeout::Error
  Log.log 'Force stopping processes'
  pids.each do |pid|
    begin
      Process.kill('KILL', pid)
    rescue Errno::ESRCH
    end
  end

  begin
    Timeout.timeout(default_timeout, Timeout::Error) do
      pids.each do |pid|
        sleep 1 while Pid.running?(pid)
      end
    end
  rescue Timeout::Error
    Log.log 'Can not stop processes'
    return false
  end
end

.force_kill_timeout(timeout) ⇒ Object


46
47
48
# File 'lib/multi_daemons/pid.rb', line 46

def self.force_kill_timeout(timeout)
  timeout || KILL_TIMEOUT
end

.running?(pid) ⇒ Boolean

Returns:

  • (Boolean)

5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/multi_daemons/pid.rb', line 5

def self.running?(pid)
  return false unless pid

  begin
    Process.kill(0, pid)
    return true
  rescue Errno::ESRCH
    return false
  rescue Exception
    return true
  end
end