Class: RFlow::PIDFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rflow/pid_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ PIDFile

Returns a new instance of PIDFile.



7
8
9
# File 'lib/rflow/pid_file.rb', line 7

def initialize(path)
  @path = path
end

Instance Method Details

#readObject



11
12
13
14
# File 'lib/rflow/pid_file.rb', line 11

def read
  return nil unless File.exist? path
  File.read(path).to_i
end

#running?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
# File 'lib/rflow/pid_file.rb', line 33

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 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



47
48
49
# File 'lib/rflow/pid_file.rb', line 47

def safe_unlink
  (current_process? and unlink) rescue nil
end

#signal(sig) ⇒ Object



51
52
53
# File 'lib/rflow/pid_file.rb', line 51

def signal(sig)
  Process.kill(sig, read)
end

#to_sObject



55
56
57
# File 'lib/rflow/pid_file.rb', line 55

def to_s
  File.expand_path(path)
end

#write(pid = $$) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rflow/pid_file.rb', line 16

def write(pid = $$)
  return unless validate?

  RFlow.logger.debug "Writing PID #{pid} file '#{to_s}'"
  pid_fp = begin
             tmp_path = File.join(File.dirname(path), ".#{File.basename(path)}")
             File.open(tmp_path, File::RDWR|File::CREAT|File::EXCL, 0644)
           rescue Errno::EEXIST
             retry
           end
  pid_fp.syswrite("#{pid}\n")
  File.rename(pid_fp.path, path)
  pid_fp.close

  pid
end