class Puma::Hunter

Constants

VERSION

Public Class Methods

from_pidfile(path, *args) click to toggle source
# 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
new(ppid, signal: "INT", max_mb: 1000, simulate: false) click to toggle source
# 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

Public Instance Methods

call() click to toggle source
# 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?() click to toggle source
# File lib/puma/hunter.rb, line 48
def simulate?
  !!@simulate
end
used_bytes() click to toggle source
# File lib/puma/hunter.rb, line 44
def used_bytes
  @used_bytes ||= workers.reduce(0) { |a, e| a + e[:rss] }
end
workers() click to toggle source
# 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