Module: Thin::Daemonizable::ClassMethods

Module Thin::Daemonizable::ClassMethods < Base

(in files lib/thin/daemonizing.rb )

Module included in classes that can be turned into a daemon. Handle stuff like:

  • storing the PID in a file
  • redirecting output to the log file
  • changing processs privileges
  • killing the process gracefully

Methods

Public Instance force_kill(pid_file)

     # File lib/thin/daemonizing.rb, line 132
132:       def force_kill(pid_file)
133:         Process.kill("KILL", File.read(pid_file))      rescue nil
134:         File.delete(pid_file) if File.exist?(pid_file) rescue nil
135:       end

Public Instance kill(pid_file, timeout=60)

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.

     # File lib/thin/daemonizing.rb, line 96
 96:       def kill(pid_file, timeout=60)
 97:         if timeout == 0
 98:           send_signal('INT', pid_file, timeout)
 99:         else
100:           send_signal('QUIT', pid_file, timeout)
101:         end
102:       end

Public Instance restart(pid_file)

Restart the server by sending HUP signal.

     # File lib/thin/daemonizing.rb, line 105
105:       def restart(pid_file)
106:         send_signal('HUP', pid_file)
107:       end

Public Instance send_signal(signal, pid_file, timeout=60)

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

     # File lib/thin/daemonizing.rb, line 110
110:       def send_signal(signal, pid_file, timeout=60)
111:         if File.file?(pid_file) && pid = File.read(pid_file)
112:           pid = pid.to_i
113:           Logging.log "Sending #{signal} signal to process #{pid} ... "
114:           Process.kill(signal, pid)
115:           Timeout.timeout(timeout) do
116:             sleep 0.1 while Process.running?(pid)
117:           end
118:           Logging.log ""
119:         else
120:           puts "Can't stop process, no PID found in #{pid_file}"
121:         end
122:       rescue Timeout::Error
123:         Logging.log "Timeout!"
124:         force_kill pid_file
125:       rescue Interrupt
126:         force_kill pid_file
127:       rescue Errno::ESRCH # No such process
128:         Logging.log "process not found!"
129:         force_kill pid_file
130:       end