2014-03-14 113 views
4

我已經使用獨角獸web服務器在heroku上設置了一個應用程序。 我的數據庫設置在外部服務器上(不是在heroku上)。我已根據需要配置了DATABASE_URL。 當我運行heroku控制檯時,我能夠成功地在服務器上運行查詢。 然而,當我去到一個URL麒麟已經啓動了後,我得到這個錯誤:獨角獸不斷連接到heroku上的postgres數據庫

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在日誌中

+0

你是如何處理這個問題的? – brauliobo

+0

是否有任何網絡設置會在超時等特定時間後阻止你的連接? – rrrrong

回答

0

在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

相關問題