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 130
130:       def force_kill(pid_file)
131:         if pid = read_pid_file(pid_file)
132:           Logging.log "Sending KILL signal to process #{pid} ... "
133:           Process.kill("KILL", pid)
134:           File.delete(pid_file) if File.exist?(pid_file)
135:         else
136:           Logging.log "Can't stop process, no PID found in #{pid_file}"
137:         end
138:       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 read_pid_file(file)

     # File lib/thin/daemonizing.rb, line 140
140:       def read_pid_file(file)
141:         if File.file?(file) && pid = File.read(file)
142:           pid.to_i
143:         else
144:           nil
145:         end
146:       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 pid = read_pid_file(pid_file)
112:           Logging.log "Sending #{signal} signal to process #{pid} ... "
113:           Process.kill(signal, pid)
114:           Timeout.timeout(timeout) do
115:             sleep 0.1 while Process.running?(pid)
116:           end
117:         else
118:           Logging.log "Can't stop process, no PID found in #{pid_file}"
119:         end
120:       rescue Timeout::Error
121:         Logging.log "Timeout!"
122:         force_kill pid_file
123:       rescue Interrupt
124:         force_kill pid_file
125:       rescue Errno::ESRCH # No such process
126:         Logging.log "process not found!"
127:         force_kill pid_file
128:       end