Sha256: eecd96a5b3f6f3d6d4cce35a80fc69edf7e082aa2456101b0ff4bc10fbb3b71c

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

require 'socket'
require 'timeout'

module RakeUp
  module Utilities
    class PortCheck
      attr_reader :host, :port, :error

      def initialize(host, port)
        @host = host
        @port = port
      end

      def open?
        @status == true
      end

      def closed?
        @status == false
      end

      def run
        @status = run_check
      end

      def to_s
        if open?
          "Found process listening on #{host}:#{port}"
        else
          "Unable to connect to process on #{host}:#{port}: #{error}"
        end
      end

      private
        def run_check
          begin
            Timeout::timeout(1) do
              begin
                s = TCPSocket.new(host, port)
                s.close
                return true
              rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => error
                @error = error
                return false
              end
            end
          rescue Timeout::Error => error
            @error = error
            return false
          end
        end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rakeup-1.2.0 lib/rakeup/utilities/port_check.rb
rakeup-1.1.0 lib/rakeup/utilities/port_check.rb
rakeup-1.0.1 lib/rakeup/utilities/port_check.rb
rakeup-1.0.0 lib/rakeup/utilities/port_check.rb