Class: RFlow::PIDFile
- Inherits:
-
Object
- Object
- RFlow::PIDFile
- Defined in:
- lib/rflow/pid_file.rb
Overview
Represents a file on disk that contains RFlow’s PID, for process management.
Instance Method Summary collapse
-
#initialize(path) ⇒ PIDFile
constructor
A new instance of PIDFile.
-
#read ⇒ Integer
Read the pid file and get the PID from it.
-
#running? ⇒ boolean
Determine if the application is running by checking the running PID and the pidfile.
-
#safe_unlink ⇒ void
Unlinks a PID file if it contains the current PID.
-
#signal(sig) ⇒ void
Signal the process with the matching PID with a given signal.
-
#write(pid = $$) ⇒ Integer
Write a new PID out to the pid file.
Constructor Details
#initialize(path) ⇒ PIDFile
Returns a new instance of PIDFile.
8 9 10 |
# File 'lib/rflow/pid_file.rb', line 8 def initialize(path) @path = path end |
Instance Method Details
#read ⇒ Integer
Read the pid file and get the PID from it.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rflow/pid_file.rb', line 14 def read return nil unless File.exist? path contents = File.read(path) if contents.empty? RFlow.logger.warn "Ignoring empty PID file #{path}" nil else contents.to_i end end |
#running? ⇒ boolean
Determine if the application is running by checking the running PID and the pidfile.
56 57 58 59 60 61 62 63 64 |
# File 'lib/rflow/pid_file.rb', line 56 def running? return false unless exist? pid = read return false unless pid Process.kill(0, pid) pid rescue Errno::ESRCH, Errno::ENOENT nil end |
#safe_unlink ⇒ void
This method returns an undefined value.
Unlinks a PID file if it contains the current PID. Still potentially racy without locking the directory (which is non-portable and may interact badly with other programs), but the window for hitting the race condition is small.
71 72 73 |
# File 'lib/rflow/pid_file.rb', line 71 def safe_unlink (current_process? and unlink) rescue nil end |
#signal(sig) ⇒ void
This method returns an undefined value.
Signal the process with the matching PID with a given signal.
77 78 79 |
# File 'lib/rflow/pid_file.rb', line 77 def signal(sig) Process.kill(sig, read) end |
#write(pid = $$) ⇒ Integer
Write a new PID out to the pid file.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rflow/pid_file.rb', line 27 def write(pid = $$) return unless validate? RFlow.logger.debug "Writing PID #{pid} to file '#{to_s}'" tmp_path = File.join(File.dirname(path), ".#{File.basename(path)}") if File.exist? tmp_path RFlow.logger.warn "Deleting stale temp PID file #{tmp_path}" File.delete(tmp_path) end pid_fp = begin File.open(tmp_path, File::RDWR|File::CREAT|File::EXCL, 0644) rescue Errno::ENOENT => e RFlow.logger.fatal "Error while writing temp PID file '#{tmp_path}'; containing directory may not exist?" RFlow.logger.fatal "Exception #{e.class}: #{e.message}" abort rescue Errno::EACCES => e RFlow.logger.fatal "Access error while writing temp PID file '#{tmp_path}'" RFlow.logger.fatal "Exception #{e.class}: #{e.message}" abort end pid_fp.syswrite("#{pid}\n") File.rename(pid_fp.path, path) pid_fp.close pid end |