Skip to content

Commit

Permalink
Merge commit 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
gnufied committed Apr 14, 2009
2 parents 741e775 + 2410826 commit 28c4e19
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 57 deletions.
2 changes: 2 additions & 0 deletions lib/backgroundrb/bdrb_config.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'yaml'
require 'erb'
module BackgrounDRb
class Config
def self.parse_cmd_options(argv)
Expand Down
128 changes: 91 additions & 37 deletions lib/backgroundrb/bdrb_start_stop.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,113 @@
# 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_pidfiles
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_pidfiles
require 'fileutils'
FileUtils.rm_rf("#{RAILS_HOME}/tmp/pids/backgroundrb_*.pid")
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 +129,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
2 changes: 1 addition & 1 deletion lib/backgroundrb/rails_worker_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def method_missing(method_id,*args)
arg,job_key,host_info,scheduled_at = arguments && arguments.values_at(:arg,:job_key,:host,:scheduled_at)

# allow both arg and args
arg ||= arguments[:args]
arg ||= arguments && arguments[:args]

new_schedule = (scheduled_at && scheduled_at.respond_to?(:utc)) ? scheduled_at.utc : Time.now.utc

Expand Down
34 changes: 15 additions & 19 deletions script/backgroundrb
Original file line number Diff line number Diff line change
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

8 changes: 8 additions & 0 deletions test/client/test_worker_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,12 @@

specify "should switch connections if invoke fails on chosen one" do
end

specify "Should allow method with empty params to work" do
actual_conn = mock()
actual_conn.expects(:server_info).returns("localhost:11211")
actual_conn.expects(:ask_work).with(:worker => :hello_worker,:worker_method => 'foobar').returns(nil)
@cluster_conn.expects(:choose_server).returns(actual_conn)
@worker_proxy.async_foobar
end
end

0 comments on commit 28c4e19

Please sign in to comment.