class SSHKit::Runner::SafeParallel

Attributes

group_size[W]

Public Class Methods

new(hosts, options = nil, &block) click to toggle source
Calls superclass method SSHKit::Runner::SafeSequential::new
# File lib/gaptool_client/runner.rb, line 41
def initialize(hosts, options = nil, &block)
  super(hosts, options, &block)
  @failed = Queue.new
  @succeeded = Queue.new
end

Public Instance Methods

execute() click to toggle source
# File lib/gaptool_client/runner.rb, line 47
def execute
  hosts.each_slice(group_size).each do |slice|
    threads = []
    slice.each do |host|
      threads << Thread.new(host) do |h|
        begin
          backend(h, &block).run
          succeeded << host
        rescue => e
          failed << { host: host, error: e }
        end
      end
    end
    threads.map(&:join)
    return convert_results if failed.length > 0 && @on_errors == :exit
    sleep wait_interval
  end
  convert_results
end

Private Instance Methods

convert_results() click to toggle source
# File lib/gaptool_client/runner.rb, line 73
def convert_results
  @failed = queue_to_array(failed)
  @succeeded = queue_to_array(succeeded)
  return true if failed.length == 0
end
group_size() click to toggle source
# File lib/gaptool_client/runner.rb, line 69
def group_size
  @group_size || options[:limit] || 2
end
queue_to_array(queue) click to toggle source
# File lib/gaptool_client/runner.rb, line 79
def queue_to_array(queue)
  res = []
  begin
    loop do
      obj = queue.pop(true)
      break unless obj
      res << obj
    end
  rescue ThreadError
    return res
  end
  res
end