Class: Puma::Hunter

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/hunter/version.rb,
lib/puma/hunter/errors.rb,
lib/puma/hunter/cli.rb,
lib/puma/hunter.rb

Defined Under Namespace

Classes: CLI, PIDNotFound

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ppid, signal: "INT", max_mb: 1000, simulate: false) ⇒ Hunter

Returns a new instance of Hunter.



17
18
19
20
21
22
# File 'lib/puma/hunter.rb', line 17

def initialize(ppid, signal: "INT", max_mb: 1000, simulate: false)
  @ppid      = ppid
  @max_bytes = (max_mb * (1024 ** 2)).to_i
  @simulate  = simulate
  @signal    = signal
end

Class Method Details

.from_pidfile(path, *args) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/puma/hunter.rb', line 7

def self.from_pidfile(path, *args)
  pidfile = Pathname(path)

  if pid = pidfile.read
    new(pid.to_i, *args)
  else
    fail PIDNotFound, path
  end
end

Instance Method Details

#callObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puma/hunter.rb', line 24

def call
  used_rss = Procps::Memsize.new(used_bytes / 1024)

  warn "Running in simulate mode" if simulate?
  puts "#{used_rss.inspect} with #{workers.size} workers without master (#{@ppid})."

  if used_bytes >= @max_bytes
    pid_to_kill = workers[-1][:pid]
    warn "Out of max #{Procps::Memsize.new(@max_bytes / 1024).inspect}. Sending #{@signal} to PID #{pid_to_kill}."

    unless simulate?
      Process.kill(@signal, pid_to_kill)
    end
  end
end

#simulate?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/puma/hunter.rb', line 48

def simulate?
  !!@simulate
end

#used_bytesObject



44
45
46
# File 'lib/puma/hunter.rb', line 44

def used_bytes
  @used_bytes ||= workers.reduce(0) { |a, e| a + e[:rss] }
end

#workersObject



40
41
42
# File 'lib/puma/hunter.rb', line 40

def workers
  @workers ||= Procps::PS.new("/bin/ps").select(:pid, :ppid, :rss).where(ppid: @ppid).sort("+rss").to_a
end