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

Ticket #10264: cap.patch

File cap.patch, 6.3 kB (added by robholland, 8 months ago)
  • vendor/gems/capistrano-2.1.0/lib/capistrano/configuration/connections.rb

    old new  
     1 
     2require 'enumerator' 
    13require 'capistrano/gateway' 
    24require 'capistrano/ssh' 
    35 
     
    9599        end 
    96100      end 
    97101 
     102      # Destroys sessions for each server in the list. 
     103      def teardown_connections_to(servers) 
     104        servers.each do |server| 
     105           @sessions[server].close 
     106           @sessions.delete(server) 
     107         end 
     108      end 
     109 
    98110      # Determines the set of servers within the current task's scope and 
    99111      # establishes connections to them, and then yields that list of 
    100112      # servers. 
     
    120132        servers = [servers.first] if options[:once] 
    121133        logger.trace "servers: #{servers.map { |s| s.host }.inspect}" 
    122134 
    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) 
    131           end 
     135        # establish connections to those servers in groups of max_hosts, as necessary 
     136        if options.has_key?(:max_hosts) 
     137          max_hosts = options[:max_hosts].to_i 
     138        elsif task 
     139          max_hosts = task.max_hosts 
    132140        end 
    133141 
    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) } 
     142        servers.each_slice(max_hosts || servers.size) do |servers_slice| 
     143          logger.trace "server slice: #{servers_slice.map { |s| s.host }.inspect}" if max_hosts 
     144 
     145          begin 
     146            establish_connections_to(servers_slice) 
     147          rescue ConnectionError => error 
     148            raise error unless task && task.continue_on_error? 
     149            error.hosts.each do |h| 
     150              servers_slice.delete(h) 
     151              failed!(h) 
     152            end 
     153          end 
     154 
     155          begin 
     156            yield servers_slice 
     157          rescue RemoteError => error 
     158            raise error unless task && task.continue_on_error? 
     159            error.hosts.each { |h| failed!(h) } 
     160          end 
     161 
     162          teardown_connections_to(servers_slice) if max_hosts 
    139163        end 
    140164      end 
    141165 
  • vendor/gems/capistrano-2.1.0/lib/capistrano/task_definition.rb

    old new  
    33module Capistrano 
    44  # Represents the definition of a single task. 
    55  class TaskDefinition 
    6     attr_reader :name, :namespace, :options, :body, :desc, :on_error 
     6    attr_reader :name, :namespace, :options, :body, :desc, :on_error, :max_hosts 
    77 
    88    def initialize(name, namespace, options={}, &block) 
    99      @name, @namespace, @options = name, namespace, options 
    1010      @desc = @options.delete(:desc) 
    1111      @on_error = options.delete(:on_error) 
     12      @max_hosts = options.delete(:max_hosts).to_i 
    1213      @body = block or raise ArgumentError, "a task requires a block" 
    1314      @servers = nil 
    1415    end 
  • vendor/gems/capistrano-2.1.0/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_tasks_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(:max_hosts => 1) 
     280    @config.expects(:find_servers_for_task).with(@config.current_task, {}).returns([cap1, cap2]) 
     281    Capistrano::SSH.expects(:connect).times(2).returns(connection1).then.returns(connection2) 
     282    block_called = 0 
     283    @config.execute_on_servers do |servers| 
     284      block_called += 1 
     285      assert_equal 1, servers.size 
     286    end 
     287    assert_equal 2, block_called 
     288  end 
     289   
     290  def test_execute_on_servers_should_only_run_on_max_hosts_hosts_at_once 
     291    cap1 = server("cap1") 
     292    cap2 = server("cap2") 
     293    connection1 = mock() 
     294    connection2 = mock() 
     295    connection1.expects(:close) 
     296    connection2.expects(:close) 
     297    @config.current_task = mock_task(:max_hosts => 1) 
     298    @config.expects(:find_servers_for_task).with(@config.current_task, {}).returns([cap1, cap2]) 
     299    Capistrano::SSH.expects(:connect).times(2).returns(connection1).then.returns(connection2) 
     300    block_called = 0 
     301    @config.execute_on_servers do |servers| 
     302      block_called += 1 
     303      assert_equal 1, servers.size 
     304    end 
     305    assert_equal 2, block_called 
     306  end 
     307   
    273308  def test_connect_should_honor_once_option 
    274309    assert @config.sessions.empty? 
    275310    @config.current_task = mock_task 
     
    283318 
    284319    def mock_task(options={}) 
    285320      continue_on_error = options[:on_error] == :continue 
    286       stub("task", :fully_qualified_name => "name", :options => options, :continue_on_error? => continue_on_error) 
     321      stub("task", 
     322        :fully_qualified_name => "name", 
     323        :options => options, 
     324        :continue_on_error? => continue_on_error, 
     325        :max_hosts => options[:max_hosts] 
     326      ) 
    287327    end 
    288328end