class Sanford::PIDFile

Constants

InvalidError

Attributes

path[R]

Public Class Methods

new(path) click to toggle source
# File lib/sanford/pid_file.rb, line 8
def initialize(path)
  @path = (path || '/dev/null').to_s
end

Public Instance Methods

pid() click to toggle source
# File lib/sanford/pid_file.rb, line 12
def pid
  pid = File.read(@path).strip
  pid && !pid.empty? ? pid.to_i : raise('no pid in file')
rescue StandardError => exception
  error = InvalidError.new("A PID couldn't be read from #{@path.inspect}")
  error.set_backtrace(exception.backtrace)
  raise error
end
remove() click to toggle source
# File lib/sanford/pid_file.rb, line 30
def remove
  FileUtils.rm_f(@path)
end
to_s() click to toggle source
# File lib/sanford/pid_file.rb, line 34
def to_s
  @path
end
write() click to toggle source
# File lib/sanford/pid_file.rb, line 21
def write
  FileUtils.mkdir_p(File.dirname(@path))
  File.open(@path, 'w'){ |f| f.puts ::Process.pid }
rescue StandardError => exception
  error = InvalidError.new("Can't write pid to file #{@path.inspect}")
  error.set_backtrace(exception.backtrace)
  raise error
end