Heroku偶尔出现Postgres错误:无法将主机名“<pg url =”“>”转换为地址:名称或服务未知(PG ::错误)</pg>

时间:2012-08-28 18:11:52

标签: ruby-on-rails postgresql heroku unicorn

我现在正在Heroku上使用共享postgres(dev / free)计划,我在日志中看到这个错误相当多。虽然连接到PG数据库通常都有效,但我不确定这是否属于我的控制之内。当发生共享数据库时,Heroku状态页面不会列出共享数据库的任何停机时间。

使用Rails 3.1.1和the Unicorn web server

错误: 无法将主机名“pg60.sharedpg.heroku.com(http://pg60.sharedpg.heroku.com)”转换为地址:名称或服务未知(PG ::错误)

我的Unicorn配置文件(在Heroku中查看Unicorn的推荐设置后):

worker_processes 3 # amount of unicorn workers to spin up
timeout 30         # restarts workers that hang for 30 seconds
preload_app true

before_fork do |server, worker|
  # Replace with MongoDB or whatever
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection_handler.clear_all_connections!
    ActiveRecord::Base.connection.disconnect!
    Rails.logger.info('Disconnected from ActiveRecord')
  end

  # If you are using Redis but not Resque, change this
  if defined?(Resque)
    Resque.redis.quit
    Rails.logger.info('Disconnected from Redis')
  end
end

after_fork do |server, worker|
  # Replace with MongoDB or whatever
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
    ActiveRecord::Base.connection_handler.verify_active_connections!
    Rails.logger.info('Connected to ActiveRecord')
  end

  # If you are using Redis but not Resque, change this
  if defined?(Resque)
    Resque.redis = ENV['REDIS_URI']
    Rails.logger.info('Connected to Redis')
  end
end

跟踪:

 328 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1032:in `initialize': could not translate host name "pg60.sharedpg.heroku.com (http://pg60.sharedpg.heroku.com)" to address: Name or service not known (PG::Error)
219 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1032:in `new'
223 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1032:in `connect'
225 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:301:in `initialize'
217 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
235 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'

328 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1032:in `initialize': could not translate host name "pg60.sharedpg.heroku.com (http://pg60.sharedpg.heroku.com)" to address: Name or service not known (PG::Error)
219 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1032:in `new'
223 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:1032:in `connect'
225 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:301:in `initialize'
217 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
235 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
235 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:304:in `new_connection'
244 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:323:in `checkout_new_connection'
249 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block (2 levels) in checkout'
225 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:261:in `loop'
238 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:261:in `block in checkout'
153 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /usr/local/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
229 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:260:in `checkout'
231 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:162:in `connection'
240 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:409:in `retrieve_connection'
249 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_specification.rb:107:in `retrieve_connection'
239 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
194 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.1/lib/active_record/base.rb:703:in `table_exists?'
128 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/app.rb:38:in `<top (required)>'
116 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from config.ru:9 (http://config.ru:9):in `require'
124 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from config.ru:9 (http://config.ru:9):in `block in <main>'
179 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
176 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize'
112 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from config.ru:1 (http://config.ru:1):in `new'
115 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from config.ru:1 (http://config.ru:1):in `<main>'
168 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.2.0/lib/unicorn.rb:44:in `eval'
180 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.2.0/lib/unicorn.rb:44:in `block in builder'
181 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.2.0/lib/unicorn/http_server.rb:691:in `call'
187 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.2.0/lib/unicorn/http_server.rb:691:in `build_app!'
182 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.2.0/lib/unicorn/http_server.rb:136:in `start'
178 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/gems/unicorn-4.2.0/bin/unicorn:121:in `<top (required)>'
146 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/bin/unicorn:19:in `load'
148 <13>1 2012-08-27T21:36:37+00:00 d.4122435c-cb44-421a-9a7d-a68bb10457d6 app web.1 (http://web.1) - - from /app/vendor/bundle/ruby/1.9.1/bin/unicorn:19:in `<main>' 

2 个答案:

答案 0 :(得分:6)

这是DNS错误。它与你的代码几乎没有任何关系。如果你的Rails应用程序和你的数据库都在Heroku中,请联系Heroku支持。

如果你的Rails应用程序在外面运行 Heroku云(作为一点搜索“Unicorn”一词表明它可能是; 请链接到这样的事情;不是每个人都知道什么是“独角兽”),你需要做一些调试来解决DNS问题所在。它是您的应用程序使用的递归DNS服务器吗?一些更近的​​缓存服务器?链上的一些中间服务器用于解析Heroku的DNS(不太可能)? Heroku的DNS是自己的吗?这是一个数据包丢失问题,服务器响应假负,一个循环群集中的一个坏服务器,...?我希望您可以dig +trace以及traceroutemtr花费大量时间 - 假设您可以ssh进入。如果您有root,tcpdump和{{ 3}}也可能有用,如果您可以访问它们,将检查您正在使用的DNS服务器的日志。

如果你没有shell访问权限,那么你几乎不得错过你所托管的人的支持。

答案 1 :(得分:1)

仅供参考,我遇到了同样的问题(Heroku遇到了AWS问题)。以下是客户服务(08/15)的有用回复:

  

我们偶尔会在我们的实例上看到DNS解析失败   我们正在努力通过AWS解决这个问题。这个问题确实存在   间歇性的,不幸的是我们很难尝试   找出原因。在我们进行调查的过程中,我有一个解决方法   对于这个问题 - 目前,你正在使用Django的默认方式   数据库连接 - 试图建立新的连接   请求。这非常昂贵,这也意味着您需要这样做   每个请求的DNS解析会增加   非常不幸地面对这个问题的可能性。相反,我   建议您使用连接池。你可以找到一些例子   这里:

     

https://devcenter.heroku.com/articles/python-concurrency-and-database-connections

     

偶尔会看到此问题的一位客户(但经常   与Django应用程序)看到连接池的良好改进,   这应该有助于防止它发生。