Class: SSHTunnel
- Inherits:
-
Object
- Object
- SSHTunnel
- Defined in:
- lib/shared/ssh_tunnel.rb
Constant Summary collapse
- INNER_TIMEOUT_SECONDS =
We want a small number so this fails quickly when unreachable. But not too small: a lower value caused false negatives on a poor connection. We want the inner timeout (the SSH connection) to trigger before the outer (loop), to avoid the situation where we make two connections in parallel.
15- OUTER_TIMEOUT_SECONDS =
INNER_TIMEOUT_SECONDS + 1
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(host, user, local_port = 2288) ⇒ SSHTunnel
constructor
A new instance of SSHTunnel.
- #open ⇒ Object
Constructor Details
#initialize(host, user, local_port = 2288) ⇒ SSHTunnel
Returns a new instance of SSHTunnel.
10 11 12 |
# File 'lib/shared/ssh_tunnel.rb', line 10 def initialize(host, user, local_port = 2288) @host, @user, @local_port = host, user, local_port end |
Instance Method Details
#connect ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/shared/ssh_tunnel.rb', line 30 def connect @thread.kill if @thread @thread = Thread.new do Net::SSH.start(@host, @user, { :timeout => INNER_TIMEOUT_SECONDS }) do |ssh| ssh.forward.local(@local_port, 'localhost', Testbot::SERVER_PORT) ssh.loop { @up = true } end end end |
#open ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/shared/ssh_tunnel.rb', line 14 def open connect start_time = Time.now while true break if @up sleep 0.5 if Time.now - start_time > OUTER_TIMEOUT_SECONDS puts "SSH connection failed, trying again... (#{@user}@#{@host})" start_time = Time.now connect end end end |