Created
October 17, 2015 07:56
-
-
Save oojikoo-gist/4f8389fa746894e347e3 to your computer and use it in GitHub Desktop.
rails: reliable sidekiq configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /config/initializers/sidekiq.rb | |
current_web_concurrency = Proc.new do | |
web_concurrency = ENV['WEB_CONCURRENCY'] | |
web_concurrency ||= Puma.respond_to? | |
(:cli_config) && Puma.cli_config.options.fetch(:max_threads) | |
web_concurrency || 16 | |
end | |
local_redis_url = Proc.new do | |
config_file = File.join(File.expand_path('../..', __FILE__), 'redis.conf') | |
port = File.read(config_file).chomp.split.last | |
"redis://localhost:#{port}" | |
end | |
ENV['REDISTOGO_URL'] ||= local_redis_url.call | |
Sidekiq.configure_server do |config| | |
config.redis = { url: ENV['REDISTOGO_URL'], namespace: 'brewdega' } | |
Rails.application.config.after_initialize do | |
ActiveRecord::Base.connection_pool.disconnect! | |
ActiveSupport.on_load(:active_record) do | |
config = Rails.application.config.database_configuration[Rails.env] | |
config['reaping_frequency'] = ENV['DATABASE_REAP_FREQ'] || 10 # seconds | |
config['pool'] = ENV['WORKER_DB_POOL_SIZE'] || Sidekiq.options[:concurrency] | |
ActiveRecord::Base.establish_connection(config) | |
Rails.logger.info("Connection Pool size for Sidekiq Server is now: #{ActiveRecord::Base.connection.pool.instance_variable_get('@size')}") | |
end | |
end | |
end | |
Sidekiq.configure_client do |config| | |
config.redis = { url: ENV['REDISTOGO_URL'], namespace: 'brewdega', :size => 1 } | |
Rails.application.config.after_initialize do | |
ActiveRecord::Base.connection_pool.disconnect! | |
ActiveSupport.on_load(:active_record) do | |
config = Rails.application.config.database_configuration[Rails.env] | |
config['reaping_frequency'] = ENV['DATABASE_REAP_FREQ'] || 10 # seconds | |
config['pool'] = ENV['WEB_DB_POOL_SIZE'] || current_web_concurrency.call | |
ActiveRecord::Base.establish_connection(config) | |
# DB connection not available during slug compliation on Heroku | |
Rails.logger.info("Connection Pool size for web server is now: #{config['pool']}") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment