Class: ZombiePassengerKiller::Reaper

Inherits:
Object
  • Object
show all
Defined in:
lib/zombie_passenger_killer/reaper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Reaper

Returns a new instance of Reaper.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/zombie_passenger_killer/reaper.rb', line 6

def initialize(options)
  @history = {}
  @history_entries = options[:history] || 5
  @max_high_cpu = options[:max]
  @high_cpu = options[:cpu] || 70
  @grace_time = options[:grace] || 5
  @pattern = options[:pattern] || ' Rack: '
  @show_times = options[:show_times] || false
  @interval = options[:interval] || 10
  @strace_time = 5
  @out = STDOUT
  @rvmsudo = options[:rvmsudo]
end

Instance Attribute Details

#outObject

overwriteable for tests



4
5
6
# File 'lib/zombie_passenger_killer/reaper.rb', line 4

def out
  @out
end

Instance Method Details

#hunt_zombiesObject



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

def hunt_zombies
  active_pids_in_passenger_status = passenger_pids
  active_processes_in_processlist = process_status
  zombies = active_processes_in_processlist.map{|x| x[:pid] } - active_pids_in_passenger_status rescue Array.new

  # kill processes with high CPU if user wants it
  high_load = if @max_high_cpu
    store_current_cpu active_processes_in_processlist
    active_pids_in_passenger_status.select do |pid|
      (@history[pid] || []).count{|x| x > @high_cpu } >= @max_high_cpu
    end
  else
    []
  end

  (high_load + zombies).each do |pid|
    kill_zombie pid
  end
end

#lurkObject



20
21
22
23
24
25
26
27
28
# File 'lib/zombie_passenger_killer/reaper.rb', line 20

def lurk
  loop do
    hunt_zombies
    sleep @interval
  end
rescue Interrupt
  log "Exiting..."
  raise $!
end