module Resqued::Pidfile

Mixin that manages a pidfile for a process.

Public Instance Methods

remove_pidfile(filename) click to toggle source

Private.

# File lib/resqued/pidfile.rb, line 27
def remove_pidfile(filename)
  (File.read(filename).to_i == $$) and File.unlink(filename) rescue nil
end
with_pidfile(filename) { || ... } click to toggle source

Public: Create a pidfile, execute the block, then remove the pidfile.

# File lib/resqued/pidfile.rb, line 5
def with_pidfile(filename)
  write_pidfile(filename) if filename
  yield
ensure
  remove_pidfile(filename) if filename
end
write_pidfile(filename) click to toggle source

Private.

# File lib/resqued/pidfile.rb, line 13
def write_pidfile(filename)
  pf =
    begin
      tmp = "#{filename}.#{rand}.#{$$}"
      File.open(tmp, File::RDWR | File::CREAT | File::EXCL, 0644)
    rescue Errno::EEXIST
      retry
    end
  pf.syswrite("#{$$}\n")
  File.rename(pf.path, filename)
  pf.close
end