class GoodJob::Daemon
Manages daemonization of the current process.
Attributes
pidfile[R]
The path of the generated pidfile. @return [Pathname,String]
Public Class Methods
new(pidfile:)
click to toggle source
@param pidfile [Pathname,String] Pidfile path
# File lib/good_job/daemon.rb 12 def initialize(pidfile:) 13 @pidfile = pidfile 14 end
Public Instance Methods
daemonize()
click to toggle source
Daemonizes the current process and writes out a pidfile. @return [void]
# File lib/good_job/daemon.rb 18 def daemonize 19 check_pid 20 Process.daemon 21 write_pid 22 end
Private Instance Methods
check_pid()
click to toggle source
@return [void]
# File lib/good_job/daemon.rb 41 def check_pid 42 case pid_status(pidfile) 43 when :running, :not_owned 44 abort "A server is already running. Check #{pidfile}" 45 when :dead 46 File.delete(pidfile) 47 end 48 end
delete_pid()
click to toggle source
@return [void]
# File lib/good_job/daemon.rb 36 def delete_pid 37 File.delete(pidfile) if File.exist?(pidfile) 38 end
pid_status(pidfile)
click to toggle source
@param pidfile [Pathname, String] @return [Symbol]
# File lib/good_job/daemon.rb 52 def pid_status(pidfile) 53 return :exited unless File.exist?(pidfile) 54 55 pid = ::File.read(pidfile).to_i 56 return :dead if pid.zero? 57 58 Process.kill(0, pid) # check process status 59 :running 60 rescue Errno::ESRCH 61 :dead 62 rescue Errno::EPERM 63 :not_owned 64 end
write_pid()
click to toggle source
@return [void]
# File lib/good_job/daemon.rb 27 def write_pid 28 File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY) { |f| f.write(Process.pid.to_s) } 29 at_exit { File.delete(pidfile) if File.exist?(pidfile) } 30 rescue Errno::EEXIST 31 check_pid 32 retry 33 end