Redis,Rails,Heroku上的Sidekiq&Clock后台任务只更新一些
我在Rails,Sidekiq,Redis和Heroku上的Clockwork上运行了很多记录的后台任务,但是我一直在很多记录上看到这个错误试图用一个API更新:Redis,Rails,Heroku上的Sidekiq&Clock后台任务只更新一些
ActiveRecord::ConnectionTimeoutError: could not obtain a database connection within 5.000 seconds
这里是我的文件:
独角兽:
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3) timeout 15
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 = Rails.application.config.database_configuration[Rails.env]
config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
config['pool'] = ENV['DB_POOL'] || 5
ActiveRecord::Base.establish_connection(config)
end
end
数据库:
production: adapter: postgresql
encoding: unicode
database: production
pool: 25
timeout: 10000
时钟:
every(24.hours, 'Update') do sites = Site.order(:id).pluck(:id)
Site.each do |site|
UpdatePosts.perform_async(site)
end
end
UpdatePosts
class UpdatePosts include Sidekiq::Worker
sidekiq_options retry: false
def perform(site_id)
...
end
end
回答:
你看到的是由多个线程试图获得比有在池中的连接ActiveRecord的连接池的连接造成的错误。当线程要求连接时,但在5秒超时内没有空闲时,会看到您看到的错误。
在Sidekiq中,工作线程数量大于默认的ActiveRecord池大小。这会导致负载下的错误。
您可以调整DB池的大小以匹配您config/initializers/sidekiq.rb
文件中使用代码Sidekiq线程数是这样的:
Sidekiq.configure_server do |config| if(database_url = ENV['DATABASE_URL'])
pool_size = Sidekiq.options[:concurrency] + 2
ENV['DATABASE_URL'] = "#{database_url}?pool=#{pool_size}"
ActiveRecord::Base.establish_connection
end
end
虽然你试图增加pool_size
在database.yml
文件,这个配置覆盖Heroku中的部署,完全由DATABASE_URL
环境变量驱动。
以上是 Redis,Rails,Heroku上的Sidekiq&Clock后台任务只更新一些 的全部内容, 来源链接: utcz.com/qa/257842.html