1

I have a script that when run in the console performs a series of api calls and it works wonderfully. But when I run it on the server I get a "getaddrinfo: A non-recoverable error occurred.." error

here's the piece of my code that seems to be causing problems:

require "uri"
require "net/http"
require "json"
uri = URI("https://api.domain.com/location/of/api_call/data.json?other=1&query=1&stuff=1")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
headers = {"Range" => "bytes=1000-",
           "Api-Key" => API_KEY,
           "X-Api-Secret" => API_SECRET}
response = ""
res = http.get(uri.path + "?" + uri.query, headers) do |data|
    response += data
end
if res.code.to_i === 200
    response = JSON.parse(response)
else
    response = {"error" => "api call failed"}
end

Fairly simple... I had to massage the http and res object a bit to get the desired results. It works great from the console, but when I launch the server I get errors.

context: I'm running a Dashing dashboard which uses rufus-scheduler to schedule jobs. This is a job that I have scheduled to run ever 30 minutes. Dashing also uses a Thin server instead of Unicorn.

Here's the full error I get:

scheduler caught exception:
getaddrinfo: A non-recoverable error occurred during a database lookup.
c:/Ruby2.0.0/lib/ruby/2.0.0/net/http.rb:878:in `initialize'
c:/Ruby2.0.0/lib/ruby/2.0.0/net/http.rb:878:in `open'
c:/Ruby2.0.0/lib/ruby/2.0.0/net/http.rb:878:in `block in connect'
c:/Ruby2.0.0/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
c:/Ruby2.0.0/lib/ruby/2.0.0/net/http.rb:877:in `connect'
c:/Ruby2.0.0/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
c:/Ruby2.0.0/lib/ruby/2.0.0/net/http.rb:851:in `start'
c:/Ruby2.0.0/lib/ruby/2.0.0/net/http.rb:1367:in `request'
c:/Ruby2.0.0/lib/ruby/2.0.0/net/http.rb:1126:in `get'
c:/path/to/file.rb: 215:in `block in <top (required)>'

Note: line 215 is on the "res = ..."

UPDATE:

I did an nslookup on the api address and got

*** Default servers are not available
Server:  UnKnown
Address:  127.0.0.1
TheJKFever
  • 685
  • 7
  • 25
  • could it just be a timeout? – Taryn East Jul 01 '14 at 01:01
  • 1
    also silly question - your API_KEY/SECRET are set up correctly? – Taryn East Jul 01 '14 at 01:03
  • I really doubt that, I have a feeling it's more of something to do with DNS. I was on localhost, but switched to using 127.0.0.1. That didn't solve it though. How would I test if it's timing out? – TheJKFever Jul 01 '14 at 01:03
  • yes it works from the console so the api/secret work fine. I just removed it for sake of posting. – TheJKFever Jul 01 '14 at 01:03
  • console is often a different environment... so worth asking/checking :) especially as running it asynchronously often doesn't include all of rails. can you do a `puts` of all your relevant data just before the `http.get` to check it's what you expect? – Taryn East Jul 01 '14 at 01:06
  • I actually have puts in my code right before the http.get ironically, but I'm trying it right now within the get "puts data" will let you know in a second – TheJKFever Jul 01 '14 at 01:21
  • ok so it print out the url that it was about the send the request to. Everything seems fine there, but it didn't get to the data. So it never got into the data part. (obviously, since it said the error was on the line with "res = "...) – TheJKFever Jul 01 '14 at 01:32
  • Yeah, data doesn't exist outside of the get-call... so `puts uri.path + "?" + uri.query` and `puts headers.inspect` return what you expect (just before the get)? Have you tried just getting the url without doing the data bit? `res = http.get(uri.path + "?" + uri.query, headers)` then `puts res.inspect` – Taryn East Jul 01 '14 at 02:06

0 Answers0