Skip to content

Instantly share code, notes, and snippets.

@pcreux pcreux/Gemfile
Last active Jun 25, 2019

Embed
What would you like to do?
Fast Rails + Heroku Configuration

Free addons!

heroku addons:add memcachier
heroku addons:add mandrill:starter
heroku addons:add newrelic
heroku addons:add pgbackups:auto-month
heroku addons:add papertrail
group :production do
gem 'unicorn'
# Enable gzip compression on heroku, but don't compress images.
gem 'heroku-deflater'
# Heroku injects it if it's not in there already
gem 'rails_12factor'
end
gem 'memcachier'
gem 'dalli'
# Fast IO for memcache
gem 'kgio'
# Serve static assets through Rack + Memcache
# https://devcenter.heroku.com/articles/rack-cache-memcached-rails31
gem 'rack-cache'
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
# config/environments/production.rb
# ...
# Enable Rails's static asset server for Heroku
config.serve_static_assets = true
# Set static assets cache header. rack-cache will cache those.
config.static_cache_control = "public, max-age=31536000"
config.cache_store = :dalli_store
client = Dalli::Client.new(ENV["MEMCACHIER_SERVERS"],
:value_max_bytes => 10485760,
:expires_in => 86400) # 1 day
# Configure rack-cache for using memcachier
config.action_dispatch.rack_cache = {
:metastore => client,
:entitystore => client
}
# Send emails via mandrill
ActionMailer::Base.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.mandrillapp.com',
:port => '587',
:authentication => :plain,
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_APIKEY'],
:domain => 'heroku.com',
}
# ...
# config/unicorn.rb
worker_processes 3
timeout 30
preload_app true
before_fork do |server, worker|
# Replace with MongoDB or whatever
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
Rails.logger.info('Disconnected from ActiveRecord')
end
# If you are using Redis but not Resque, change this
if defined?(Resque)
Resque.redis.quit
Rails.logger.info('Disconnected from Redis')
end
sleep 1
end
after_fork do |server, worker|
# Replace with MongoDB or whatever
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
Rails.logger.info('Connected to ActiveRecord')
end
# If you are using Redis but not Resque, change this
if defined?(Resque)
Resque.redis = ENV['REDIS_URI']
Rails.logger.info('Connected to Redis')
end
end
@antoinelyset

This comment has been minimized.

Copy link

antoinelyset commented Nov 27, 2013

Awesome thanks !

@EtienneDepaulis

This comment has been minimized.

Copy link

EtienneDepaulis commented Nov 27, 2013

Some suggestions:

  • sucker_punch (to avoir using workers for just emails)
  • add a comment on new relic regarding the monitoring feature to keep the app online without using external services
@boriscy

This comment has been minimized.

Copy link

boriscy commented Nov 28, 2013

sucker_punch is great but one must be really carefull if you aren't using ruby 2.0.

@mathieugagne

This comment has been minimized.

Copy link

mathieugagne commented Nov 29, 2013

Can't precompile assets: undefined method day' for 1:Fixnum expires_in` takes the "default TTL in seconds if you do not pass TTL as a parameter to an individual operation, defaults to 0 or forever" [source: http://www.rubydoc.info/github/mperham/dalli/Dalli/Client]
If it's 1 day you want then value should be 86400

client = Dalli::Client.new(ENV["MEMCACHIER_SERVERS"],
                             :value_max_bytes => 10485760,
                             :expires_in => 86400)
@croaky

This comment has been minimized.

Copy link

croaky commented Nov 30, 2013

These settings, minus Memcache, are in Suspenders. I prefer fronting my Heroku apps with Fastly to Memcache for the fastest static asset serving that only hits the Heroku dynos once per cache via origin pull.

@maurogeorge

This comment has been minimized.

Copy link

maurogeorge commented Dec 2, 2013

You can change the Sendgrid by the Mandrill. In the free plan, they have the double of emails.

@pcreux

This comment has been minimized.

Copy link
Owner Author

pcreux commented Feb 22, 2014

@mathieugagne Thanks! I just fixed it!

@pcreux

This comment has been minimized.

Copy link
Owner Author

pcreux commented Feb 22, 2014

@maurogeorge Done!

@pcreux

This comment has been minimized.

Copy link
Owner Author

pcreux commented Feb 22, 2014

@croaky Fastly charges a minimum of $50 a month right?

@chuckbergeron

This comment has been minimized.

Copy link

chuckbergeron commented Jan 8, 2015

Could use a gem 'newrelic' line in the Gemfile. I added the addon but missed the gem, and was wondering why Newrelic was "waiting on data" 😁

@jesuslerma

This comment has been minimized.

Copy link

jesuslerma commented Jan 28, 2015

do u need to have workers?

@chuckbergeron

This comment has been minimized.

Copy link

chuckbergeron commented May 19, 2015

The command is now heroku addons:create instead of heroku addons:add

Also, heroku addons:add pgbackups:auto-month doesn't exist. The only postgres backup system I see now is heroku addons:create autobus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.