我在Heroku上运行一个Rails应用程序,其中包括几个Web Dynos和一个Worker Dyno。我每天通过Sidekiq运行数千个工作任务,但偶尔会出现ActiveRecord :: ConnectionTimeoutError错误(大约每天出现50次)。我已按以下方式设置我的独角兽服务器:
worker_processes 4
timeout 30
preload_app true
before_fork do |server, worker|
# As suggested here: https://devcenter.heroku.com/articles/rails-unicorn
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!
end
end
after_fork do |server,worker|
if defined?(ActiveRecord::Base)
config = Rails.application.config.database_configuration[Rails.env]
config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
config['pool'] = ENV['DB_POOL'] || 10
ActiveRecord::Base.establish_connection(config)
end
Sidekiq.configure_client do |config|
config.redis = { :size => 1 }
end
Sidekiq.configure_server do |config|
config = Rails.application.config.database_configuration[Rails.env]
config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
config['pool'] = ENV['DB_POOL'] || 10
ActiveRecord::Base.establish_connection(config)
end
end
在Heroku上,我将DB_POOL配置变量设置为2,如Heroku推荐。这些错误应该完全避免不了吗?似乎很奇怪。你有什么建议吗?
config
。如果您想在 sidekiq 的config
上调用任何内容,则应将本地变量重命名为db_config
或其他名称。 - mpoisotconfig
重命名为db_config
。我刚刚更新了答案以反映这一点。再次感谢您。 - stereoscott