Skip to content
Browse files

Improve logging.

  • Loading branch information...
1 parent d11ea7c commit be81825b8f37e40921a9de62886978f2d1248ba7 @imbriaco committed Aug 14, 2011
Showing with 21 additions and 9 deletions.
  1. +11 −3 lib/sinatra/redis_tunnel.rb
  2. +10 −6 lib/tcp_tunnel.rb
View
14 lib/sinatra/redis_tunnel.rb
@@ -6,18 +6,26 @@ module Sinatra
module RedisTunnel
def redis_tunnel
tunnel = URI.parse(ENV['TUNNEL_URL'])
- @redis_tunnel ||= TCPTunnel.new(tunnel.user, tunnel.host, tunnel.port, ENV['TUNNEL_SSH_KEY'])
+ @redis_tunnel ||=
+ begin
+ STDERR.puts "#{self.class}: Establishing new TCPTunnel connection."
+ TCPTunnel.new(tunnel.user, tunnel.host, tunnel.port, ENV['TUNNEL_SSH_KEY'])
+ end
end
def redis
- @redis ||= Redis.new(:path => redis_tunnel.socket.path, :password => ENV['REDIS_PASSWORD'])
+ @redis ||=
+ begin
+ STDERR.puts "#{self.class}: Returning new Redis connection."
+ Redis.new(:path => redis_tunnel.socket.path, :password => ENV['REDIS_PASSWORD'])
+ end
end
protected
def self.registered(app)
app.after do
- # Explicitly close the redis client to avoid leaking SSH tunnels
+ STDERR.puts "#{self.class}: Closing Redis client socket."
redis.quit
end
end
View
16 lib/tcp_tunnel.rb
@@ -8,11 +8,13 @@ class TCPTunnel
def initialize(user, host, port, key_data)
@user, @host, @port, @key_data = user, host, port, key_data
- @socket ||= begin
- path = "/tmp/tcp_tunnel_#{user}@#{host}_#{port}.sock"
- FileUtils.rm_f(path)
- UNIXServer.new(path)
- end
+ @socket ||=
+ begin
+ path = "/tmp/tcp_tunnel_#{user}@#{host}_#{port}.sock"
+ STDERR.puts "#{self.class}: Creating new UNIX socket server at #{path}."
+ FileUtils.rm_f(path)
+ UNIXServer.new(path)
+ end
@pid = connect
@@ -35,12 +37,14 @@ def connect
while true
begin
+ STDERR.puts "#{self.class}: Establishing new SSH session to #{@user}@#{@host}."
Net::SSH.start(@host, @user, :key_data => @key_data) do |ssh|
+ STDERR.puts "#{self.class}: Setting up SSH port forwarding for #{socket.path} to remote host port #{@port}."
ssh.forward.local(socket, "localhost", @port)
ssh.loop { true }
end
rescue IOError => ioe
- STDERR.puts "#{self.class} caught IO error: #{ioe.to_s}. Attempting to reconnect."
+ STDERR.puts "#{self.class}: Caught IO error: #{ioe.to_s}. Attempting to reconnect."
end
end
end

0 comments on commit be81825

Please sign in to comment.
Something went wrong with that request. Please try again.