Thin::Daemonizable

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

Attributes

pid_file[RW]
log_file[RW]

Public Class Methods

included(base) click to toggle source
    # File lib/thin/daemonizing.rb, line 27
27:     def self.included(base)
28:       base.extend ClassMethods
29:     end

Public Instance Methods

change_privilege(user, group=user) click to toggle source

Change privileges of the process to the specified user and group.

    # File lib/thin/daemonizing.rb, line 62
62:     def change_privilege(user, group=user)
63:       log ">> Changing process privilege to #{user}:#{group}"
64:       
65:       uid, gid = Process.euid, Process.egid
66:       target_uid = Etc.getpwnam(user).uid
67:       target_gid = Etc.getgrnam(group).gid
68: 
69:       if uid != target_uid || gid != target_gid
70:         # Change process ownership
71:         Process.initgroups(user, target_gid)
72:         Process::GID.change_privilege(target_gid)
73:         Process::UID.change_privilege(target_uid)
74:       end
75:     rescue Errno::EPERM => e
76:       log "Couldn't change user and group to #{user}:#{group}: #{e}"
77:     end
daemonize() click to toggle source

Turns the current script into a daemon process that detaches from the console.

    # File lib/thin/daemonizing.rb, line 36
36:     def daemonize
37:       raise PlatformNotSupported, 'Daemonizing is not supported on Windows'     if Thin.win?
38:       raise ArgumentError,        'You must specify a pid_file to daemonize' unless @pid_file
39:       
40:       remove_stale_pid_file
41:       
42:       pwd = Dir.pwd # Current directory is changed during daemonization, so store it
43:       
44:       # HACK we need to create the directory before daemonization to prevent a bug under 1.9
45:       #      ignoring all signals when the directory is created after daemonization.
46:       FileUtils.mkdir_p File.dirname(@pid_file)
47:       
48:       Daemonize.daemonize(File.expand_path(@log_file), name)
49:       
50:       Dir.chdir(pwd)
51:       
52:       write_pid_file
53:       
54:       at_exit do
55:         log ">> Exiting!"
56:         remove_pid_file
57:       end
58:     end
on_restart(&block) click to toggle source

Register a proc to be called to restart the server.

    # File lib/thin/daemonizing.rb, line 80
80:     def on_restart(&block)
81:       @on_restart = block
82:     end
pid() click to toggle source
    # File lib/thin/daemonizing.rb, line 31
31:     def pid
32:       File.exist?(pid_file) ? open(pid_file).read.to_i : nil
33:     end
restart() click to toggle source

Restart the server.

    # File lib/thin/daemonizing.rb, line 85
85:     def restart
86:       raise ArgumentError, "Can't restart, no 'on_restart' proc specified" unless @on_restart
87:       log '>> Restarting ...'
88:       stop
89:       remove_pid_file
90:       @on_restart.call
91:       exit!
92:     end

Protected Instance Methods

remove_pid_file() click to toggle source
     # File lib/thin/daemonizing.rb, line 153
153:       def remove_pid_file
154:         File.delete(@pid_file) if @pid_file && File.exists?(@pid_file)
155:       end
remove_stale_pid_file() click to toggle source

If PID file is stale, remove it.

     # File lib/thin/daemonizing.rb, line 164
164:       def remove_stale_pid_file
165:         if File.exist?(@pid_file)
166:           if pid && Process.running?(pid)
167:             raise PidFileExist, "#{@pid_file} already exists, seems like it's already running (process ID: #{pid}). " +
168:                                 "Stop the process or delete #{@pid_file}."
169:           else
170:             log ">> Deleting stale PID file #{@pid_file}"
171:             remove_pid_file
172:           end
173:         end
174:       end
write_pid_file() click to toggle source
     # File lib/thin/daemonizing.rb, line 157
157:       def write_pid_file
158:         log ">> Writing PID to #{@pid_file}"
159:         open(@pid_file,"w") { |f| f.write(Process.pid) }
160:         File.chmod(0644, @pid_file)
161:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.