我有一个脚本,当在控制台中运行时,它会执行一系列 api 调用,并且效果很好。但是当我在服务器上运行它时,我得到一个“getaddrinfo:发生不可恢复的错误..”错误
这是我的一段似乎导致问题的代码:
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
相当简单......我不得不稍微按摩一下 http 和 res 对象以获得所需的结果。它在控制台上运行良好,但是当我启动服务器时出现错误。
上下文:我正在运行一个 Dashing 仪表板,它使用 rufus-scheduler 来安排作业。这是我计划每 30 分钟运行一次的工作。Dashing 还使用瘦服务器而不是 Unicorn。
这是我得到的完整错误:
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)>'
注意:第 215 行在“res = ...”上
更新:
我对 api 地址进行了 nslookup 并得到了
*** Default servers are not available
Server: UnKnown
Address: 127.0.0.1