Class: RExec::Daemon::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rexec/daemon/base.rb

Overview

This class is the base daemon class. If you are writing a daemon, you should inherit from this class.

The basic structure of a daemon is as follows: class Server < RExec::Daemon::Base def self.run # Long running process, e.g. web server, game server, etc. end end Server.daemonize

The base directory specifies a path such that:

working_directory = #{@@base_directory}/#{daemon_name}
log_directory = #{working_directory}/log
log_file_path = #{log_directory}/daemon.log
runtime_directory = #{working_directory}/run
process_file_path = #{runtime_directory}/daemon.pid

Constant Summary collapse

@@base_directory =

For a system-level daemon you might want to specify “/var”

"."

Class Method Summary collapse

Class Method Details

.crashed?Boolean

Check the last few lines of the log file to find out if the daemon crashed.

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rexec/daemon/base.rb', line 105

def self.crashed?
	File.open(log_file_path, "r") do |log_file|
		log_file.seek_end

		count = 3
		log_file.reverse_each_line do |line|
			return true if line.match("=== Daemon Crashed")

			count -= 1

			break if count == 0
		end
	end

	return false
end

.daemon_nameObject

Return the name of the daemon



50
51
52
# File 'lib/rexec/daemon/base.rb', line 50

def self.daemon_name
	return name.gsub(/[^a-zA-Z0-9]+/, '-')
end

.daemonizeObject

Corresponds to controller method of the same name



123
124
125
# File 'lib/rexec/daemon/base.rb', line 123

def self.daemonize
	Controller.daemonize(self)
end

.log_directoryObject

Return the directory to store log files in.



60
61
62
# File 'lib/rexec/daemon/base.rb', line 60

def self.log_directory
	File.join(working_directory, "log")
end

.log_file_pathObject

Standard log file for stdout and stderr.



65
66
67
# File 'lib/rexec/daemon/base.rb', line 65

def self.log_file_path
	File.join(log_directory, "#{daemon_name}.log")
end

.mark_logObject

Mark the output log.



80
81
82
83
84
# File 'lib/rexec/daemon/base.rb', line 80

def self.mark_log
	File.open(log_file_path, "a") do |log_file|
		log_file.puts "=== Log Marked @ #{Time.now.to_s} ==="
	end
end

.preforkObject

The main function to setup any environment required by the daemon



143
144
145
146
147
148
# File 'lib/rexec/daemon/base.rb', line 143

def self.prefork
	@@base_directory = File.expand_path(@@base_directory) if @@base_directory

	FileUtils.mkdir_p(log_directory)
	FileUtils.mkdir_p(runtime_directory)
end

.process_file_pathObject

Standard location of process pid file.



75
76
77
# File 'lib/rexec/daemon/base.rb', line 75

def self.process_file_path
	File.join(runtime_directory, "#{daemon_name}.pid")
end

.runObject

The main function to start the daemon



151
152
# File 'lib/rexec/daemon/base.rb', line 151

def self.run
end

.runtime_directoryObject

Runtime data directory for the daemon.



70
71
72
# File 'lib/rexec/daemon/base.rb', line 70

def self.runtime_directory
	File.join(working_directory, "run")
end

.shutdownObject

The main function to stop the daemon



155
156
157
158
# File 'lib/rexec/daemon/base.rb', line 155

def self.shutdown
	# Interrupt all children processes, preferably to stop them so that they are not left behind.
	Process.kill(0, :INT)
end

.startObject

Corresponds to controller method of the same name



128
129
130
# File 'lib/rexec/daemon/base.rb', line 128

def self.start
	Controller.start(self)
end

.statusObject

Corresponds to controller method of the same name



138
139
140
# File 'lib/rexec/daemon/base.rb', line 138

def self.status
	Controller.status(self)
end

.stopObject

Corresponds to controller method of the same name



133
134
135
# File 'lib/rexec/daemon/base.rb', line 133

def self.stop
	Controller.stop(self)
end

.tail_log(output) ⇒ Object

Prints some information relating to daemon startup problems.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rexec/daemon/base.rb', line 87

def self.tail_log(output)
	lines = []

	File.open(log_file_path, "r") do |log_file|
		log_file.seek_end

		log_file.reverse_each_line do |line|
			lines << line
			break if line.match("=== Log Marked") || line.match("=== Daemon Exception Backtrace")
		end
	end

	lines.reverse_each do |line|
		output.puts line
	end
end

.working_directoryObject

The directory the daemon will run in.



55
56
57
# File 'lib/rexec/daemon/base.rb', line 55

def self.working_directory
	@@base_directory
end