Skip to content

Instantly share code, notes, and snippets.

@swalberg
Created March 29, 2014 14:09
Show Gist options
  • Save swalberg/9855092 to your computer and use it in GitHub Desktop.
Save swalberg/9855092 to your computer and use it in GitHub Desktop.
module MyApp
module Database
def connect(pool_size = nil, reap_time = nil)
return unless defined? ActiveRecord::Base
config = Rails.application.config.database_configuration[Rails.env]
config['reaping_frequency'] = reap_time || ENV['AR_DB_REAP_FREQ'] || 10 # seconds
config['pool'] = pool_size || ENV['AR_DB_POOL'] || 5
ActiveRecord::Base.establish_connection(config)
end
def disconnect
return unless defined? ActiveRecord::Base
ActiveRecord::Base.connection_pool.disconnect!
end
def reconnect(pool_size = nil, reap_time = nil)
disconnect
connect(pool_size, reap_time)
end
module_function :disconnect, :connect, :reconnect
end
end
# doc
# https://github.com/puma/puma/blob/master/examples/config.rb
max_threads = Integer(ENV['PUMA_MAX_THREADS'] || '16')
threads Integer(ENV['PUMA_MIN_THREADS'] || '0'), max_threads
workers Integer(ENV['PUMA_WORKERS'] || '1')
require './lib/config/active_record'
on_worker_boot do
ActiveSupport.on_load(:active_record) do
MyApp::Database.reconnect(max_threads)
end
end
if ENV['PUMA_CONTROL_ENDPOINT']
if ENV['PUMA_CONTROL_ENDPOINT_AUTH_TOKEN']
activate_control_app ENV['PUMA_CONTROL_ENDPOINT'], { auth_token: ENV['PUMA_CONTROL_ENDPOINT_AUTH_TOKEN'] }
elsif ENV['PUMA_CONTROL_ENDPOINT_NO_AUTH_TOKEN']
activate_control_app ENV['PUMA_CONTROL_ENDPOINT'], { no_token: true }
else
activate_control_app ENV['PUMA_CONTROL_ENDPOINT']
end
end
preload_app!
ENV['PUMA_LOADED'] = 'y'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment