Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10264: cap.max_hosts.patch

File cap.max_hosts.patch, 3.9 kB (added by robholland, 10 months ago)

Patch

  • test/configuration/connections_test.rb

    old new  
    217217    @config.current_task = mock_task(:on_error => :continue) 
    218218    @config.expects(:find_servers_for_task).with(@config.current_task, {}).returns(list) 
    219219    Capistrano::SSH.expects(:connect).times(2).raises(Exception).then.returns(:success) 
    220     @config.expects(:failed!).with(server("cap1")) 
    221220    @config.execute_on_servers do |servers| 
    222221      assert_equal %w(cap2), servers.map { |s| s.host } 
    223222    end 
     
    260259      assert_equal %w(cap2), servers.map { |s| s.host } 
    261260    end 
    262261  end 
    263  
     262   
    264263  def test_connect_should_establish_connections_to_all_servers_in_scope 
    265264    assert @config.sessions.empty? 
    266265    @config.current_task = mock_task 
     
    269268    @config.connect! 
    270269    assert_equal %w(cap1 cap2 cap3), @config.sessions.keys.sort.map { |s| s.host } 
    271270  end 
    272  
     271   
     272  def test_execute_on_servers_should_only_run_on_max_hosts_hosts_at_once 
     273    cap1 = server("cap1") 
     274    cap2 = server("cap2") 
     275    connection1 = mock() 
     276    connection2 = mock() 
     277    connection1.expects(:close) 
     278    connection2.expects(:close) 
     279    @config.current_task = mock_task 
     280    @config.expects(:find_servers_for_task).with(@config.current_task, {:max_hosts => 1}).returns([cap1, cap2]) 
     281    Capistrano::SSH.expects(:connect).times(2).returns(connection1).then.returns(connection2) 
     282    block_called = 0 
     283    @config.execute_on_servers(:max_hosts => 1) do |servers| 
     284      block_called += 1 
     285      assert_equal 1, servers.size 
     286    end 
     287    assert_equal 2, block_called 
     288  end 
     289   
    273290  def test_connect_should_honor_once_option 
    274291    assert @config.sessions.empty? 
    275292    @config.current_task = mock_task 
  • lib/capistrano/configuration/connections.rb

    old new  
     1 
     2require 'enumerator' 
    13require 'capistrano/gateway' 
    24require 'capistrano/ssh' 
    35 
     
    120122        servers = [servers.first] if options[:once] 
    121123        logger.trace "servers: #{servers.map { |s| s.host }.inspect}" 
    122124 
    123         # establish connections to those servers, as necessary 
    124         begin 
    125           establish_connections_to(servers) 
    126         rescue ConnectionError => error 
    127           raise error unless task && task.continue_on_error? 
    128           error.hosts.each do |h| 
    129             servers.delete(h) 
    130             failed!(h) 
     125        # establish connections to those servers in groups of max_hosts, as necessary 
     126        max_hosts = options.fetch(:max_hosts, servers.size) 
     127        servers.each_slice(max_hosts) do |servers_slice| 
     128          begin 
     129            establish_connections_to(servers_slice) 
     130          rescue ConnectionError => error 
     131            raise error unless task && task.continue_on_error? 
     132            error.hosts.each do |h| 
     133              servers_slice.delete(h) 
     134              failed!(h) 
     135            end 
    131136          end 
    132         end 
    133137 
    134         begin 
    135           yield servers 
    136         rescue RemoteError => error 
    137           raise error unless task && task.continue_on_error? 
    138           error.hosts.each { |h| failed!(h) } 
     138          begin 
     139            yield servers_slice 
     140          rescue RemoteError => error 
     141            raise error unless task && task.continue_on_error? 
     142            error.hosts.each { |h| failed!(h) } 
     143          end 
     144 
     145          if max_hosts < servers.size 
     146            # Nasty hack? Force the sessions closed so that we don't tie up more 
     147            # than max_hosts outbound connections 
     148            servers_slice.each do |server| 
     149              @sessions[server].close 
     150              @sessions.delete(server) 
     151            end 
     152          end 
    139153        end 
    140154      end 
    141155