Class: Process::Daemon::ProcessFile

Inherits:
Object
  • Object
show all
Defined in:
lib/process/daemon/process_file.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



56
57
58
# File 'lib/process/daemon/process_file.rb', line 56

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

.clear(daemon) ⇒ Object

Removes the pid saved for a particular daemon



38
39
40
41
42
# File 'lib/process/daemon/process_file.rb', line 38

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

.recall(daemon) ⇒ Object

Retrieves the pid for the given daemon



33
34
35
# File 'lib/process/daemon/process_file.rb', line 33

def self.recall(daemon)
	File.read(daemon.process_file_path).to_i rescue nil
end

.running(daemon) ⇒ Object

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



45
46
47
48
49
50
51
52
53
# File 'lib/process/daemon/process_file.rb', line 45

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.



62
63
64
65
66
67
68
# File 'lib/process/daemon/process_file.rb', line 62

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

.store(daemon, pid) ⇒ Object

Saves the pid for the given daemon



28
29
30
# File 'lib/process/daemon/process_file.rb', line 28

def self.store(daemon, pid)
	File.write(daemon.process_file_path, pid)
end