我在 Heroku 上使用 Rails、Sidekiq、Redis 和 Clockwork 对许多记录运行后台任务,但在尝试使用 API 更新的许多记录上,我不断收到此错误:
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
更新帖子
class UpdatePosts
include Sidekiq::Worker
sidekiq_options retry: false
def perform(site_id)
...
end
end