简体   繁体   English

Rails 6 和 Puma 是否仍需要在工作启动时建立连接?

[英]Is establish_connection on worker boot still required on Rails 6 and Puma?

I read on Heroku that for Rails (4+ they say) I should add the following directive to Puma, when I use multiple workers and I preload the app:我在 Heroku 上读到,对于 Rails(他们说 4+),当我使用多个工作人员并预加载应用程序时,我应该向 Puma 添加以下指令:

on_worker_boot do
  ActiveRecord::Base.establish_connection
end

However I have both preload_app!但是我有两个preload_app! and multiple workers without that code... and the app seems to work fine.和多个没有该代码的workers ......并且该应用程序似乎运行良好。

Am I missing something?我错过了什么吗? Is that still required?这还需要吗? What is the purpose?什么目的?

This establish_connection call is no longer needed, assuming you're running Rails 5.2+.假设您正在运行 Rails 5.2+,则不再需要此establish_connection连接调用。

The config code for the Puma-maintained plugin for best practices Puma configuration on Heroku (it lives here ) currently includes the following: Puma 维护的最佳实践插件的配置代码 Heroku(它位于此处)上的 Puma 配置当前包括以下内容:

# 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

Note the comment here.注意这里的评论。 A little more digging on the linked PR and some PRs it linked me to led to the PR that actually removes the need for this stuff: https://github.com/rails/rails/pull/31241 .对链接的 PR 和它链接我的一些 PR 进行更多挖掘,导致 PR 实际上消除了对这些东西的需求: https://github.com/rails/rails/pull/31241 In this PR, the question is explicitly asked,在这个 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?这是否意味着如果我们在我们的 pre-Rails 5.2.0 puma.rb 文件中有现有的 before_fork 和 on_worker_boot 块,我们现在可以在升级到 Rails 5.2.0 后简单地删除这些块?

And the answer, my friend, is...我的朋友,答案是……

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. (当然,假设他们正在做 AR 连接管理。)他们离开那里是完全安全的,所以升级指南中没有提到,但它们应该不再是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM