Module: Resqued::Pidfile

Included in:
Master
Defined in:
lib/resqued/pidfile.rb

Overview

Mixin that manages a pidfile for a process.

Instance Method Summary collapse

Instance Method Details

#remove_pidfile(filename) ⇒ Object

Private.



27
28
29
# 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) ⇒ Object

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



5
6
7
8
9
10
# 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) ⇒ Object

Private.



13
14
15
16
17
18
19
20
21
22
23
24
# 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