diff --git a/config/unicorn/production.rb b/config/unicorn/production.rb index b88aa8177..e4fe76330 100644 --- a/config/unicorn/production.rb +++ b/config/unicorn/production.rb @@ -81,3 +81,16 @@ after_fork do |server, worker| ActiveRecord::Base.establish_connection end end + +# This runs when we send unicorn a SIGUSR2 to do a hot restart. We need to +# clear out old BUNDLER_* and GEM_* environment variables, otherwise the new +# worker will still use the old Gemfile from the previous deployment, which +# will cause mysterious problems with libraries. BUNDLER_GEMFILE is the main +# thing we need to clear, but we wipe everything for safety. +# +# https://bogomips.org/unicorn/Sandbox.html +# https://jamielinux.com/blog/zero-downtime-unicorn-restart-when-using-rbenv/ +before_exec do |server| + ENV.keep_if { |name, value| name.match?(/\A(RAILS_.*|UNICORN_.*|HOME)\z/) } + ENV["PATH"] = "#{ENV["HOME"]}/.rbenv/shims:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" +end