2

I'm using Ruby version 2.3.0.

I want to check when my application is up, and I wrote this method for my "deployer". At runtime http.request_get(uri) raises

EOFError: end of file reached

when I pass http://localhost as a first argument into the method:

require 'net/http'

def check_start_application(address, port)
  success_codes = [200, 301]
  attempts = 200
  uri = URI.parse("#{address}:#{port}")
  http = Net::HTTP.new(uri.host, uri.port)

  attempts.times do |attempt|
    # it raises EOFError: end of file reached
    http.request_get(uri) do |response|
      if success_codes.include?(response.code.to_i)
        return true
      elsif attempt == attempts - 1
        return false
      end
    end
  end
end

But, when I test this method separately from a context with irb, this code works pretty well for two cases:

check_start_application('http://example.com', '80')
check_start_application('http://localhost', any_port)

In an app's context this code works for only one case:

check_start_application('http://example.com', '80')

What I tried:

  • using 'rest-client' instead of 'net/http'
  • using 'net/https' with http.use_ssl = false
  • remove times from the method
  • call sleep before a request

Who faced a similar problem? I believe I'm not the only one.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
arkhwise
  • 873
  • 9
  • 13
  • On which port your code is failing? Is your app running on https ? – rorra Jul 18 '16 at 20:37
  • @rorra Thx. My app deploys on a random free port, and I use Docker for this. The range of ports is within an ephemeral port range. The app isn't running on https. – arkhwise Jul 18 '16 at 22:05
  • @rorra I got what I want through %x(curl --write-out %{http_code} --silent 127.0.0.1:#{any_port}). But it's not the right way to check response. – arkhwise Jul 19 '16 at 06:13

1 Answers1

0

It may be that on the deploy your app is running on SSL. It's hard to help debug without access to it, but try with:

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
rorra
  • 9,593
  • 3
  • 39
  • 61