Class: RExec::Daemon::Base
- Inherits:
-
Object
- Object
- RExec::Daemon::Base
- 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.
Constant Summary collapse
- @@var_directory =
nil
- @@log_directory =
nil
- @@pid_directory =
nil
Class Method Summary collapse
-
.crashed? ⇒ Boolean
Check the last few lines of the log file to find out if the daemon crashed.
-
.daemon_name ⇒ Object
Return the name of the daemon.
-
.daemonize ⇒ Object
Corresponds to controller method of the same name.
-
.err_fn ⇒ Object
Standard log file for errors.
-
.log_directory ⇒ Object
Return the directory to store log files in.
-
.log_fn ⇒ Object
Standard log file for normal output.
-
.mark_err_log ⇒ Object
Mark the error log.
-
.pid_directory ⇒ Object
Standard location of pid file.
-
.pid_fn ⇒ Object
Standard pid file.
-
.prefork ⇒ Object
The main function to setup any environment required by the daemon.
-
.run ⇒ Object
The main function to start the daemon.
-
.shutdown ⇒ Object
The main function to stop the daemon.
-
.start ⇒ Object
Corresponds to controller method of the same name.
-
.status ⇒ Object
Corresponds to controller method of the same name.
-
.stop ⇒ Object
Corresponds to controller method of the same name.
-
.tail_err_log(outp) ⇒ Object
Prints some information relating to daemon startup problems.
-
.var_directory ⇒ Object
Base directory for daemon log files / run files.
-
.working_directory ⇒ Object
The directory the daemon will run in (Dir.chdir).
Class Method Details
.crashed? ⇒ Boolean
Check the last few lines of the log file to find out if the daemon crashed.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rexec/daemon/base.rb', line 95 def self.crashed? File.open(err_fn, "r") do |fp| fp.seek_end count = 2 fp.reverse_each_line do |line| return true if line.match("=== Daemon Crashed") count -= 1 break if count == 0 end end return false end |
.daemon_name ⇒ Object
Return the name of the daemon
29 30 31 |
# File 'lib/rexec/daemon/base.rb', line 29 def self.daemon_name return name.gsub(/[^a-zA-Z0-9]+/, '-') end |
.daemonize ⇒ Object
Corresponds to controller method of the same name
113 114 115 |
# File 'lib/rexec/daemon/base.rb', line 113 def self.daemonize Controller.daemonize(self) end |
.err_fn ⇒ Object
Standard log file for errors
49 50 51 |
# File 'lib/rexec/daemon/base.rb', line 49 def self.err_fn File.join(log_directory, "stderr.log") end |
.log_directory ⇒ Object
Return the directory to store log files in
44 45 46 |
# File 'lib/rexec/daemon/base.rb', line 44 def self.log_directory @@log_directory || File.join(var_directory, "log", daemon_name) end |
.log_fn ⇒ Object
Standard log file for normal output
54 55 56 |
# File 'lib/rexec/daemon/base.rb', line 54 def self.log_fn File.join(log_directory, "stdout.log") end |
.mark_err_log ⇒ Object
Mark the error log
69 70 71 72 73 |
# File 'lib/rexec/daemon/base.rb', line 69 def self.mark_err_log fp = File.open(err_fn, "a") fp.puts "=== Error Log Opened @ #{Time.now.to_s} ===" fp.close end |
.pid_directory ⇒ Object
Standard location of pid file
59 60 61 |
# File 'lib/rexec/daemon/base.rb', line 59 def self.pid_directory @@pid_directory || File.join(var_directory, "run", daemon_name) end |
.pid_fn ⇒ Object
Standard pid file
64 65 66 |
# File 'lib/rexec/daemon/base.rb', line 64 def self.pid_fn File.join(pid_directory, "#{daemon_name}.pid") end |
.prefork ⇒ Object
The main function to setup any environment required by the daemon
133 134 135 136 137 138 139 140 |
# File 'lib/rexec/daemon/base.rb', line 133 def self.prefork @@var_directory = File.(@@var_directory) if @@var_directory @@log_directory = File.(@@log_directory) if @@log_directory @@pid_directory = File.(@@pid_directory) if @@pid_directory FileUtils.mkdir_p(log_directory) FileUtils.mkdir_p(pid_directory) end |
.run ⇒ Object
The main function to start the daemon
143 144 |
# File 'lib/rexec/daemon/base.rb', line 143 def self.run end |
.shutdown ⇒ Object
The main function to stop the daemon
147 148 |
# File 'lib/rexec/daemon/base.rb', line 147 def self.shutdown end |
.start ⇒ Object
Corresponds to controller method of the same name
118 119 120 |
# File 'lib/rexec/daemon/base.rb', line 118 def self.start Controller.start(self) end |
.status ⇒ Object
Corresponds to controller method of the same name
128 129 130 |
# File 'lib/rexec/daemon/base.rb', line 128 def self.status Controller.status(self) end |
.stop ⇒ Object
Corresponds to controller method of the same name
123 124 125 |
# File 'lib/rexec/daemon/base.rb', line 123 def self.stop Controller.stop(self) end |
.tail_err_log(outp) ⇒ Object
Prints some information relating to daemon startup problems
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rexec/daemon/base.rb', line 76 def self.tail_err_log(outp) lines = [] File.open(err_fn, "r") do |fp| fp.seek_end fp.reverse_each_line do |line| lines << line break if line.match("=== Error Log") || line.match("=== Daemon Exception Backtrace") end end lines.reverse_each do |line| outp.puts line end end |
.var_directory ⇒ Object
Base directory for daemon log files / run files
34 35 36 |
# File 'lib/rexec/daemon/base.rb', line 34 def self.var_directory @@var_directory || File.join("", "var") end |
.working_directory ⇒ Object
The directory the daemon will run in (Dir.chdir)
39 40 41 |
# File 'lib/rexec/daemon/base.rb', line 39 def self.working_directory var_directory end |