Skip to content

Instantly share code, notes, and snippets.

@swalberg
Created March 29, 2014 14:09
Show Gist options
  • Select an option

  • Save swalberg/9855092 to your computer and use it in GitHub Desktop.

Select an option

Save swalberg/9855092 to your computer and use it in GitHub Desktop.

Revisions

  1. @steakknife steakknife revised this gist Mar 28, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions puma.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # 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')
  2. @steakknife steakknife created this gist Mar 28, 2014.
    23 changes: 23 additions & 0 deletions active_record.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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
    24 changes: 24 additions & 0 deletions puma.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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'