class Quebert::Support::PidFile

Deal with all of our pid file stuff

Constants

ProcessRunning

Attributes

path[R]

Public Class Methods

new(path) click to toggle source
# File lib/quebert/support/pid_file.rb, line 11
def initialize(path)
  @path = path
end
read(file) click to toggle source

Read pids and turn them into ints

# File lib/quebert/support/pid_file.rb, line 20
def self.read(file)
  if File.file?(file) && pid = File.read(file)
    pid.to_i
  else
    nil
  end
end
running?(pid) click to toggle source

Tells us if a current process is running

# File lib/quebert/support/pid_file.rb, line 29
def self.running?(pid)
  Process.getpgid(pid) != -1
rescue Errno::EPERM
  true
rescue Errno::ESRCH
  false
end

Public Instance Methods

write!() click to toggle source
# File lib/quebert/support/pid_file.rb, line 15
def write!
  remove_stale and write
end

Private Instance Methods

exists?() click to toggle source
# File lib/quebert/support/pid_file.rb, line 66
def exists?
  File.exist?(path)
end
pid() click to toggle source
# File lib/quebert/support/pid_file.rb, line 58
def pid
  self.class.read(path)
end
remove() click to toggle source
# File lib/quebert/support/pid_file.rb, line 48
def remove
  File.delete(path) if exists?
  true
end
remove_stale() click to toggle source

If PID file is stale, remove it.

# File lib/quebert/support/pid_file.rb, line 39
def remove_stale
  if exists? && running?
    raise ProcessRunning, "#{path} already exists, seems like it's already running (process ID: #{pid}). " +
                        "Stop the process or delete #{path}."
  else
    remove
  end
end
running?() click to toggle source
# File lib/quebert/support/pid_file.rb, line 62
def running?
  self.class.running?(pid)
end
write() click to toggle source
# File lib/quebert/support/pid_file.rb, line 53
def write
  File.open(path,"w") { |f| f.write(Process.pid) }
  File.chmod(0644, path)
end