# 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 |
This comment has been minimized.
This comment has been minimized.
Thanks for sharing the unicorn config file for heroku! Can setting preload_app = true cause a lot of problems? |
This comment has been minimized.
This comment has been minimized.
Hi Pratik, Not sure how to answer this, but when using Hope that helps, |
This comment has been minimized.
This comment has been minimized.
Thanks! So, problem will occur only if the app is not cleaning up. |
This comment has been minimized.
This comment has been minimized.
Is the |
This comment has been minimized.
This comment has been minimized.
Oops, no :) Edited! |
This comment has been minimized.
This comment has been minimized.
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." |
This comment has been minimized.
This comment has been minimized.
@walidhalabi, Is this true for Mongoid 2.x as well? I couldn't find any supporting documentation. Thanks. |
This comment has been minimized.
This comment has been minimized.
@premjg, you can find the same documentation for Mongoid 2.x, here : http://two.mongoid.org/docs/rails/railties.html |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
@leshill, there is a comment My thought was it would look something like the below text. However, the 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 |
This comment has been minimized.
This comment has been minimized.
@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. |
This comment has been minimized.
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.