lib/rflow/pid_file.rb in rflow-1.3.0 vs lib/rflow/pid_file.rb in rflow-1.3.1
- old
+ new
@@ -1,15 +1,18 @@
class RFlow
+ # Represents a file on disk that contains RFlow's PID, for process management.
class PIDFile
private
attr_reader :path
public
def initialize(path)
@path = path
end
+ # Read the pid file and get the PID from it.
+ # @return [Integer]
def read
return nil unless File.exist? path
contents = File.read(path)
if contents.empty?
RFlow.logger.warn "Ignoring empty PID file #{path}"
@@ -17,10 +20,12 @@
else
contents.to_i
end
end
+ # Write a new PID out to the pid file.
+ # @return [Integer] the pid
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)}")
@@ -40,31 +45,37 @@
pid_fp.close
pid
end
+ # Determine if the application is running by checking the running PID and the pidfile.
+ # @return [boolean]
def running?
return false unless exist?
pid = read
return false unless pid
Process.kill(0, pid)
pid
rescue Errno::ESRCH, Errno::ENOENT
nil
end
- # unlinks a PID file at given if it contains the current PID still
+ # 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
+ # window for hitting the race condition is small.
+ # @return [void]
def safe_unlink
(current_process? and unlink) rescue nil
end
+ # Signal the process with the matching PID with a given signal.
+ # @return [void]
def signal(sig)
Process.kill(sig, read)
end
+ # @!visibility private
def to_s
File.expand_path(path)
end
private