Rails 6 和 Puma 是否仍然需要 establish_connection on worker boot?
Is establish_connection on worker boot still required on Rails 6 and Puma?
我在 Heroku 上读到,对于 Rails(他们说 4+),当我使用多个 worker 并预加载应用程序时,我应该向 Puma 添加以下指令:
on_worker_boot do
ActiveRecord::Base.establish_connection
end
但是我有两个 preload_app!
和多个 workers
没有那个代码...而且应用程序似乎工作正常。
我错过了什么吗?那还需要吗?目的是什么?
不再需要此 establish_connection
调用,假设您是 运行 Rails 5.2+。
Heroku 上用于最佳实践 Puma 配置的 Puma-maintained 插件的配置代码(它存在 here)目前包括以下内容:
# Not necessary in Rails 5.2+, see https://github.com/rails/rails/pull/29807
if defined?(::ActiveRecord) && defined?(::ActiveRecord::Base) && Gem::Version.new(Rails.version) < Gem::Version.new('5.2.0')
c.before_fork { ActiveRecord::Base.connection_pool.disconnect! }
c.on_worker_boot { ActiveRecord::Base.establish_connection }
end
注意这里的评论。对链接的 PR 和它链接到我的一些 PR 进行更多挖掘导致实际上消除了对这些东西的需要的 PR:https://github.com/rails/rails/pull/31241。在这个 PR 中,明确提出了这个问题,
Does this mean that if we have existing before_fork and on_worker_boot blocks in our pre-Rails 5.2.0 puma.rb file, we can now simply remove those blocks after upgrading to Rails 5.2.0?
朋友,答案是……
Yes. (Assuming they're doing AR connection management, of course.)
They're perfectly safe to leave there, so it's not called out in the upgrade guide, but they should no longer be necessary.
我在 Heroku 上读到,对于 Rails(他们说 4+),当我使用多个 worker 并预加载应用程序时,我应该向 Puma 添加以下指令:
on_worker_boot do
ActiveRecord::Base.establish_connection
end
但是我有两个 preload_app!
和多个 workers
没有那个代码...而且应用程序似乎工作正常。
我错过了什么吗?那还需要吗?目的是什么?
不再需要此 establish_connection
调用,假设您是 运行 Rails 5.2+。
Heroku 上用于最佳实践 Puma 配置的 Puma-maintained 插件的配置代码(它存在 here)目前包括以下内容:
# Not necessary in Rails 5.2+, see https://github.com/rails/rails/pull/29807
if defined?(::ActiveRecord) && defined?(::ActiveRecord::Base) && Gem::Version.new(Rails.version) < Gem::Version.new('5.2.0')
c.before_fork { ActiveRecord::Base.connection_pool.disconnect! }
c.on_worker_boot { ActiveRecord::Base.establish_connection }
end
注意这里的评论。对链接的 PR 和它链接到我的一些 PR 进行更多挖掘导致实际上消除了对这些东西的需要的 PR:https://github.com/rails/rails/pull/31241。在这个 PR 中,明确提出了这个问题,
Does this mean that if we have existing before_fork and on_worker_boot blocks in our pre-Rails 5.2.0 puma.rb file, we can now simply remove those blocks after upgrading to Rails 5.2.0?
朋友,答案是……
Yes. (Assuming they're doing AR connection management, of course.) They're perfectly safe to leave there, so it's not called out in the upgrade guide, but they should no longer be necessary.