Class: WatcherAction::KillProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/watcher_action/kill_process.rb

Overview

Kills the process with the given PID.

Options

pidfile

The file containing the process id

signal

The signal to send to the process. This may be an array or a comma-separated list of signals. If there is more than one signal, this will wait for wait seconds before trying the next signal, if the process didn’t die. Defaults to KILL

wait

Time to wait between signals (Default: 5)

restart_time

Time allowed for the process to restart. The action will not do anything if called again during this interval. (Default: 120)

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ KillProcess

Returns a new instance of KillProcess.



20
21
22
23
24
25
# File 'lib/watcher_action/kill_process.rb', line 20

def initialize(config)
  @pidfile = config.get_value(:pidfile, false)
  @signals = config.get_list(:signal, 'KILL')
  @wait = config.get_value(:wait, 5).to_i
  @restart_time = config.get_value(:restart_time, 120).to_i
end

Instance Method Details

#execute(event) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/watcher_action/kill_process.rb', line 27

def execute(event)
  if(@last_action && (Time.now - @last_action).floor < @restart_time)
    dog_log.info("Ignoring restart request in restarting time.")
    return false
  end
  
  Thread.new(@signals, @wait, @pidfile) do |signals, wait, pidfile|
    pid = File.open(pidfile) { |io| io.read }.to_i
    signals.each_with_index do |signal, index|
      sleep(wait) if(index > 0)
      if(WatchDogger.check_process(pid))
        dog_log.debug('KillerThread') { "Sending signal #{signal} to process #{pid}" }
        Process.kill(signal, pid)
      else
        dog_log.debug('KillerThread') { "Process #{pid} is dead." }
      end
    end
  end
  @last_action = Time.now
rescue Exception => e
  dog_log.warn { "Unable to kill process: #{e}" }
end