【问题标题】:Unicorn keeps losing connection to postgres database on heroku独角兽不断失去与heroku上postgres数据库的连接
【发布时间】:2014-04-19 03:15:32
【问题描述】:

我已经使用独角兽网络服务器在 Heroku 上设置了一个应用程序。 我的数据库设置在外部服务器上(而不是在 heroku 上)。我已根据需要配置了 DATABASE_URL。 当我运行 heroku 控制台时,我能够在服务器上成功运行查询。 但是,当我在 unicorn 启动后访问 URL 时,会出现以下错误:

2014-03-14T01:44:53.820853+00:00 app[web.1]: ActiveRecord::StatementInvalid (PG::ConnectionBad: connection is closed:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
2014-03-14T01:44:53.820853+00:00 app[web.1]:                      pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
2014-03-14T01:44:53.820853+00:00 app[web.1]:                 FROM pg_attribute a LEFT JOIN pg_attrdef d
2014-03-14T01:44:53.820853+00:00 app[web.1]:                   ON a.attrelid = d.adrelid AND a.attnum = d.adnum
2014-03-14T01:44:53.820853+00:00 app[web.1]:                WHERE a.attrelid = '"currencies"'::regclass
2014-03-14T01:44:53.820853+00:00 app[web.1]:                  AND a.attnum > 0 AND NOT a.attisdropped
2014-03-14T01:44:53.820853+00:00 app[web.1]:                ORDER BY a.attnum
2014-03-14T01:44:53.820853+00:00 app[web.1]: ):
2014-03-14T01:44:53.820853+00:00 app[web.1]:   app/controllers/search_controller.rb:5:in `index'

我已经按照说明进行了配置。这是我的独角兽配置。

worker_processes Integer(ENV["WEB_CONCURRENCY"] || 2)
timeout 25
preload_app true

before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

    if defined?(ActiveRecord::Base)
      ActiveRecord::Base.connection.disconnect!
      puts 'Unicorn disconnected from database'
    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|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  if defined?(ActiveRecord::Base)
    config = Rails.application.config.database_configuration[Rails.env]
    config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10
    config['pool'] = ENV['DB_POOL'] || 2
    ActiveRecord::Base.establish_connection(config)
    puts 'Unicorn connected to database'
  end

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

为什么连接已关闭,即使它已成功连接? (我在日志中看到了Unicorn connected to database

【问题讨论】:

  • 你是怎么处理这个问题的?
  • 是否有任何网络设置会在超时等特定时间后阻止您的连接?

标签: ruby-on-rails postgresql heroku unicorn


【解决方案1】:

在 Rails 3 + Resque 中,我必须添加以下内容:

Resque.after_fork do |job|
  ActiveRecord::Base.verify_active_connections! 
end

我打赌类似的东西对你有用。池中的连接超时;他们必须被清除。不过,Rails 4 对此进行了更改,请参阅此处https://github.com/socialcast/resque-ensure-connected/issues/3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2016-01-07
    相关资源
    最近更新 更多