Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unicorn config for cedar stack on Heroku.
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
# config/unicorn.rb
# See comment by @paulelliott
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
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
@paulelliott

This comment has been minimized.

Copy link

commented Nov 28, 2011

You need to be careful with 4 workers. We were running out of memory on our dynos and it is something that you wouldn't be alerted to in Airbrake or the like. Bumped it down to 3 and everything has been fine.

@tispratik

This comment has been minimized.

Copy link

commented Dec 25, 2011

Thanks for sharing the unicorn config file for heroku! Can setting preload_app = true cause a lot of problems?

@leshill

This comment has been minimized.

Copy link
Owner Author

commented Dec 25, 2011

Hi Pratik,

Not sure how to answer this, but when using preload_app = true, unless you have an unusual app, you are going to have to have the before_fork and after_fork blocks. If those are not cleaning up the app correctly, for example ActiveRecord is cleanup up above, then you will have problems.

Hope that helps,

@Vibhuti

This comment has been minimized.

Copy link

commented Dec 25, 2011

Thanks! So, problem will occur only if the app is not cleaning up.

@sickill

This comment has been minimized.

Copy link

commented Jun 9, 2012

Is the sleep in before_fork necessary?

@leshill

This comment has been minimized.

Copy link
Owner Author

commented Jun 9, 2012

Oops, no :) Edited!

@walidhalabi

This comment has been minimized.

Copy link

commented Aug 14, 2012

Just because I googled this for ages and browsed source code:

If using Mongoid, you don't need to manually reconnect as in the gist.

Mongoid claims: "When using Unicorn or Passenger, each time a child process is forked when using app preloading or smart spawning, Mongoid will automatically reconnect to the master database. If you are doing this in your application manually you may remove your code."

@cloud-on-prem

This comment has been minimized.

Copy link

commented Oct 3, 2012

@walidhalabi, Is this true for Mongoid 2.x as well? I couldn't find any supporting documentation. Thanks.

@etiennepeiniau

This comment has been minimized.

Copy link

commented Nov 4, 2012

@premjg, you can find the same documentation for Mongoid 2.x, here : http://two.mongoid.org/docs/rails/railties.html

@tibbon

This comment has been minimized.

Copy link

commented Jan 1, 2013

What would it look like if you were using Mongo, Postgres/ActiveRecord AND Resque? I'm having weird EOF issues with Resque/Postgres and Heroku, and I'm wondering if my config here is part of the issue.

@harlow

This comment has been minimized.

Copy link

commented Mar 16, 2013

@leshill, there is a comment # If you are using Redis but not Resque, change this, what should it be changed to if you are just using Redis?

My thought was it would look something like the below text. However, the Redis.current.quit throws errors because .current tries to connect to the default localhost (which doesn't exist if using an external Redis server).

before_fork do |server, worker|
  if defined?(Redis)
    # this throws errors
    # Redis.current tries to connect localhost
    Redis.current.quit
  end
end

after_fork do |server, worker|
  if defined?(Redis)
    Redis.current = Redis.new(url: ENV['REDIS_URI'])
  end
end
@josh803316

This comment has been minimized.

Copy link

commented Apr 7, 2013

@harlow Did you figure the Redis config out. Im using Redistogo without Resque and can't find much in the way of configs like that.

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.