Class: Pidly::Control
- Inherits:
-
Object
- Object
- Pidly::Control
- Defined in:
- lib/pidly/control.rb
Overview
Pidly daemon control
Instance Attribute Summary collapse
-
#allow_multiple ⇒ Object
Returns the value of attribute allow_multiple.
-
#daemon ⇒ Object
Returns the value of attribute daemon.
-
#error_count ⇒ Object
Returns the value of attribute error_count.
-
#log_file ⇒ Object
Returns the value of attribute log_file.
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#pid ⇒ Object
Returns the value of attribute pid.
-
#pid_file ⇒ Object
Returns the value of attribute pid_file.
-
#sync_log ⇒ Object
Returns the value of attribute sync_log.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Attributes included from Logger
Class Method Summary collapse
Instance Method Summary collapse
-
#clean! ⇒ Object
Clean.
-
#fetch_class_var(name) ⇒ Symbol, ...
Fetch Class Variable.
-
#initialize(options = {}) ⇒ Control
constructor
Initialize control object.
-
#kill(remove_pid_file = true) ⇒ Object
Kill.
-
#restart ⇒ Object
Restart.
-
#running? ⇒ true, false
Running?.
-
#start ⇒ Object
Start.
-
#status ⇒ String
Status.
-
#stop ⇒ Object
Stop.
Methods included from Logger
Methods included from Callbacks
#after_stop, #before_start, #error, included
Constructor Details
#initialize(options = {}) ⇒ Control
Initialize control object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/pidly/control.rb', line 63 def initialize(={}) @messages = [] @error_count = 0 @name = .fetch(:name) @path = if .has_key?(:path) Pathname.new(.fetch(:path)) else Pathname.new('/tmp') end unless @path.directory? raise('Path does not exist or is not a directory.') end unless @path.readable? && @path.writable? raise('Path must be readable and writable.') end @pid_file = if .has_key?(:pid_file) Pathname.new(.fetch(:pid_file)) else Pathname.new(File.join(@path.to_s, 'pids', @name + '.pid')) end @log_file = if .has_key?(:log_file) Pathname.new(.fetch(:log_file)) else Pathname.new(File.join(@path.to_s, 'logs', @name + '.log')) end @pid = fetch_pid @sync_log = .fetch(:sync_log, true) @allow_multiple = .fetch(:allow_multiple, false) @signal = .fetch(:signal, "TERM") @timeout = .fetch(:timeout, 10) @verbosity = .fetch(:verbose, true) @logger = .fetch(:logger, true) validate_callbacks! validate_files_and_paths! end |
Instance Attribute Details
#allow_multiple ⇒ Object
Returns the value of attribute allow_multiple.
23 24 25 |
# File 'lib/pidly/control.rb', line 23 def allow_multiple @allow_multiple end |
#daemon ⇒ Object
Returns the value of attribute daemon.
23 24 25 |
# File 'lib/pidly/control.rb', line 23 def daemon @daemon end |
#error_count ⇒ Object
Returns the value of attribute error_count.
23 24 25 |
# File 'lib/pidly/control.rb', line 23 def error_count @error_count end |
#log_file ⇒ Object
Returns the value of attribute log_file.
23 24 25 |
# File 'lib/pidly/control.rb', line 23 def log_file @log_file end |
#messages ⇒ Object
Returns the value of attribute messages.
23 24 25 |
# File 'lib/pidly/control.rb', line 23 def @messages end |
#name ⇒ Object
Returns the value of attribute name.
23 24 25 |
# File 'lib/pidly/control.rb', line 23 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
23 24 25 |
# File 'lib/pidly/control.rb', line 23 def path @path end |
#pid ⇒ Object
Returns the value of attribute pid.
23 24 25 |
# File 'lib/pidly/control.rb', line 23 def pid @pid end |
#pid_file ⇒ Object
Returns the value of attribute pid_file.
23 24 25 |
# File 'lib/pidly/control.rb', line 23 def pid_file @pid_file end |
#sync_log ⇒ Object
Returns the value of attribute sync_log.
23 24 25 |
# File 'lib/pidly/control.rb', line 23 def sync_log @sync_log end |
#timeout ⇒ Object
Returns the value of attribute timeout.
23 24 25 |
# File 'lib/pidly/control.rb', line 23 def timeout @timeout end |
#verbose ⇒ Object
Returns the value of attribute verbose.
23 24 25 |
# File 'lib/pidly/control.rb', line 23 def verbose @verbose end |
Class Method Details
.spawn(options = {}) ⇒ Control
Spawn
131 132 133 |
# File 'lib/pidly/control.rb', line 131 def self.spawn(={}) @daemon = new() end |
Instance Method Details
#clean! ⇒ Object
Clean
Remove all files created by the daemon.
293 294 295 296 297 |
# File 'lib/pidly/control.rb', line 293 def clean! FileUtils.rm(@log_file) FileUtils.rm(@pid_file) rescue Errno::ENOENT end |
#fetch_class_var(name) ⇒ Symbol, ...
Fetch Class Variable
309 310 311 312 313 314 315 |
# File 'lib/pidly/control.rb', line 309 def fetch_class_var(name) Control.instance_eval do return nil unless class_variable_defined?(:"@@#{name}") class_variable_get(:"@@#{name}") end end |
#kill(remove_pid_file = true) ⇒ Object
Kill
261 262 263 264 265 266 267 268 269 270 |
# File 'lib/pidly/control.rb', line 261 def kill(remove_pid_file=true) if running? log(:info, "Killing \"#{@name}\" (PID: #{@pid})") execute_callback(:kill) Process.kill 9, @pid end FileUtils.rm(@pid_file) if remove_pid_file rescue Errno::ENOENT end |
#restart ⇒ Object
Restart
Restart the daemon
252 253 254 |
# File 'lib/pidly/control.rb', line 252 def restart stop; sleep 1 while running?; start end |
#running? ⇒ true, false
Running?
277 278 279 280 281 282 283 284 285 286 |
# File 'lib/pidly/control.rb', line 277 def running? Process.kill 0, @pid true rescue Errno::ESRCH false rescue Errno::EPERM true rescue false end |
#start ⇒ Object
Start
Validate callbacks and start daemon
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/pidly/control.rb', line 140 def start unless @allow_multiple if running? log(:error, "\"#{@name}\" is already running (PID: #{@pid})") return end end @pid = fork do begin Process.setsid open(@pid_file, 'w') do |f| f << Process.pid @pid = Process.pid end execute_callback(:before_start) Dir.chdir @path.to_s File.umask 0000 if @logger log = File.new(@log_file, "a") log.sync = @sync_log STDIN.reopen "/dev/null" STDOUT.reopen log STDERR.reopen STDOUT end trap("TERM") do stop end execute_callback(:start) rescue RuntimeError => STDERR.puts STDERR.puts .backtrace execute_callback(:error) rescue => STDERR.puts STDERR.puts .backtrace execute_callback(:error) end end rescue => STDERR.puts STDERR.puts .backtrace execute_callback(:error) end |
#status ⇒ String
Status
Return current daemon status and pid
239 240 241 242 243 244 245 |
# File 'lib/pidly/control.rb', line 239 def status if running? log(:info, "\"#{@name}\" is running (PID: #{@pid})") else log(:info, "\"#{@name}\" is NOT running") end end |
#stop ⇒ Object
Stop
Stop daemon and remove pid file
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/pidly/control.rb', line 202 def stop if running? Process.kill(@signal, @pid) FileUtils.rm(@pid_file) execute_callback(:stop) begin Process.wait(@pid) rescue Errno::ECHILD end @timeout.downto(0) do sleep 1 exit unless running? end Process.kill 9, @pid if running? execute_callback(:after_stop) else FileUtils.rm(@pid_file) if File.exists?(@pid_file) log(:info, "\"#{@name}\" PID file not found.") end rescue Errno::ENOENT end |