Module: Thin::Daemonizable::ClassMethods

Defined in:
lib/thin/daemonizing.rb

Instance Method Summary collapse

Instance Method Details

#force_kill(pid_file) ⇒ Object



132
133
134
135
# File 'lib/thin/daemonizing.rb', line 132

def force_kill(pid_file)
  Process.kill("KILL", File.read(pid_file))      rescue nil
  File.delete(pid_file) if File.exist?(pid_file) rescue nil
end

#kill(pid_file, timeout = 60) ⇒ Object

Send a QUIT or INT (if timeout is 0) signal the process which PID is stored in pid_file. If the process is still running after timeout, KILL signal is sent.



96
97
98
99
100
101
102
# File 'lib/thin/daemonizing.rb', line 96

def kill(pid_file, timeout=60)
  if timeout == 0
    send_signal('INT', pid_file, timeout)
  else
    send_signal('QUIT', pid_file, timeout)
  end
end

#restart(pid_file) ⇒ Object

Restart the server by sending HUP signal.



105
106
107
# File 'lib/thin/daemonizing.rb', line 105

def restart(pid_file)
  send_signal('HUP', pid_file)
end

#send_signal(signal, pid_file, timeout = 60) ⇒ Object

Send a signal to the process which PID is stored in pid_file.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/thin/daemonizing.rb', line 110

def send_signal(signal, pid_file, timeout=60)
  if File.file?(pid_file) && pid = File.read(pid_file)
    pid = pid.to_i
    Logging.log "Sending #{signal} signal to process #{pid} ... "
    Process.kill(signal, pid)
    Timeout.timeout(timeout) do
      sleep 0.1 while Process.running?(pid)
    end
    Logging.log ""
  else
    puts "Can't stop process, no PID found in #{pid_file}"
  end
rescue Timeout::Error
  Logging.log "Timeout!"
  force_kill pid_file
rescue Interrupt
  force_kill pid_file
rescue Errno::ESRCH # No such process
  Logging.log "process not found!"
  force_kill pid_file
end