Class: DrbPool

Inherits:
Object
  • Object
show all
Defined in:
lib/drbman/drb_pool.rb

Overview

Synopsis

A pool of drb objects

Defined Under Namespace

Modules: InUse

Instance Method Summary collapse

Constructor Details

#initialize(hosts, logger) {|self| ... } ⇒ DrbPool

Create the pool of drb objects.

Examples:

Without using a block

pool = DrbPool.new(hosts, logger)
pool.getObject {|obj| obj.do_something}
pool.shutdown

Using a block

DrbPool.new(hosts, logger) do |pool|
  pool.getObject {|obj| obj.do_something}
end

Parameters:

  • the (Array<HostMachine>)

    host_machine instances to use to populate the pool of drb objects

  • the (Logger)

    logger

Yields:

  • (self)


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

Examples:

Usage

pool.get_object {|obj| obj.do_something}

Yields:

  • (DRbObject)

Raises:



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

#shutdownObject

Shut the pool down Only necessary if not using a block with DrbPool.new

Examples:

pool = DrbPool.new(hosts, logger)
pool.getObject {|obj| obj.do_something}
pool.shutdown


64
65
66
67
68
# File 'lib/drbman/drb_pool.rb', line 64

def shutdown
  @objects.each do |obj|
    obj.stop_service
  end
end