Ruby on rails Puma正在重新加载Rails应用程序代码,然后在生产环境中重新启动
在部署过程中注意到奇怪的异常后(我们注意到在发布puma restart之前,依赖于部署过程中仍在运行的迁移的新代码已经提供),我们可以在通过Ruby on rails Puma正在重新加载Rails应用程序代码,然后在生产环境中重新启动,ruby-on-rails,puma,Ruby On Rails,Puma,在部署过程中注意到奇怪的异常后(我们注意到在发布puma restart之前,依赖于部署过程中仍在运行的迁移的新代码已经提供),我们可以在通过git pull更新代码库时确认puma正在重新加载应用程序代码,即使我们在生产环境中运行,Puma也应该等待重启发布 更新:下面是一段1分钟的短片,介绍正在发生的事情: 操作系统:Ubuntu 18.04 LTS Puma版本4.3.0 Puma配置: shared_dir = "/var/www/app/shared" workers 25 thr
git pull
更新代码库时确认puma正在重新加载应用程序代码,即使我们在生产环境中运行,Puma也应该等待重启发布
更新:下面是一段1分钟的短片,介绍正在发生的事情:
操作系统:Ubuntu 18.04 LTS
Puma版本4.3.0
Puma配置:
shared_dir = "/var/www/app/shared"
workers 25
threads 1, 1
bind "unix://#{shared_dir}/tmp/sockets/puma-app.sock"
environment "production"
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true
pidfile "#{shared_dir}/tmp/pids/puma.pid"
preload_app!
before_fork do
ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
end
on_worker_boot do
ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
end
Puma插座(我们正在使用):
预期行为-Puma不应在我们使用git pull更新应用程序的代码库后立即开始提供新代码。它应该等待我们发出sudo systemctl restart puma-app.service
如果我们在应用程序的代码库文件夹中手动运行git pull
,我们可以确认请求立即开始看到新代码,即使没有发出puma重新启动,puma.stdout.log中也没有指示puma重新启动
更新:这是我的Rails application.rb和production.rb
# application.rb
# Enables garbage colletion data for New Relic
GC::Profiler.enable
require_relative 'boot'
require 'rails/all'
require 'connection_pool'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module MyApp
class Application < Rails::Application
config.before_initialize do
::REDIS = ConnectionPool.new(size: ENV['WORKER_PROCESS'].to_i) { Redis.new }
end
config.autoload_paths += %W(
#{config.root}/lib
#{config.root}/lib/middleware
)
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = 'Brasilia'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :'pt-br'
# dynamic and custom error pages
config.exceptions_app = self.routes
# ActiveJob
config.active_job.queue_adapter = :sidekiq
end
end
根据更改的位置(例如,可能是延迟加载且当时未加载的路径的一部分,可能特定情况下的自动加载/缓存无法按预期工作…)以及您在
env/production.rb
或application.rb
中配置rails的方式,Puma或其他rails服务器即使不重新启动也可以“接收”您的更改
为了安全起见,有时您可能希望拆分对迁移的更改,然后编写使用这些更改的代码(即,有两步展开)
编辑:
为了确定发生了什么,您可以执行以下操作:
# does below command returns a list containing use ActionDispatch::Reloader ?
bundle exec rake middleware RAILS_ENV=production
我们最近刚从独角兽迁移到美洲狮(大约2个月),我们非常依赖这种行为(非常有效)。我没有看到任何关于Puma这种行为的文档(无论它在哪里提到重新启动以获取新代码),我也无法想象我们在env/production或application.rb中的配置可能会导致这种情况。您能详细说明一下吗?您能从application.rb/production.rb共享与类缓存、自动加载路径等相关的配置吗。。。还有,具体的变化是什么?(你也可以在unicorn上尝试这个特定的更改,看看它是否有不同的行为)我用这两个文件更新了这个问题;在重新启动之前立即选择的具体更改是any view.html.erb文件。也刚刚更新了一个视频感谢视频-我已经添加了对答案的编辑
# production.rb
Rails.application.configure do
config.webpacker.check_yarn_integrity = false
# Code is not reloaded between requests.
config.cache_classes = true
# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
config.assets.compile = false
config.assets.digest = true
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
config.active_storage.service = :local
config.force_ssl = true
config.log_level = :info
Rails.application.routes.default_url_options = {host: 'example.com'}
config.action_mailer.asset_host = 'https://example.com'
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {...}
end
# does below command returns a list containing use ActionDispatch::Reloader ?
bundle exec rake middleware RAILS_ENV=production