Heroku的Rails、Mongoid和Unicorn配置

31

我正在使用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

"独角兽和乘客"

当使用独角兽或乘客时,在使用应用程序预加载或智能生成子进程时,每次fork子进程时,Mongoid将自动重新连接到主数据库。如果您手动在应用程序中执行此操作,则可以删除您的代码。

尽管知道相应的Mongoid代码仍然很有趣。


请将您的更新放在下面的答案中并接受它 :) - gef
mongoid的链接已经失效,但是这个链接仍然存在:https://mongoid.github.io/old/en/mongoid/docs/rails.html(我无法编辑答案,所以我把它放在了评论中)。 - campisano
3个回答

19

不需要这样做,所以对于查看此问题的人,请参见:

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

"Unicorn和Passenger

当使用Unicorn或Passenger时,每次使用应用程序预加载或智能派生时fork子进程,Mongoid将自动重新连接到主数据库。如果您在应用程序中手动执行此操作,则可以删除代码。"

尽管了解等效的Mongoid代码仍然很有趣。


似乎这在某个时候发生了变化,请参见 https://docs.mongodb.com/mongoid/current/tutorials/mongoid-configuration/#usage-with-forking-servers。您能否请您修改您的回答? - kaikuchn

0

0

关于 ::Mongoid.default_session.connect ::Mongoid.default_session.disconnect


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接