getaddrinfo:节点名或服务名提供,或不知道

我在运行Mac OS X 10.6的计算机上部署了一个Ruby on Rails应用程序。 出现问题的代码由delayed_job运行。 只有通过delayed_job运行时才会出现问题。 如果我在控制台( rails console production )中运行它,或者通过cURL直接调用API,它没有任何问题。 而且,整个过程在我的开发环境中没有问题。

基本上,代码在一个地方工作,但由于某种原因,失败的地方工作。 我已经search并find了一些资源,但没有任何build议适用或有任何区别。 当我login到服务器并运行命令时,一切正常。 但由于某种原因,当它们由Capistrano运行/启动时,它不起作用。

任何帮助是极大的赞赏。

重要笔记:

  • Ruby版本:1.9.2-p0
  • Rails版本:3.0.1
  • delayed_job版本:最新(来自collectiveidea / delayed_job )
  • rest-client版本:1.6.1

码:

 class CallApi < Struct.new(:num) def perform log "Entering perform" apinum = num || 5 log "ApiNum = #{apinum}" results = attempt(2,10) do ActiveSupport::JSON.decode(RestClient.get(API_URL, {:params => {:apinum => apinum}})) end log "Results retrieved. (count: #{results.count})" end def log(message) Delayed::Worker.logger.info "[CallApi] #{Time.now} - #{message}" end end 

环境configuration(注:url是假的,但forms与真实的一样):

 API_URL = "http://api.example.org/api_endpoint" # Originally, I had "http://" before the beginning, but found a # post mentioning that Net::Http.start didn't like that. # So I tried it both ways. # The same error occurs regardless of if the "http://" is there. 

打电话错误发生的地方:

 RestClient.get(API_URL, {:params => {:apinum => apinum}}) 

错误:

 getaddrinfo: nodename nor servname provided, or not known /Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/net/http.rb:644:in `initialize' /Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/net/http.rb:644:in `open' /Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/net/http.rb:644:in `block in connect' /Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/timeout.rb:44:in `timeout' /Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/timeout.rb:87:in `timeout' /Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/net/http.rb:644:in `connect' /Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/net/http.rb:637:in `do_start' /Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/net/http.rb:626:in `start' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/rest-client-1.6.1/lib/restclient/request.rb:166:in `transmit' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/rest-client-1.6.1/lib/restclient/request.rb:60:in `execute' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/rest-client-1.6.1/lib/restclient/request.rb:31:in `execute' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/rest-client-1.6.1/lib/restclient.rb:68:in `get' /private/var/www/project-gemset/releases/20101109002137/lib/call_api.rb:7:in `block in perform' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/attempt-0.2.0/lib/attempt.rb:70:in `attempt' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/attempt-0.2.0/lib/attempt.rb:114:in `attempt' /private/var/www/project-gemset/releases/20101109002137/lib/call_api.rb:6:in `perform' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/backend/base.rb:77:in `invoke_job' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:118:in `block (2 levels) in run' /Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/timeout.rb:57:in `timeout' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:118:in `block in run' /Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:117:in `run' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:176:in `reserve_and_run_one_job' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:102:in `block in work_off' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:101:in `times' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:101:in `work_off' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:76:in `block (2 levels) in start' /Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:75:in `block in start' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:72:in `loop' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:72:in `start' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/command.rb:100:in `run' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/command.rb:79:in `block in run_process' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application.rb:250:in `call' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application.rb:250:in `block in start_proc' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/daemonize.rb:199:in `call' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/daemonize.rb:199:in `call_as_daemon' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application.rb:254:in `start_proc' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application.rb:294:in `start' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application_group.rb:159:in `block (2 levels) in start_all' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application_group.rb:158:in `fork' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application_group.rb:158:in `block in start_all' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application_group.rb:157:in `each' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application_group.rb:157:in `start_all' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/controller.rb:80:in `run' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons.rb:193:in `block in run_proc' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in `call' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in `catch_exceptions' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons.rb:192:in `run_proc' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/command.rb:78:in `run_process' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/command.rb:72:in `block in daemonize' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/command.rb:70:in `times' /Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/command.rb:70:in `daemonize' script/delayed_job:5:in `' 

DNSparsing失败时发生错误。 检查你是否可以从命令行wget(或curl)的APIurl。 更改DNS服务器并进行testing可能会有所帮助。

我今天遇到了类似的情况 – 将应用程序部署到Mac OS X服务器,并在尝试访问外部API时收到“getaddrinfo”消息。 事实certificate,当最初启动应用程序的ssh会话不再处于活动状态时,会发生错误。 这就是为什么一切工作正常,如果你ssh到你的服务器和手动运行命令(或手动启动服务器) – 只要你保持你的SSH会话活着,这个错误不会发生。

无论这是OS X中的一个bug还是一个怪癖,我都不确定。 以下是使我find解决scheme的页面 – http://lists.apple.com/archives/unix-porting/2010/Jul/msg00001.html

我所要做的就是更新我的capistrano任务,使用“nohup”启动应用程序。 所以改变

 run "cd #{current_path} && RAILS_ENV=production unicorn_rails -c config/unicorn.rb -D" 

 run "cd #{current_path} && RAILS_ENV=production nohup unicorn_rails -c config/unicorn.rb -D" 

为我做了诡计。

希望这有助于某人 – 这是一个相当痛苦的弄清楚!

rest-client的RestClient在parsingURL时需要http: scheme。 它为你调用Net::HTTP ,它不需要http:部分,但是rest-client负责这个。

该url是您尝试访问的实际url吗? example.org是一个用于testing和文档的有效域,并且可以访问; 我期望“api”和“api_endpoint”部分失败,并在我尝试联系他们时看到。

 require 'socket' IPSocket.getaddress('example.org') # => "2620:0:2d0:200::10" IPSocket.getaddress('api.example.org') # => # ~> -:7:in `getaddress': getaddrinfo: nodename nor servname provided, or not known (SocketError) # ~> from -:7:in `<main>' 

以下是我使用Curl的内容:

 greg-mbp-wireless:~ greg$ curl api.example.org/api_endpoint curl: (6) Couldn't resolve host 'api.example.org' greg-mbp-wireless:~ greg$ curl example.org/api_endpoint <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>404 Not Found</title> </head><body> <h1>Not Found</h1> <p>The requested URL /api_endpoint was not found on this server.</p> <hr> <address>Apache Server at example.org Port 80</address> </body></html> greg-mbp-wireless:~ greg$ curl example.org <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <TITLE>Example Web Page</TITLE> </HEAD> <body> <p>You have reached this web page by typing &quot;example.com&quot;, &quot;example.net&quot;,&quot;example.org&quot or &quot;example.edu&quot; into your web browser.</p> <p>These domain names are reserved for use in documentation and are not available for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC 2606</a>, Section 3.</p> </BODY> </HTML> 

我重新启动了我的电脑(Mac Mountain Lion),问题解决了。 一些事情必须与壳认为它是从互联网断开,我想。

以某种确定的方式重新启动shell也可以解决这个问题。 只是打开一个新的会话/窗口,但没有奏效。

我简单地通过closures和重新打开terminal来解决这个问题。

我看到这个错误与轨道无关。 原来我的testing是试图使用一个太高的端口(大于65535)。

这段代码会产生有问题的错误

 require 'socket' Socket.getaddrinfo("127.0.0.1", "65536") 

我在尝试开发与Internet断开连接时遇到了错误。 但是,我正在处理的网站需要能够与其他一些网站进行交stream,所以当它不能这样做的时候就会窒息。 连接到互联网修复了错误。

如果以上全部失败,请尝试转换为UNIX行结尾,或者执行:

 brew install dos2unix sudo dos2unix -c mac /private/etc/hosts 

也许主机编码是错误的。

希望这可以帮助

我有这个问题运行rake db:create 。 这个网页提到了我的DNS问题。 我检查了我的VPN连接,发现它由于某种原因断开连接。 我重新连接,现在耙工作顺利。

为了避免这个问题,我们可以绑定到127.0.0.1而不是localhost

bin/rails server -b 127.0.0.1

我得到了同样的错误,当我检查本地主机设置在主机文件它是不知何故未设置。 设置本地主机到127.0.0.1解决了它。

 sudo vi /etc/hosts >> 127.0.0.1 localhost