unicorn: clear environment variables during hot restarts.

Fixes an issue with zero downtime deployments (#4123). Hot restarting
didn't clear out BUNDLER_GEMFILE, which mean that new workers still used
the old Gemfile from previous deployments. This led to mysterious errors
with autoloading libraries (#4138).
This commit is contained in:
evazion
2019-08-15 00:36:25 -05:00
parent 5f1226ca92
commit 13dbdd5cd9

View File

@@ -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