Skip to content

Commit

Permalink
made the startup script more lsb compliant: added status, restart, ma…
Browse files Browse the repository at this point in the history
…de start clean the old pidfile in case of dead/crased process, and made stop exit 0 if already stopped. Also, moved requires and environment loads only to the correct part of start, so all other actions (ex. stop, status) happen immediately without loading all of rails.

Signed-off-by: Hemant Kumar <gethemant@gmail.com>
  • Loading branch information
Woody Peterson authored and gnufied committed Apr 14, 2009
1 parent 058245e commit eae8d17
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 56 deletions.
2 changes: 2 additions & 0 deletions lib/backgroundrb/bdrb_config.rb
@@ -1,3 +1,5 @@
require 'yaml'
require 'erb'
module BackgrounDRb
class Config
def self.parse_cmd_options(argv)
Expand Down
127 changes: 90 additions & 37 deletions lib/backgroundrb/bdrb_start_stop.rb
@@ -1,54 +1,112 @@
# see http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
# for LSB-compliancy info
module BackgrounDRb
class StartStop
def kill_process arg_pid_file
pid = nil
pid = File.open(arg_pid_file, "r") { |pid_handle| pid_handle.gets.strip.chomp.to_i }
pgid = Process.getpgid(pid)
puts "Stopping BackgrounDRb with pid #{pid}...."
Process.kill('-TERM', pgid)
File.delete(arg_pid_file) if File.exists?(arg_pid_file)
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_pidfile
end

# status == 3, not running.
STDOUT.sync = true
print("Starting BackgrounDRb .... ")
start_bdrb
puts "Success!"
end


def running?; File.exists?(PID_FILE); end

def really_running? pid

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
end

# 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
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

def pidfile_exists?; File.exists?(PID_FILE); end

def process_running?
begin
Process.kill(0,pid)
Process.kill(0,self.pid)
true
rescue Errno::ESRCH
puts "pid file exists but process doesn't seem to be running restarting now"
false
end
end

def try_restart
pid = nil
pid = File.open(PID_FILE, "r") { |pid_handle| pid_handle.gets.strip.chomp.to_i }
if really_running? pid
puts "pid file already exists, exiting..."
exit(-1)
end

def running?;status == 0;end
# pidfile exists but process isn't running

def dead?;status == 1;end

def pid
File.read(PID_FILE).strip.to_i if pidfile_exists?
end

def remove_pidfile
File.delete(PID_FILE)
end

def start
if fork
sleep(5)
exit(0)
else
try_restart if running?
puts "Starting BackgrounDRb .... "
op = File.open(PID_FILE, "w")
op.write(Process.pid().to_s)
op.close
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.parse_cmd_options ARGV

require RAILS_HOME + "/config/environment"
require "bdrb_job_queue"
require "backgroundrb_server"
main_pid = fork do
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

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


# Free file descriptors and
# point them somewhere sensible
# STDOUT/STDERR should go to a logfile
Expand All @@ -70,10 +128,5 @@ def redirect_io(logfile_name)
STDERR.sync = true
end


def stop
pid_files = Dir["#{RAILS_HOME}/tmp/pids/backgroundrb_*.pid"]
pid_files.each { |x| kill_process(x) }
end
end
end
34 changes: 15 additions & 19 deletions script/backgroundrb
Expand Up @@ -8,32 +8,28 @@ WORKER_LOAD_ENV = File.join(RAILS_HOME,"script","load_worker_env")
["server","server/lib","lib","lib/backgroundrb"].each { |x| $LOAD_PATH.unshift(BDRB_HOME + "/#{x}")}
$LOAD_PATH.unshift(WORKER_ROOT)

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.parse_cmd_options ARGV
require 'rubygems'
require 'bdrb_config'
BDRB_CONFIG = BackgrounDRb::Config.read_config("#{RAILS_HOME}/config/backgroundrb.yml")

require RAILS_HOME + "/config/environment"
require "bdrb_job_queue"
require "backgroundrb_server"

PID_FILE = "#{RAILS_HOME}/tmp/pids/backgroundrb_#{BDRB_CONFIG[:backgroundrb][:port]}.pid"
SERVER_LOGGER = "#{RAILS_HOME}/log/backgroundrb_debug_#{BDRB_CONFIG[:backgroundrb][:port]}.log"

require 'bdrb_start_stop'
daemon = BackgrounDRb::StartStop.new

case ARGV[0]
when 'start'; daemon.start
when 'stop'; daemon.stop()
else; BackgrounDRb::MasterProxy.new()
when 'stop'; daemon.stop
when 'restart'; daemon.stop;daemon.start
when 'status'
if daemon.running?
puts "BackgrounDRb Running"
exit
else
puts "BackgrounDRb Not Running"
exit!(daemon.status)
end
else
BackgrounDRb::MasterProxy.new()
end

0 comments on commit eae8d17

Please sign in to comment.