# File lib/daemons/application.rb, line 28 28: def initialize(group, add_options = {}, pid = nil) 29: @group = group 30: @options = group.options.dup 31: @options.update(add_options) 32: 33: @dir_mode = @dir = @script = nil 34: 35: @force_kill_waittime = @options[:force_kill_waittime] || 20 36: 37: unless @pid = pid 38: if @options[:no_pidfiles] 39: @pid = PidMem.new 40: elsif dir = pidfile_dir 41: @pid = PidFile.new(dir, @group.app_name, @group.multiple) 42: else 43: @pid = PidMem.new 44: end 45: end 46: end
# File lib/daemons/application.rb, line 48 48: def change_privilege 49: user = options[:user] 50: group = options[:group] 51: CurrentProcess.change_privilege(user, group) if user 52: end
This is a nice little function for debugging purposes: In case a multi-threaded ruby script exits due to an uncaught exception it may be difficult to find out where the exception came from because one cannot catch exceptions that are thrown in threads other than the main thread.
This function searches for all exceptions in memory and outputs them to STDERR (if it is connected) and to a log file in the pid-file directory.
# File lib/daemons/application.rb, line 347 347: def exception_log 348: return unless logfile 349: 350: require 'logger' 351: 352: l_file = Logger.new(logfile) 353: 354: # the code below finds the last exception 355: e = nil 356: 357: ObjectSpace.each_object {|o| 358: if ::Exception === o 359: e = o 360: end 361: } 362: 363: l_file.info "*** below you find the most recent exception thrown, this will be likely (but not certainly) the exception that made the application exit abnormally ***" 364: l_file.error e 365: 366: l_file.info "*** below you find all exception objects found in memory, some of them may have been thrown in your application, others may just be in memory because they are standard exceptions ***" 367: 368: # this code logs every exception found in memory 369: ObjectSpace.each_object {|o| 370: if ::Exception === o 371: l_file.error o 372: end 373: } 374: 375: l_file.close 376: end
# File lib/daemons/application.rb, line 62 62: def logdir 63: logdir = options[:log_dir] 64: unless logdir 65: logdir = options[:dir_mode] == :system ? '/var/log' : pidfile_dir 66: end 67: logdir 68: end
# File lib/daemons/application.rb, line 74 74: def logfile 75: logdir ? File.join(logdir, @group.app_name + '.log') : nil 76: end
# File lib/daemons/application.rb, line 70 70: def output_logfile 71: (options[:log_output] && logdir) ? File.join(logdir, @group.app_name + '.output') : nil 72: end
# File lib/daemons/application.rb, line 58 58: def pidfile_dir 59: Pid.dir(@dir_mode || @group.dir_mode, @dir || @group.dir, @script || @group.script) 60: end
def run
if @group.controller.options[:exec] run_via_exec() else run_via_load() end
end
def run_via_exec
end
def run_via_load
end
# File lib/daemons/application.rb, line 325 325: def reload 326: if @pid.pid == 0 327: zap 328: start 329: else 330: begin 331: Process.kill('HUP', @pid.pid) 332: rescue 333: # ignore 334: end 335: end 336: end
This function implements a (probably too simle) method to detect whether the program with the pid found in the pid-file is still running. It just searches for the pid in the output of ps ax, which is probably not a good idea in some cases. Alternatives would be to use a direct access method the unix process control system.
# File lib/daemons/application.rb, line 467 467: def running? 468: if @pid.exist? 469: return Pid.running?(@pid.pid) 470: end 471: 472: return false 473: end
# File lib/daemons/application.rb, line 54 54: def script 55: @script || @group.script 56: end
# File lib/daemons/application.rb, line 454 454: def show_status 455: running = self.running? 456: 457: puts "#{self.group.app_name}: #{running ? '' : 'not '}running#{(running and @pid.exist?) ? ' [pid ' + @pid.pid.to_s + ']' : ''}#{(@pid.exist? and not running) ? ' (but pid-file exists: ' + @pid.pid.to_s + ')' : ''}" 458: end
# File lib/daemons/application.rb, line 282 282: def start 283: change_privilege 284: @group.create_monitor(@group.applications[0] || self) unless options[:ontop] # we don't monitor applications in the foreground 285: 286: case options[:mode] 287: when :none 288: # this is only used to daemonize the currently running process 289: start_none 290: when :exec 291: start_exec 292: when :load 293: start_load 294: when :proc 295: start_proc 296: else 297: start_load 298: end 299: end
# File lib/daemons/application.rb, line 122 122: def start_exec 123: if options[:backtrace] 124: puts "option :backtrace is not supported with :mode => :exec, ignoring" 125: end 126: 127: unless options[:ontop] 128: Daemonize.daemonize(output_logfile, @group.app_name) 129: else 130: Daemonize.simulate(output_logfile) 131: end 132: 133: # note that we cannot remove the pid file if we run in :ontop mode (i.e. 'ruby ctrl_exec.rb run') 134: @pid.pid = Process.pid 135: 136: ENV['DAEMONS_ARGV'] = @controller_argv.join(' ') 137: # haven't tested yet if this is really passed to the exec'd process... 138: 139: started() 140: Kernel.exec(script(), *(@app_argv || [])) 141: end
# File lib/daemons/application.rb, line 143 143: def start_load 144: unless options[:ontop] 145: Daemonize.daemonize(output_logfile, @group.app_name) 146: else 147: Daemonize.simulate(output_logfile) 148: end 149: 150: @pid.pid = Process.pid 151: 152: 153: # We need this to remove the pid-file if the applications exits by itself. 154: # Note that <tt>at_exit</tt> will only be run if the applications exits by calling 155: # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt> 156: # in your application! 157: # 158: at_exit { 159: begin; @pid.cleanup; rescue ::Exception; end 160: 161: # If the option <tt>:backtrace</tt> is used and the application did exit by itself 162: # create a exception log. 163: if options[:backtrace] and not options[:ontop] and not $daemons_sigterm 164: begin; exception_log(); rescue ::Exception; end 165: end 166: 167: } 168: 169: # This part is needed to remove the pid-file if the application is killed by 170: # daemons or manually by the user. 171: # Note that the applications is not supposed to overwrite the signal handler for 172: # 'TERM'. 173: # 174: $daemons_stop_proc = options[:stop_proc] 175: trap(SIGNAL) { 176: begin 177: if $daemons_stop_proc 178: $daemons_stop_proc.call 179: end 180: rescue ::Exception 181: end 182: 183: begin; @pid.cleanup; rescue ::Exception; end 184: $daemons_sigterm = true 185: 186: if options[:hard_exit] 187: exit! 188: else 189: exit 190: end 191: } 192: 193: # Now we really start the script... 194: $DAEMONS_ARGV = @controller_argv 195: ENV['DAEMONS_ARGV'] = @controller_argv.join(' ') 196: 197: ARGV.clear 198: ARGV.concat @app_argv if @app_argv 199: 200: started() 201: # TODO: begin - rescue - end around this and exception logging 202: load script() 203: end
this function is only used to daemonize the currently running process (Daemons.daemonize)
# File lib/daemons/application.rb, line 79 79: def start_none 80: unless options[:ontop] 81: Daemonize.daemonize(output_logfile, @group.app_name) 82: else 83: Daemonize.simulate(output_logfile) 84: end 85: 86: @pid.pid = Process.pid 87: 88: 89: # We need this to remove the pid-file if the applications exits by itself. 90: # Note that <tt>at_text</tt> will only be run if the applications exits by calling 91: # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt> 92: # in your application! 93: # 94: at_exit { 95: begin; @pid.cleanup; rescue ::Exception; end 96: 97: # If the option <tt>:backtrace</tt> is used and the application did exit by itself 98: # create a exception log. 99: if options[:backtrace] and not options[:ontop] and not $daemons_sigterm 100: begin; exception_log(); rescue ::Exception; end 101: end 102: 103: } 104: 105: # This part is needed to remove the pid-file if the application is killed by 106: # daemons or manually by the user. 107: # Note that the applications is not supposed to overwrite the signal handler for 108: # 'TERM'. 109: # 110: trap(SIGNAL) { 111: begin; @pid.cleanup; rescue ::Exception; end 112: $daemons_sigterm = true 113: 114: if options[:hard_exit] 115: exit! 116: else 117: exit 118: end 119: } 120: end
# File lib/daemons/application.rb, line 205 205: def start_proc 206: return unless p = options[:proc] 207: 208: myproc = proc do 209: 210: @pid.pid = Process.pid 211: 212: # We need this to remove the pid-file if the applications exits by itself. 213: # Note that <tt>at_text</tt> will only be run if the applications exits by calling 214: # <tt>exit</tt>, and not if it calls <tt>exit!</tt> (so please don't call <tt>exit!</tt> 215: # in your application! 216: # 217: at_exit { 218: begin; @pid.cleanup; rescue ::Exception; end 219: 220: # If the option <tt>:backtrace</tt> is used and the application did exit by itself 221: # create a exception log. 222: if options[:backtrace] and not options[:ontop] and not $daemons_sigterm 223: begin; exception_log(); rescue ::Exception; end 224: end 225: 226: } 227: 228: # This part is needed to remove the pid-file if the application is killed by 229: # daemons or manually by the user. 230: # Note that the applications is not supposed to overwrite the signal handler for 231: # 'TERM'. 232: # 233: $daemons_stop_proc = options[:stop_proc] 234: trap(SIGNAL) { 235: begin 236: if $daemons_stop_proc 237: $daemons_stop_proc.call 238: end 239: rescue ::Exception 240: end 241: 242: begin; @pid.cleanup; rescue ::Exception; end 243: $daemons_sigterm = true 244: 245: if options[:hard_exit] 246: exit! 247: else 248: exit 249: end 250: } 251: 252: started() 253: 254: p.call() 255: end 256: 257: unless options[:ontop] 258: Daemonize.call_as_daemon(myproc, output_logfile, @group.app_name) 259: 260: else 261: Daemonize.simulate(output_logfile) 262: 263: myproc.call 264: 265: # why did we use this?? 266: # Thread.new(&options[:proc]) 267: 268: # why did we use the code below?? 269: # unless pid = Process.fork 270: # @pid.pid = pid 271: # Daemonize.simulate(logfile) 272: # options[:proc].call 273: # exit 274: # else 275: # Process.detach(@pid.pid) 276: # end 277: end 278: 279: end
# File lib/daemons/application.rb, line 301 301: def started 302: if pid = @pid.pid 303: puts "#{self.group.app_name}: process with pid #{pid} started." 304: STDOUT.flush 305: end 306: end
# File lib/daemons/application.rb, line 379 379: def stop(no_wait = false) 380: if not running? 381: self.zap 382: return 383: end 384: 385: pid = @pid.pid 386: 387: # Catch errors when trying to kill a process that doesn't 388: # exist. This happens when the process quits and hasn't been 389: # restarted by the monitor yet. By catching the error, we allow the 390: # pid file clean-up to occur. 391: begin 392: Process.kill(SIGNAL, pid) 393: rescue Errno::ESRCH => e 394: puts "#{e} #{pid}" 395: puts "deleting pid-file." 396: end 397: 398: if not no_wait 399: if @force_kill_waittime > 0 400: puts "#{self.group.app_name}: trying to stop process with pid #{pid}..." 401: STDOUT.flush 402: 403: begin 404: Timeout::timeout(@force_kill_waittime) { 405: while Pid.running?(pid) 406: sleep(0.2) 407: end 408: } 409: rescue Timeout::Error 410: puts "#{self.group.app_name}: process with pid #{pid} won't stop, we forcefully kill it..." 411: STDOUT.flush 412: 413: begin 414: Process.kill('KILL', pid) 415: rescue Errno::ESRCH 416: end 417: 418: begin 419: Timeout::timeout(20) { 420: while Pid.running?(pid) 421: sleep(1) 422: end 423: } 424: rescue Timeout::Error 425: puts "#{self.group.app_name}: unable to forcefully kill process with pid #{pid}." 426: STDOUT.flush 427: end 428: end 429: end 430: 431: 432: end 433: 434: sleep(0.1) 435: unless Pid.running?(pid) 436: # We try to remove the pid-files by ourselves, in case the application 437: # didn't clean it up. 438: begin; @pid.cleanup; rescue ::Exception; end 439: 440: puts "#{self.group.app_name}: process with pid #{pid} successfully stopped." 441: STDOUT.flush 442: end 443: 444: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.