【发布时间】:2013-02-13 05:58:28
【问题描述】:
我正在使用 Mongoid 3、Rails 3.2.9 和 Unicorn 进行生产。想设置一个 before_fork & after_fork 来连接 mongodb,发现活动记录的代码如下:
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
Mongoid(连接和断开)的相关代码是什么?
更新:
您实际上不需要这样做,因此对于查看此问题的人来说,请参阅:
http://mongoid.org/en/mongoid/docs/rails.html
“独角兽与乘客
在使用 Unicorn 或Passenger 时,每次在使用应用预加载或智能生成时分叉子进程时,Mongoid 都会自动重新连接到主数据库。如果您在应用程序中手动执行此操作,则可以删除您的代码。”
虽然知道什么是等效的 Mongoid 代码仍然很有趣。
【问题讨论】:
-
你应该把你的更新放在下面的答案中并接受它:)
-
mongoid 链接坏了,但这个仍然存在:mongoid.github.io/old/en/mongoid/docs/rails.html(我无法编辑答案,所以我把它放在评论中)
标签: ruby-on-rails mongodb heroku mongoid unicorn