class Unicorn::Wrangler::UnicornProcess

Attributes

pid[R]
pidfile[R]

Public Class Methods

from_pidfile(pidfile) click to toggle source
# File lib/unicorn/wrangler.rb, line 133
def self.from_pidfile(pidfile)
  File.exist?(pidfile) && new(pidfile)
end
new(pidfile) click to toggle source
# File lib/unicorn/wrangler.rb, line 47
def initialize(pidfile)
  @pidfile = pidfile
  @pid = File.read(pidfile).to_i
  recall_assassin
end
start(pidfile, command) click to toggle source
# File lib/unicorn/wrangler.rb, line 127
def self.start(pidfile, command)
  Process.spawn(command, pgroup: true)
  wait_for(60) { File.exist?(pidfile) }
  new(pidfile)
end

Public Instance Methods

assassin_pidfile() click to toggle source
# File lib/unicorn/wrangler.rb, line 115
def assassin_pidfile
  pidfile + ".assassin"
end
launch_assassin(grace_period) click to toggle source
# File lib/unicorn/wrangler.rb, line 84
def launch_assassin(grace_period)
  if running? && !@assassin_launched
    @assassin_launched = true
    debug "preparing to kill unicorn #{pid} in #{grace_period} seconds"
    unless fork
      $0 = "unicorn-wrangler (waiting to kill #{pid})"
      File.write(assassin_pidfile, Process.pid.to_s)

      trap_signals :TERM do
        debug "Trapped and killed assassin"
        exit
      end

      Process.setsid
      sleep grace_period
      terminate
      File.delete(assassin_pidfile)
    end
  end
end
recall_assassin() click to toggle source
# File lib/unicorn/wrangler.rb, line 105
def recall_assassin
  if File.exist?(assassin_pidfile)
    assassin_pid = File.read(assassin_pidfile).to_i
    debug "Recalling assassin with pid #{assassin_pid}"
    Process.kill 'KILL', assassin_pid
    File.delete assassin_pidfile
  end
rescue Errno::ESRCH
end
reload(grace_period) click to toggle source
# File lib/unicorn/wrangler.rb, line 64
def reload(grace_period)
  signal :USR2
  if wait_for(grace_period) { reloaded_unicorn }
    Thread.new do
      sleep grace_period
      terminate
    end
    reloaded_unicorn
  else
    raise "unicorn didn't reload correctly within grace period (was pid #{pid})"
  end
end
reloaded_unicorn() click to toggle source
# File lib/unicorn/wrangler.rb, line 77
def reloaded_unicorn
  reloaded = UnicornProcess.from_pidfile(pidfile)
  if reloaded && pid != reloaded.pid
    reloaded
  end
end
running?() click to toggle source
# File lib/unicorn/wrangler.rb, line 53
def running?
  Process.getpgid(pid)
rescue Errno::ESRCH
  false
end
signal(msg) click to toggle source
# File lib/unicorn/wrangler.rb, line 59
def signal(msg)
  debug "Sending signal #{msg} to #{pid}"
  Process.kill msg.to_s, pid
end
terminate() click to toggle source
# File lib/unicorn/wrangler.rb, line 119
def terminate
  if running?
    signal :TERM
  else
    warn "Attempt to terminate #{pid} failed as process not running"
  end
end