Skip to content

Commit

Permalink
improve start stop managment
Browse files Browse the repository at this point in the history
  • Loading branch information
gnufied committed Oct 3, 2008
1 parent 3903647 commit d211e3e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 40 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -1,5 +1,9 @@
2008-09-07 hemant kumar <hemant@shire.com>

* Fix environment loading issues.

* Patch by Kieran P for disabling persistent job queue and its polling.

* Commit patch by P Baker, related to scheduling a persistent task at specified time. For example:
MiddleMan(:hello_worker).enq_some_task(:arg => "hello_world",
:job_key => "boy",:scheduled_at => (Time.now + 1.hour))
Expand Down
10 changes: 8 additions & 2 deletions Rakefile
Expand Up @@ -4,6 +4,7 @@ require 'rake/testtask'
require 'rake/rdoctask'
require 'spec/rake/spectask'
require 'rake/contrib/sshpublisher'
require "darkfish-rdoc"

desc 'Default: run unit tests.'
task :default => :test
Expand Down Expand Up @@ -31,14 +32,19 @@ desc 'Generate documentation for the backgroundrb plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'doc/output/manual'
rdoc.title = 'Backgroundrb'
rdoc.options << '--line-numbers' << '--inline-source'
#rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('LICENSE')
rdoc.rdoc_files.include('lib/*.rb')
rdoc.rdoc_files.include('lib/backgroundrb/*.rb')
rdoc.rdoc_files.include('server/*.rb')
rdoc.rdoc_files.include('server/lib/*.rb')
rdoc.template = 'jamis'
#rdoc.template = 'jamis'
rdoc.options += [
'-SHN',
'-f', 'darkfish', # This is the important bit
]

end

module Rake
Expand Down
2 changes: 1 addition & 1 deletion lib/backgroundrb.rb
Expand Up @@ -16,7 +16,7 @@
require "backgroundrb/rails_worker_proxy"
require "backgroundrb/bdrb_connection"
require "backgroundrb/bdrb_cluster_connection"

require "backgroundrb/bdrb_start_stop"
MiddleMan = BackgrounDRb::ClusterConnection.new


41 changes: 41 additions & 0 deletions lib/backgroundrb/bdrb_start_stop.rb
@@ -0,0 +1,41 @@
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)
puts "Success!"
end
def running?; File.exists?(PID_FILE); end

def start
if fork
sleep(5)
exit(0)
else
if running?
puts "pid file already exists, exiting..."
exit(-1)
end
puts "Starting BackgrounDRb .... "
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'
log_file = File.open(SERVER_LOGGER,"w+")
[STDIN, STDOUT, STDERR].each {|desc| desc.reopen(log_file)}
end

BackgrounDRb::MasterProxy.new()
end
end

def stop
pid_files = Dir["#{RAILS_HOME}/tmp/pids/backgroundrb_*.pid"]
pid_files.each { |x| kill_process(x) }
end
end
end
22 changes: 22 additions & 0 deletions lib/backgroundrb/rails_worker_proxy.rb
@@ -1,7 +1,9 @@
module BackgrounDRb
# A Worker proxy, which uses +method_missing+ for delegating method calls to the workers
class RailsWorkerProxy
attr_accessor :worker_name, :worker_method, :data, :worker_key,:middle_man

# create new worker proxy
def initialize(p_worker_name,p_worker_key = nil,p_middle_man = nil)
@worker_name = p_worker_name
@middle_man = p_middle_man
Expand Down Expand Up @@ -40,14 +42,17 @@ def method_missing(method_id,*args)
end
end

# enqueue tasks to the worker pool
def enqueue_task options = {}
BdrbJobQueue.insert_job(options)
end

# remove tasks from the worker pool
def dequeue_task options = {}
BdrbJobQueue.remove_job(options)
end

# invoke method on worker
def run_method host_info,method_name,worker_options = {}
result = []
connection = choose_connection(host_info)
Expand Down Expand Up @@ -75,11 +80,14 @@ def run_method host_info,method_name,worker_options = {}
return_result(result)
end

# choose a backgroundrb server connection and invoke worker method on it.
def invoke_on_connection connection,method_name,options = {}
raise NoServerAvailable.new("No BackgrounDRb is found running") unless connection
connection.send(method_name,options)
end

# get results back from the cache. Cache can be in-memory worker cache or memcache
# based cache
def ask_result job_key
options = compact(:worker => worker_name,:worker_key => worker_key,:job_key => job_key)
if BDRB_CONFIG[:backgroundrb][:result_storage] == 'memcache'
Expand All @@ -90,33 +98,46 @@ def ask_result job_key
end
end

# return runtime information about worker
def worker_info
t_connections = middle_man.backend_connections
result = t_connections.map { |conn| conn.worker_info(compact(:worker => worker_name,:worker_key => worker_key)) }
return_result(result)
end

# generate worker key
def gen_key options
key = [options[:worker],options[:worker_key],options[:job_key]].compact.join('_')
key
end

# return result from memcache
def return_result_from_memcache options = {}
middle_man.cache[gen_key(options)]
end

# reset result within memcache for given key
def reset_memcache_result(job_key,value)
options = compact(:worker => worker_name,:worker_key => worker_key,:job_key => job_key)
key = gen_key(options)
middle_man.cache[key] = value
value
end

def return_result result
result = Array(result)
result.size <= 1 ? result[0] : result
end

# delete a worker
def delete
middle_man.backend_connections.each do |connection|
connection.delete_worker(compact(:worker => worker_name, :worker_key => worker_key))
end
return worker_key
end

# choose a worker
def choose_connection host_info
case host_info
when :all; middle_man.backend_connections
Expand All @@ -126,6 +147,7 @@ def choose_connection host_info
end
end

# helper method to compact a hash and for getting rid of nil parameters
def compact(options = { })
options.delete_if { |key,value| value.nil? }
options
Expand Down
43 changes: 6 additions & 37 deletions script/backgroundrb
Expand Up @@ -26,45 +26,14 @@ require RAILS_HOME + "/config/environment"
require "bdrb_job_queue"
require "backgroundrb_server"

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

case ARGV[0]
when 'start'
if fork
sleep(5)
exit
else
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'
log_file = File.open(SERVER_LOGGER,"w+")
[STDIN, STDOUT, STDERR].each {|desc| desc.reopen(log_file)}
end
daemon = BackgrounDRb::StartStop.new

BackgrounDRb::MasterProxy.new()
end
when 'stop'
def kill_process arg_pid_file
pid = nil
File.open(arg_pid_file, "r") { |pid_handle| pid = pid_handle.gets.strip.chomp.to_i }
begin
pgid = Process.getpgid(pid)
Process.kill('TERM', pid)
Process.kill('-TERM', pgid)
Process.kill('KILL', pid)
rescue Errno::ESRCH => e
puts "Deleting pid file"
rescue
puts $!
ensure
File.delete(arg_pid_file) if File.exists?(arg_pid_file)
end
end
pid_files = Dir["#{RAILS_HOME}/tmp/pids/backgroundrb_*.pid"]
pid_files.each { |x| kill_process(x) }
else
BackgrounDRb::MasterProxy.new()
case ARGV[0]
when 'start'; daemon.start
when 'stop'; daemon.stop()
else; BackgrounDRb::MasterProxy.new()
end

0 comments on commit d211e3e

Please sign in to comment.