2

我有一堆更新数据库的后台任务(Sidekiq 工作人员),并且我不断收到这个失败的线程异常。

Heroku Log: WARN: could not obtain a database connection within 5.000 seconds (waited 5.001 seconds)
  • Heroku
  • Heroku Postgres :: Olive - 20 个连接限制。
  • Redis ToGo - 10 个连接限制。
  • Sidekiq - 2 个连接。

每个客户端请求创建约 50 个线程 - 最后约 20 个线程尝试更新数据库。现在我知道这是太多试图建立连接的线程(更新 Active:Record..)。我不介意他们等到成功再试一次。

-config/unicorn.rb

worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 30
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!
  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 = ActiveRecord::Base.configurations[Rails.env] ||
          Rails.application.config.database_configuration[Rails.env]
      config['pool']            =   ENV['DB_POOL'] || 2
      config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
      ActiveRecord::Base.establish_connection(config)
  end
end

-config/initializers/sidekiq.rb

require 'sidekiq'
  Sidekiq.configure_server do |config|
    if(database_url = ENV['DATABASE_URL'])
        p pool_size = Sidekiq.options[:concurrency] + 2
        p ENV['DATABASE_URL'] = "#{database_url}?pool=#{pool_size}"
        ActiveRecord::Base.establish_connection
    end
end

--condig/sidekiq.yml

:concurrency: 2

非常感谢所有的帮助,埃尔达

4

0 回答 0