Class: Weave::ConnectionPool
- Inherits:
-
Object
- Object
- Weave::ConnectionPool
- Defined in:
- lib/weave.rb
Overview
A pool of SSH connections. Operations over the pool may be performed in serial or in parallel.
Instance Method Summary collapse
-
#disconnect! ⇒ Object
Disconnect all open connections.
-
#execute(options = {}, &block) ⇒ Object
Run a command over the connection pool.
-
#execute_with(host_list, options = {}, &block) ⇒ Object
This is the same as #execute, except that host_list overrides the list of connections with which this ConnectionPool was initialized.
-
#initialize(host_list = []) ⇒ ConnectionPool
constructor
use #execute_with (instead of #execute) to specify the whole list of hosts each time.
Constructor Details
#initialize(host_list = []) ⇒ ConnectionPool
use #execute_with (instead of #execute) to specify the whole list of hosts each time.
63 64 65 66 |
# File 'lib/weave.rb', line 63 def initialize(host_list = []) @hosts = host_list @connections = host_list.reduce({}) { |pool, host| pool.merge(host => LazyConnection.new(host)) } end |
Instance Method Details
#disconnect! ⇒ Object
Disconnect all open connections.
105 |
# File 'lib/weave.rb', line 105 def disconnect!() @connections.each_value(&:disconnect) end |
#execute(options = {}, &block) ⇒ Object
Run a command over the connection pool. The block is evaluated in the context of LazyConnection.
77 78 79 |
# File 'lib/weave.rb', line 77 def execute( = {}, &block) execute_with(@hosts, , &block) end |
#execute_with(host_list, options = {}, &block) ⇒ Object
This is the same as #execute, except that host_list overrides the list of connections with which this ConnectionPool was initialized. Any hosts in here that weren't already in the pool will be added.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/weave.rb', line 83 def execute_with(host_list, = {}, &block) host_list.each { |host| @connections[host] ||= LazyConnection.new(host) } args = [:args] || [] num_threads = [:num_threads] || DEFAULT_THREAD_POOL_SIZE if [:serial] host_list.each { |host| @connections[host].self_eval args, &block } elsif [:batch_by] num_threads = [:batch_by] if num_threads == :unlimited host_list.each_slice([:batch_by]) do |batch| Weave.with_thread_pool(batch, num_threads) do |host, mutex| @connections[host].self_eval args, mutex, &block end end else num_threads = host_list.size if num_threads == :unlimited Weave.with_thread_pool(host_list, num_threads) do |host, mutex| @connections[host].self_eval args, mutex, &block end end end |