Class: DrbPool
- Inherits:
-
Object
- Object
- DrbPool
- Defined in:
- lib/drbman/drb_pool.rb
Overview
Synopsis
A pool of drb objects
Defined Under Namespace
Modules: InUse
Instance Method Summary collapse
-
#get_object {|DRbObject| ... } ⇒ Object
Use an object from the pool.
-
#initialize(hosts, logger) {|self| ... } ⇒ DrbPool
constructor
Create the pool of drb objects.
-
#shutdown ⇒ Object
Shut the pool down Only necessary if not using a block with DrbPool.new.
Constructor Details
#initialize(hosts, logger) {|self| ... } ⇒ DrbPool
Create the pool of drb objects.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/drbman/drb_pool.rb', line 16 def initialize(hosts, logger, &block) @logger = logger @objects = [] threads = [] mutex = Mutex.new @logger.debug { "drb_pool hosts => #{hosts.inspect}"} hosts.each do |host_name, host_machine| threads << Thread.new(host_machine) do |host| if host.alive? obj = get_drb_object(host.machine, host.port) unless obj.nil? mutex.synchronize do @objects << obj end end end end end threads.each {|thrd| thrd.join} unless block.nil? block.call(self) shutdown end end |
Instance Method Details
#get_object {|DRbObject| ... } ⇒ Object
Use an object from the pool
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/drbman/drb_pool.rb', line 47 def get_object(&block) raise EmptyDrbPoolError.new("No drb servers available") if @objects.empty? mutex = Mutex.new while((object = next_object(mutex)).nil?) sleep 0.1 end raise ArgumentError.new('a block is required') if block.nil? block.call(object) object.in_use = false end |
#shutdown ⇒ Object
Shut the pool down Only necessary if not using a block with DrbPool.new
64 65 66 67 68 |
# File 'lib/drbman/drb_pool.rb', line 64 def shutdown @objects.each do |obj| obj.stop_service end end |