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 |
# File 'lib/drbman/drb_pool.rb', line 16 def initialize(hosts, logger, &block) @logger = logger @objects = [] threads = [] mutex = Mutex.new hosts.each do |host_name, host_machine| threads << Thread.new(host_machine) do |host| obj = get_drb_object(host.machine, host.port) mutex.synchronize do @objects << obj 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
42 43 44 45 46 47 48 49 50 |
# File 'lib/drbman/drb_pool.rb', line 42 def get_object(&block) 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
58 59 60 61 62 |
# File 'lib/drbman/drb_pool.rb', line 58 def shutdown @objects.each do |obj| obj.stop_service end end |