Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, Mongoid & Unicorn config for Heroku

I am using Mongoid 3, with Rails 3.2.9 and Unicorn for production. Would like to setup a before_fork & after_fork for the connection to mongodb, found the following code for active record:

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 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 end 

What is the relevant code for Mongoid (to connect and disconnect)?

Update:

You dont actually need to do this, so for people coming to view this question see:

http://mongoid.org/en/mongoid/docs/rails.html

"Unicorn and Passenger

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."

Though it would still be interesting to know what would be the equivalent Mongoid code.

like image 283
ismail Avatar asked Feb 27 '13 12:02

ismail


People also ask

What is Mongoid document?

Documents are the core objects in Mongoid and any object that is to be persisted to the database must include Mongoid::Document . The representation of a Document in MongoDB is a BSON object that is very similar to a Ruby hash or JSON object.

Can we use MongoDB with rails?

Read More: How to install and use MongoDB with Rails 6Create a new rails application to use Ruby MongoDB. Make sure that you add –skip-active-record. If you notice, there is no database. yml and no sqlite3 gem is added automatically.


1 Answers

You dont actually need to do this, so for people coming to view this question see:

http://mongoid.org/en/mongoid/docs/rails.html

"Unicorn and Passenger

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."

Though it would still be interesting to know what would be the equivalent Mongoid code.

like image 88
ismail Avatar answered Sep 19 '22 09:09

ismail