Class: Net::SSH::Multi::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/monkey_patches/net-ssh-multi.rb

Instance Method Summary collapse

Instance Method Details

#next_session(server, force = false) ⇒ Object

:nodoc:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef/monkey_patches/net-ssh-multi.rb', line 57

def next_session(server, force = false) #:nodoc:
  # don't retry a failed attempt
  return nil if server.failed?

  @session_mutex.synchronize do
    if !force && concurrent_connections && concurrent_connections <= open_connections
      connection = PendingConnection.new(server)
      @pending_sessions << connection
      return connection
    end

    # ===== PATCH START
    # Only increment the open_connections count if the connection
    # is not being forced. Incase of a force, it will already be
    # incremented.
    if !force
      @open_connections += 1
    end
    # ===== PATCH END
  end

  begin
    server.new_session

    # I don't understand why this should be necessary--StandardError is a
    # subclass of Exception, after all--but without explicitly rescuing
    # StandardError, things like Errno::* and SocketError don't get caught
    # here!
  rescue Exception, StandardError => e
    server.fail!
    @session_mutex.synchronize { @open_connections -= 1 }

    case on_error
    when :ignore then
      # do nothing
    when :warn then
      warn("error connecting to #{server}: #{e.class} (#{e.message})")
    when Proc then
      go = catch(:go) { on_error.call(server); nil }
      case go
      when nil, :ignore then # nothing
      when :retry then retry
      when :raise then raise
      else warn "unknown 'go' command: #{go.inspect}"
      end
    else
      raise
    end

    return nil
  end
end

#realize_pending_connections!Object

:nodoc:



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chef/monkey_patches/net-ssh-multi.rb', line 110

def realize_pending_connections! #:nodoc:
  return unless concurrent_connections

  server_list.each do |server|
    server.close if !server.busy?(true)
    server.update_session!
  end

  @connect_threads.delete_if { |t| !t.alive? }

  count = concurrent_connections ? (concurrent_connections - open_connections) : @pending_sessions.length
  count.times do
    session = @pending_sessions.pop
    break unless session
    # ===== PATCH START
    # Increment the open_connections count here to prevent
    # creation of connection thread again before that is
    # incremented by the thread.
    @session_mutex.synchronize { @open_connections += 1 }
    # ===== PATCH END
    @connect_threads << Thread.new do
      session.replace_with(next_session(session.server, true))
    end
  end
end