Skip to content

Instantly share code, notes, and snippets.

@ksaynice
Forked from sj26/boot.rb
Last active October 13, 2015 04:32
Show Gist options
  • Save ksaynice/2fe23453fe3b50cb67b0 to your computer and use it in GitHub Desktop.
Save ksaynice/2fe23453fe3b50cb67b0 to your computer and use it in GitHub Desktop.
Use dotenv with unicorn (put in config/boot.rb, thanks @keithpitt)
class PumaHelper
def self.refresh_connection_pool(base_klass, config_key)
base_klass = base_klass.constantize
config = Rails.application.config.database_configuration[config_key]
base_klass.connection_pool.disconnect! rescue ActiveRecord::ConnectionNotEstablished
base_klass.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
base_klass.establish_connection config
true
rescue
false
end
end
threads ENV.fetch('NIBBLER_PUMA_MIN_THREADS').to_i, ENV.fetch('NIBBLER_PUMA_MAX_THREADS').to_i
workers ENV.fetch('NIBBLER_PUMA_WORKERS').to_i
bind ENV.fetch('NIBBLER_PUMA_SOCKET_DIR')
pidfile ENV.fetch('NIBBLER_PUMA_PIDFILE')
state_path ENV.fetch('NIBBLER_PUMA_STATE_PATH')
preload_app!
on_worker_boot do
if defined?(ActiveRecord::Base)
ActiveSupport.on_load(:active_record) do
PumaHelper.refresh_connection_pool 'ActiveRecord::Base', rails_env
end
end
end
activate_control_app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment