Class: Racecar::Daemon
- Inherits:
-
Object
- Object
- Racecar::Daemon
- Defined in:
- lib/racecar/daemon.rb
Instance Attribute Summary collapse
-
#pidfile ⇒ Object
readonly
Returns the value of attribute pidfile.
Instance Method Summary collapse
- #check_pid ⇒ Object
- #daemonize! ⇒ Object
-
#initialize(pidfile) ⇒ Daemon
constructor
A new instance of Daemon.
- #pid ⇒ Object
- #pid_status ⇒ Object
- #pidfile? ⇒ Boolean
- #redirect_output(logfile) ⇒ Object
- #running? ⇒ Boolean
- #stop! ⇒ Object
- #suppress_input ⇒ Object
- #suppress_output ⇒ Object
- #write_pid ⇒ Object
Constructor Details
Instance Attribute Details
#pidfile ⇒ Object (readonly)
Returns the value of attribute pidfile.
5 6 7 |
# File 'lib/racecar/daemon.rb', line 5 def pidfile @pidfile end |
Instance Method Details
#check_pid ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/racecar/daemon.rb', line 44 def check_pid if pidfile? case pid_status when :running, :not_owned $stderr.puts "=> Racecar is already running with that PID file (#{pidfile})" exit(1) when :dead File.delete(pidfile) end end end |
#daemonize! ⇒ Object
17 18 19 20 21 22 |
# File 'lib/racecar/daemon.rb', line 17 def daemonize! exit if fork Process.setsid exit if fork Dir.chdir "/" end |
#pid ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/racecar/daemon.rb', line 56 def pid if File.exist?(pidfile) File.read(pidfile).to_i else nil end end |
#pid_status ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/racecar/daemon.rb', line 72 def pid_status return :exited if pid.nil? return :dead if pid == 0 # This will raise Errno::ESRCH if the process doesn't exist. Process.kill(0, pid) :running rescue Errno::ESRCH :dead rescue Errno::EPERM :not_owned end |
#pidfile? ⇒ Boolean
13 14 15 |
# File 'lib/racecar/daemon.rb', line 13 def pidfile? !pidfile.nil? end |
#redirect_output(logfile) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/racecar/daemon.rb', line 24 def redirect_output(logfile) FileUtils.mkdir_p(File.dirname(logfile), mode: 0755) FileUtils.touch(logfile) File.chmod(0644, logfile) $stderr.reopen(logfile, 'a') $stdout.reopen($stderr) $stdout.sync = $stderr.sync = true end |
#running? ⇒ Boolean
64 65 66 |
# File 'lib/racecar/daemon.rb', line 64 def running? pid_status == :running || pid_status == :not_owned end |
#stop! ⇒ Object
68 69 70 |
# File 'lib/racecar/daemon.rb', line 68 def stop! Process.kill("TERM", pid) end |
#suppress_input ⇒ Object
35 36 37 |
# File 'lib/racecar/daemon.rb', line 35 def suppress_input $stdin.reopen('/dev/null') end |
#suppress_output ⇒ Object
39 40 41 42 |
# File 'lib/racecar/daemon.rb', line 39 def suppress_output $stderr.reopen('/dev/null', 'a') $stdout.reopen($stderr) end |
#write_pid ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/racecar/daemon.rb', line 86 def write_pid File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY) do |f| f.write(Process.pid.to_s) end at_exit do File.delete(pidfile) if File.exist?(pidfile) end rescue Errno::EEXIST check_pid retry end |