Skip to content

Instantly share code, notes, and snippets.

@ivarvong
Created September 16, 2014 19:04
Show Gist options
  • Save ivarvong/8b4da0b53fc9ed06c614 to your computer and use it in GitHub Desktop.
Save ivarvong/8b4da0b53fc9ed06c614 to your computer and use it in GitHub Desktop.
module Platform
module Database
def connect(size=25)
config = ActiveRecord::Base.configurations[Rails.env] || Rails.application.config.database_configuration[Rails.env]
#config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
config['pool'] = ENV['DB_POOL'] || size
puts "About to connect to AR with config: #{config}"
ActiveRecord::Base.establish_connection(config)
end
def disconnect
ActiveRecord::Base.connection_pool.disconnect!
end
def reconnect(size)
disconnect
connect(size)
end
module_function :disconnect, :connect, :reconnect
end
end
Rails.application.config.after_initialize do
Platform::Database.disconnect
ActiveSupport.on_load(:active_record) do
if Puma.respond_to?(:cli_config)
size = Puma.cli_config.options.fetch(:max_threads)
Platform::Database.reconnect(size)
else
Platform::Database.connect
end
Sidekiq.configure_server do |config|
size = Sidekiq.options[:concurrency]
Platform::Database.reconnect(size)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment