Class: BackgrounDRb::StartStop
- Inherits:
-
Object
- Object
- BackgrounDRb::StartStop
- Defined in:
- lib/backgroundrb/bdrb_start_stop.rb
Instance Method Summary collapse
-
#dead? ⇒ Boolean
pidfile exists but process isn’t running.
- #kill_process(pid_file) ⇒ Object
- #pid ⇒ Object
- #pidfile_exists? ⇒ Boolean
- #process_running? ⇒ Boolean
-
#redirect_io(logfile_name) ⇒ Object
Free file descriptors and point them somewhere sensible STDOUT/STDERR should go to a logfile.
- #remove_pidfiles ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
- #start_bdrb ⇒ Object
-
#status ⇒ Object
returns the correct lsb code for the status: 0 program is running or service is OK 1 program is dead and /var/run pid file exists 3 program is not running.
- #stop ⇒ Object
Instance Method Details
#dead? ⇒ Boolean
pidfile exists but process isn’t running
67 |
# File 'lib/backgroundrb/bdrb_start_stop.rb', line 67 def dead?;status == 1;end |
#kill_process(pid_file) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/backgroundrb/bdrb_start_stop.rb', line 116 def kill_process(pid_file) pid = File.open(pid_file, "r") { |pid_handle| pid_handle.gets.strip.to_i } pgid = Process.getpgid(pid) Process.kill('-TERM', pgid) File.delete(pid_file) if File.exists?(pid_file) puts "Stopped BackgrounDRb worker with pid #{pid}" end |
#pid ⇒ Object
69 70 71 |
# File 'lib/backgroundrb/bdrb_start_stop.rb', line 69 def pid File.read(PID_FILE).strip.to_i if pidfile_exists? end |
#pidfile_exists? ⇒ Boolean
53 |
# File 'lib/backgroundrb/bdrb_start_stop.rb', line 53 def pidfile_exists?; File.exists?(PID_FILE); end |
#process_running? ⇒ Boolean
55 56 57 58 59 60 61 62 |
# File 'lib/backgroundrb/bdrb_start_stop.rb', line 55 def process_running? begin Process.kill(0,self.pid) true rescue Errno::ESRCH false end end |
#redirect_io(logfile_name) ⇒ Object
Free file descriptors and point them somewhere sensible STDOUT/STDERR should go to a logfile
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/backgroundrb/bdrb_start_stop.rb', line 128 def redirect_io(logfile_name) begin; STDIN.reopen "/dev/null"; rescue ::Exception; end if logfile_name begin STDOUT.reopen logfile_name, "a" STDOUT.sync = true rescue ::Exception begin; STDOUT.reopen "/dev/null"; rescue ::Exception; end end else begin; STDOUT.reopen "/dev/null"; rescue ::Exception; end end begin; STDERR.reopen STDOUT; rescue ::Exception; end STDERR.sync = true end |
#remove_pidfiles ⇒ Object
73 74 75 76 |
# File 'lib/backgroundrb/bdrb_start_stop.rb', line 73 def remove_pidfiles require 'fileutils' FileUtils.rm_r(Dir["#{RAILS_HOME}/tmp/pids/backgroundrb_*.pid"]) end |
#running? ⇒ Boolean
64 |
# File 'lib/backgroundrb/bdrb_start_stop.rb', line 64 def running?;status == 0;end |
#start ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/backgroundrb/bdrb_start_stop.rb', line 5 def start if running? # starting an already running process is considered a success puts "BackgrounDRb Already Running" exit(0) elsif dead? # dead, but pid exists remove_pidfiles end # status == 3, not running. $stdout.sync = true print "Starting BackgrounDRb.... " start_bdrb # TODO: find a way to report success/failure puts "Done!" exit(0) end |
#start_bdrb ⇒ Object
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/backgroundrb/bdrb_start_stop.rb', line 78 def start_bdrb require "rubygems" require "yaml" require "erb" require "logger" require "packet" require "optparse" require "bdrb_config" require RAILS_HOME + "/config/boot" require "active_support" BackgrounDRb::Config. ARGV require RAILS_HOME + "/config/environment" require "bdrb_job_queue" require "backgroundrb_server" #proper way to daemonize - double fork to ensure parent can have no interest in the grandchild if fork # Parent exits, child continues. sleep(1) else Process.setsid # Become session leader. op = File.open(PID_FILE, "w") op.write(Process.pid().to_s) op.close if BDRB_CONFIG[:backgroundrb][:log].nil? or BDRB_CONFIG[:backgroundrb][:log] != 'foreground' redirect_io(SERVER_LOGGER) end $0 = "backgroundrb master" BackgrounDRb::MasterProxy.new() end #File.open(PID_FILE, "w") {|pidfile| pidfile.write(main_pid)} end |
#status ⇒ Object
returns the correct lsb code for the status: 0 program is running or service is OK 1 program is dead and /var/run pid file exists 3 program is not running
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/backgroundrb/bdrb_start_stop.rb', line 39 def status @status = begin if pidfile_exists? and process_running? 0 elsif pidfile_exists? # but not process_running 1 else 3 end end return @status end |
#stop ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/backgroundrb/bdrb_start_stop.rb', line 22 def stop pid_files = Dir["#{RAILS_HOME}/tmp/pids/backgroundrb_*.pid"] puts "BackgrounDRb Not Running" if pid_files.empty? pid_files.each do |x| begin kill_process(x) rescue Errno::ESRCH # stopping an already stopped process is considered a success (exit status 0) end end remove_pidfiles end |