[Backgroundrb-devel] Stopping DRb after using BackgrounDRb::MiddleManDRbObject.init
Ezra Zygmuntowicz
ezmobius at gmail.com
Fri Jan 19 19:01:19 EST 2007
On Jan 19, 2007, at 3:37 PM, Robert Bjarnason wrote:
> Hi Ezra,
>
> I would like to control which uri is used to start the DRb
> connection for Rails based load-balancing purposes from multiple
> mongrels connecting to multiple backgroundrbs running on several
> boxes. I don't see how I could BackgrounDRb::MiddleManRailsProxy
> for this purpose. Any ideas?
>
> Thanks,
> Robert
Robert-
Ahh ok I see. Well maybe something like this(untested!)
# start ----
require 'pathname'
BACKGROUNDRB_ROOT = Pathname.new(RAILS_ROOT).realpath.to_s
require 'backgroundrb'
# Set up a MiddleMan for the BackgrounDRb server configured for this
# particular instance of Rails.
class BackgrounDRb::MiddleManProxyBalancer
def self.init(server_pool={})
config = { :config => "#{BACKGROUNDRB_ROOT}/config/
backgroundrb.yml" }
BackgrounDRb::Config.setup(config)
@@proxy_members = {}
server_pool.each {|num, drburi|
@@proxy_members[num] = BackgrounDRb::MiddleManDRbObject.init
(:uri => drburi)
}
return self
end
def self.[](key)
@@proxy_members[key]
end
end
server_pool = {
1 => "druby://localhost:22221",
2 => "druby://localhost:22222",
3 => "druby://10.0.0.123:22221",
4 => "druby://10.0.0.123:22221"
}
MiddleManPool = BackgrounDRb::MiddleManProxyBalancer.init(server_pool)
# end ----
Then you would use the MiddleManPool in your rails apps instead of
the normal rails proxy. You would have to key the middleman you want
my the number in the server_pool. So it would look like this to use:
session[:jobkey] = MiddleManPool[1].new_worker( :class => :someshit )
session[:otherjobkey] = MiddleManPool[2].new_worker( :class
=> :someshit )
worker = MiddleManPool[1].worker(session[:jobkey])
worker.results.to_hash
worker = MiddleManPool[2].worker(session[: otherjobkey])
worker.results.to_hash
This is just a sketch but I think you could take this and run with
it. You would use the above to replace the
BackgrounDRb::MiddleManRailsProxy in the lib dir of the plugin so
that rails would load your new proxy instead of the old connection
stuff.
So this would make sure that every mongrel had a connection to every
server in the server_pool. ANd you woudl just need to always use the
number of which drb server you want from the pool. But it caches the
connection so that each mongrel only gets one connection to every drb
server. No new connection should be made after the thing boots
because the connections are cached.
Make sense? Please give this or something like this a shot and let
us know how it goes for you. I am interested in adding something like
this to the project.
Cheers-
-- Ezra Zygmuntowicz
-- Lead Rails Evangelist
-- ez at engineyard.com
-- Engine Yard, Serious Rails Hosting
-- (866) 518-YARD (9273)
More information about the Backgroundrb-devel
mailing list