Skip to content
This repository was archived by the owner on Dec 31, 2022. It is now read-only.

Commit db17de6

Browse files
committed
feat(appserver): re-establish database connections when preloading app
More info: https://www.speedshop.co/2017/10/12/appserver.html#copy-on-write-behavior Resolves #198
1 parent 27bad91 commit db17de6

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

templates/default/puma.rb.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ directory "<%= @deploy_dir %>/current"
55

66
<% if @out[:preload_app] %>
77
preload_app!
8+
on_worker_boot do
9+
next unless defined?(ActiveRecord::Base) && defined?(Rails)
10+
next unless Gem::Requirement.new('>= 4.1')
11+
.satisfied_by?(Gem::Version.new(Rails.version))
12+
ActiveRecord::Base.establish_connection
13+
end
814
<% end %>
915

1016
# Set the environment in which the rack's app will run. The value must be a string.

templates/default/unicorn.conf.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ stderr_path "<%= @deploy_dir %>/shared/log/unicorn.stderr.log"
2424
stdout_path "<%= @deploy_dir %>/shared/log/unicorn.stdout.log"
2525

2626
preload_app <%= @out[:preload_app] %>
27+
<% if @out[:preload_app] %>
28+
after_fork do |_server, _worker|
29+
next unless defined?(ActiveRecord::Base) && defined?(Rails)
30+
next unless Gem::Requirement.new('>= 4.1')
31+
.satisfied_by?(Gem::Version.new(Rails.version))
32+
ActiveRecord::Base.establish_connection
33+
end
34+
<% end %>
2735
GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=)
2836

2937
# ensure Unicorn doesn't use a stale Gemfile when restarting

0 commit comments

Comments
 (0)