Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Setting up Puma and Rails on Heroku
# Gemfile
gem "puma"
# Procfile
web: bundle exec puma -p $PORT -e $RACK_ENV -C config/puma.rb
# add to config block config/environments/production.rb
config.threadsafe!
# get rid of NewRelic after_fork code, if you were doing this:
# http://support.newrelic.com/kb/troubleshooting/unicorn-no-data
# and get rid of config/unicorn.rb if you were using that
# config/puma.rb
require "active_record"
cwd = File.dirname(__FILE__)+"/.."
ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
ActiveRecord::Base.establish_connection(ENV["DATABASE_URL"] || YAML.load_file("#{cwd}/config/database.yml")[ENV["RACK_ENV"]])
ActiveRecord::Base.verify_active_connections!
# if you use NewRelic, set your NEWRELIC_DISPATCHER environment variable on heroku, per
# https://github.com/puma/puma/issues/128 - may not be needed for future releases of Puma
heroku config:add NEWRELIC_DISPATCHER=Puma
@juniorz

This comment has been minimized.

Show comment Hide comment
@juniorz

juniorz Dec 14, 2012

Why do you need the config/puma.rb?

juniorz commented Dec 14, 2012

Why do you need the config/puma.rb?

@thomasklemm

This comment has been minimized.

Show comment Hide comment
@thomasklemm

thomasklemm Feb 8, 2013

Hey Mike, what's your reasoning behind disconnecting and reconnecting? Thanks in advance! Thomas

Hey Mike, what's your reasoning behind disconnecting and reconnecting? Thanks in advance! Thomas

@BuddyE

This comment has been minimized.

Show comment Hide comment
@BuddyE

BuddyE Feb 14, 2013

@juniorz @thomasklemm You can't pass the same connection across a fork, need puma to re/connect on fork.

BuddyE commented Feb 14, 2013

@juniorz @thomasklemm You can't pass the same connection across a fork, need puma to re/connect on fork.

@divoxx

This comment has been minimized.

Show comment Hide comment
@divoxx

divoxx Feb 21, 2013

I don't think Puma forks at all, it uses thread. Also, the config file will run when puma reads the configuration file and not after creating a thread (or a fork).

divoxx commented Feb 21, 2013

I don't think Puma forks at all, it uses thread. Also, the config file will run when puma reads the configuration file and not after creating a thread (or a fork).

@jmonteiro

This comment has been minimized.

Show comment Hide comment
@jmonteiro

jmonteiro Feb 26, 2013

@divoxx is right. According to Puma's README:

Puma then serves the request in a thread from an internal thread pool (which you can control). This allows Puma to provide real concurrency for your web application!

There's no need to use that config/puma.rb config file. And AFAIK, on Rails 4 (and maybe also on 3.2), if an ENV['DATABASE_URL'], it won't need config/database.yml.

Curious about what a Puma config file is about? Check https://github.com/puma/puma/tree/master/examples for some examples.

@divoxx is right. According to Puma's README:

Puma then serves the request in a thread from an internal thread pool (which you can control). This allows Puma to provide real concurrency for your web application!

There's no need to use that config/puma.rb config file. And AFAIK, on Rails 4 (and maybe also on 3.2), if an ENV['DATABASE_URL'], it won't need config/database.yml.

Curious about what a Puma config file is about? Check https://github.com/puma/puma/tree/master/examples for some examples.

@mpoisot

This comment has been minimized.

Show comment Hide comment
@mpoisot

mpoisot Mar 6, 2013

heroku config:add NEWRELIC_DISPATCHER=Puma will make New Relic think ALL Heroku processes are Pumas, including workers and one-off heroku run rails c.

I know Puma 2 makes this setting unnecessary, but it's still in beta so I'm on the latest 1x (1.6.3).

Does anybody know how to detect what Heroku PS type was invoked? Or some other way to tell if I'm running on a web dyno or not? I figure I can use that knowledge in an initializer to only set ENV[NEWRELIC_DISPATCHER]=Puma for web dynos.

mpoisot commented Mar 6, 2013

heroku config:add NEWRELIC_DISPATCHER=Puma will make New Relic think ALL Heroku processes are Pumas, including workers and one-off heroku run rails c.

I know Puma 2 makes this setting unnecessary, but it's still in beta so I'm on the latest 1x (1.6.3).

Does anybody know how to detect what Heroku PS type was invoked? Or some other way to tell if I'm running on a web dyno or not? I figure I can use that knowledge in an initializer to only set ENV[NEWRELIC_DISPATCHER]=Puma for web dynos.

@thomasklemm

This comment has been minimized.

Show comment Hide comment
@thomasklemm

thomasklemm Apr 30, 2013

An alternative approach that helps increase AR's connection pool size on Heroku has been published in the Heroku dev center.

An alternative approach that helps increase AR's connection pool size on Heroku has been published in the Heroku dev center.

@calasyr

This comment has been minimized.

Show comment Hide comment
@calasyr

calasyr May 11, 2013

config.threadsafe! is deprecated for rails 4:

"Rails applications behave by default as thread safe in production as long as config.cache_classes and config.eager_load are set to true."

calasyr commented May 11, 2013

config.threadsafe! is deprecated for rails 4:

"Rails applications behave by default as thread safe in production as long as config.cache_classes and config.eager_load are set to true."

@GantMan

This comment has been minimized.

Show comment Hide comment
@GantMan

GantMan Jul 8, 2013

ActiveRecord::Base.verify_active_connections! is also deprecated, and should be removed.

GantMan commented Jul 8, 2013

ActiveRecord::Base.verify_active_connections! is also deprecated, and should be removed.

@subelsky

This comment has been minimized.

Show comment Hide comment
@subelsky

subelsky Jul 14, 2013

yeah you don't need a puma.rb file. That was vestigial from when I was on unicorn, and I thought had read somewhere that puma needed it to be able to restart cleanly

Owner

subelsky commented Jul 14, 2013

yeah you don't need a puma.rb file. That was vestigial from when I was on unicorn, and I thought had read somewhere that puma needed it to be able to restart cleanly

@subelsky

This comment has been minimized.

Show comment Hide comment
@subelsky

subelsky Jul 14, 2013

Owner

subelsky commented Jul 14, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment