Sha256: f18ea67ab990606d3841de0600eda6ad3000e0832bf1472f69c3c4db3a2a995b

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'socket'
require 'timeout'

module Orats
  module Server
    def foreman_start
      puts  '', '='*80
      say_status  'action', "\e[1mStarting server with the following commands:\e[0m", :cyan
      say_status  'command', "cd #{@app_name}", :magenta
      say_status  'command', 'bundle exec foreman start', :magenta
      puts  '='*80, ''

      attempt_to_start
    end

    private

      def attempt_to_start
        while port_taken? do
          puts
          say_status  'error', "\e[1mAnother application is using port 3000\n\e[0m", :red
          puts        '-'*80

          exit 1 if no?('Would you like to try running the server again? (y/N)', :cyan)
        end

        puts
        run_with_cd 'bundle exec foreman start'
      end

      def port_taken?
        begin
          Timeout::timeout(5) do
            begin
              s = TCPSocket.new('localhost', 3000)
              s.close

              return true
            rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
              return false
            end
          end
        rescue Timeout::Error
          false
        end

        false
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
orats-0.2.0 lib/orats/server.rb