Module: RExec::Daemon::PidFile

Defined in:
lib/rexec/daemon/pidfile.rb

Overview

This module controls the storage and retrieval of process id files.

Class Method Summary collapse

Class Method Details

.cleanup(daemon) ⇒ Object

Remove the pid file if the daemon is not running



49
50
51
# File 'lib/rexec/daemon/pidfile.rb', line 49

def self.cleanup(daemon)
  clear(daemon) unless running(daemon)
end

.clear(daemon) ⇒ Object

Removes the pid saved for a particular daemon



31
32
33
34
35
# File 'lib/rexec/daemon/pidfile.rb', line 31

def self.clear(daemon)
  if File.exist? daemon.pid_fn
    FileUtils.rm(daemon.pid_fn)
  end
end

.recall(daemon) ⇒ Object

Retrieves the pid for the given daemon



26
27
28
# File 'lib/rexec/daemon/pidfile.rb', line 26

def self.recall(daemon)
  IO.read(daemon.pid_fn).to_i rescue nil
end

.running(daemon) ⇒ Object

Checks whether the daemon is running by checking the saved pid and checking the corresponding process



38
39
40
41
42
43
44
45
46
# File 'lib/rexec/daemon/pidfile.rb', line 38

def self.running(daemon)
  pid = recall(daemon)

  return false if pid == nil

  gpid = Process.getpgid(pid) rescue nil

  return gpid != nil ? true : false
end

.status(daemon) ⇒ Object

This function returns the status of the daemon. This can be one of :running, :unknown (pid file exists but no corresponding process can be found) or :stopped.



55
56
57
58
59
60
61
# File 'lib/rexec/daemon/pidfile.rb', line 55

def self.status(daemon)
  if File.exist? daemon.pid_fn
    return PidFile.running(daemon) ? :running : :unknown
  else
    return :stopped
  end
end

.store(daemon, pid) ⇒ Object

Saves the pid for the given daemon



21
22
23
# File 'lib/rexec/daemon/pidfile.rb', line 21

def self.store(daemon, pid)
  File.open(daemon.pid_fn, 'w') {|f| f << pid}
end