Sha256: 45f3f5bc48eb3aa1b2ee56cff95a531d1d3302a335436b5928f220bf822ee930

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 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 #{@active_path}", :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_from @active_path, '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

2 entries across 2 versions & 1 rubygems

Version Path
orats-0.3.1 lib/orats/server.rb
orats-0.3.0 lib/orats/server.rb