Module: Cognizant::System::Signal::ClassMethods

Defined in:
lib/cognizant/system/signal.rb

Instance Method Summary collapse

Instance Method Details

#send_signals(pid, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cognizant/system/signal.rb', line 13

def send_signals(pid, options = {})
  # Return if the process is not running.
  return true unless pid_running?(pid)

  signals = options[:signals] || ["TERM", "INT"]
  timeout = options[:timeout] || 30

  catch :stopped do
    signals.each do |stop_signal|
      # Send the stop signal and wait for it to stop.
      signal(stop_signal, pid)

      # Poll to see if it's stopped yet. Minimum 2 so that we check at least once again.
      ([timeout / signals.size, 2].max).times do
        throw :stopped unless pid_running?(pid)
        sleep 1
      end
    end
  end
  not pid_running?(pid)
end

#signal(signal, pid) ⇒ Object



9
10
11
# File 'lib/cognizant/system/signal.rb', line 9

def signal(signal, pid)
  ::Process.kill(signal, pid) if pid and pid != 0
end