Handling connection failures with capistrano

Posted by Rob, Mon May 28 10:03:00 UTC 2007

We’ve had some problems with handling connection failures/timeouts with capistrano, because it currently (1.99.1) causes capistrano to exit from an exception trigged by Net::SSH. We wanted to handle this just like a command failure. I’ve not had time to form a proper patch for capistrano to submit upstream for this because of an impending deadline, but we’ve put a monkey patch in place (re-opening classes in ruby is just, well, awesome). I will of course write a proper patch for this once I’ve time, including being more specific about the Exceptions we wish to catch :)

You can use this to have capistrano treat a connection failure as a command failure:

module Capistrano
  class Configuration
    module Connections    
      # Hack, we return a fake connection which always reports closed.
      class FakeConnection
        def initialize(server)
          @server = server
        end

        def open_channel(&block)
          logger.important "could not open channel", @server if logger
          { :closed => true, :server => @server }
        end
      end

      class DefaultConnectionFactory
        def wrapped_connect_to(server)
          begin
            connection = capistrano_connect_to(server)
          rescue Exception
            FakeConnection.new(server)
          end
        end

        alias capistrano_connect_to connect_to
        alias connect_to wrapped_connect_to
      end
    end
end

Filed Under: | Tags:

Comments