Module: Thin::Daemonizable
- Included in:
- Server
- Defined in:
- lib/thin/daemonizing.rb
Overview
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
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#log_file ⇒ Object
Returns the value of attribute log_file.
-
#pid_file ⇒ Object
Returns the value of attribute pid_file.
Class Method Summary collapse
Instance Method Summary collapse
-
#change_privilege(user, group = user) ⇒ Object
Change privileges of the process to the specified user and group.
-
#daemonize ⇒ Object
Turns the current script into a daemon process that detaches from the console.
-
#on_restart(&block) ⇒ Object
Register a proc to be called to restart the server.
- #pid ⇒ Object
-
#restart ⇒ Object
Restart the server.
Instance Attribute Details
#log_file ⇒ Object
Returns the value of attribute log_file.
25 26 27 |
# File 'lib/thin/daemonizing.rb', line 25 def log_file @log_file end |
#pid_file ⇒ Object
Returns the value of attribute pid_file.
25 26 27 |
# File 'lib/thin/daemonizing.rb', line 25 def pid_file @pid_file end |
Class Method Details
.included(base) ⇒ Object
27 28 29 |
# File 'lib/thin/daemonizing.rb', line 27 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#change_privilege(user, group = user) ⇒ Object
Change privileges of the process to the specified user and group.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/thin/daemonizing.rb', line 59 def change_privilege(user, group=user) log ">> Changing process privilege to #{user}:#{group}" uid, gid = Process.euid, Process.egid target_uid = Etc.getpwnam(user).uid target_gid = Etc.getgrnam(group).gid if uid != target_uid || gid != target_gid # Change process ownership Process.initgroups(user, target_gid) Process::GID.change_privilege(target_gid) Process::UID.change_privilege(target_uid) end rescue Errno::EPERM => e log "Couldn't change user and group to #{user}:#{group}: #{e}" end |
#daemonize ⇒ Object
Turns the current script into a daemon process that detaches from the console.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/thin/daemonizing.rb', line 36 def daemonize raise PlatformNotSupported, 'Daemonizing is not supported on Windows' if Thin.win? raise ArgumentError, 'You must specify a pid_file to daemonize' unless @pid_file remove_stale_pid_file pwd = Dir.pwd # Current directory is changed during daemonization, so store it Daemonize.daemonize(File.(@log_file), name) Dir.chdir(pwd) write_pid_file trap('HUP') { restart } at_exit do log ">> Exiting!" remove_pid_file end end |
#on_restart(&block) ⇒ Object
Register a proc to be called to restart the server.
77 78 79 |
# File 'lib/thin/daemonizing.rb', line 77 def on_restart(&block) @on_restart = block end |
#pid ⇒ Object
31 32 33 |
# File 'lib/thin/daemonizing.rb', line 31 def pid File.exist?(pid_file) ? open(pid_file).read.to_i : nil end |
#restart ⇒ Object
Restart the server.
82 83 84 85 86 87 88 89 |
# File 'lib/thin/daemonizing.rb', line 82 def restart raise ArgumentError, "Can't restart, no 'on_restart' proc specified" unless @on_restart log '>> Restarting ...' stop remove_pid_file @on_restart.call exit! end |