Skip to content

A platform for community discussion. Free, open, simple.

DEV: Support per-unicorn-worker DB variable overrides (#35615)

Alan Guo Xiang Tan45204e5c553e
diff --git a/config/database.yml b/config/database.yml
index 9885fd883d9..1b6642f4b9f 100644
--- a/config/database.yml
+++ b/config/database.yml
@@ -1,3 +1,10 @@
+<%
+ db_variables =
+ ENV
+ .filter { |key, _| key.start_with?("DISCOURSE_DB_VARIABLES_") }
+ .transform_keys { |key| key.slice(("DISCOURSE_DB_VARIABLES_".length)..).downcase }
+%>
+
development:
prepared_statements: false
adapter: postgresql
@@ -11,9 +18,7 @@ development:
### - restart sidekiq if you change this setting
### - rebake all to posts using: `RAILS_ENV=production bundle exec rake posts:rebake`
- "<%= ENV['DISCOURSE_HOSTNAME'] || 'localhost' %>"
- variables: <%= ENV.filter { |k,v| k.start_with? 'DISCOURSE_DB_VARIABLES_' }
- .transform_keys { |k| k.slice(('DISCOURSE_DB_VARIABLES_'.length)..).downcase }
- .to_json %>
+ variables: <%= db_variables.to_json %>
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
@@ -41,6 +46,7 @@ test:
checkout_timeout: <%= ENV["CHECKOUT_TIMEOUT"] || 5 %>
host_names:
- test.localhost
+ variables: <%= db_variables.to_json %>
# profile db is used for benchmarking using the script/bench.rb script
profile:
diff --git a/config/unicorn.conf.rb b/config/unicorn.conf.rb
index fd2bf2ae0e9..d3fc43c3737 100644
--- a/config/unicorn.conf.rb
+++ b/config/unicorn.conf.rb
@@ -194,6 +194,7 @@ end
after_fork do |server, worker|
DiscourseEvent.trigger(:web_fork_started)
+ Discourse.after_unicorn_worker_fork
Discourse.after_fork
SignalTrapLogger.instance.after_fork
diff --git a/lib/discourse.rb b/lib/discourse.rb
index 55105fa003b..27abbd1815c 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -934,6 +934,26 @@ module Discourse
Process.warmup
end
+ def self.after_unicorn_worker_fork
+ unicorn_worker_db_variables_prefix = "DISCOURSE_UNICORN_WORKER_DB_VARIABLES_"
+ db_variables_overrides = {}
+
+ ENV.filter do |key, value|
+ if key.start_with?(unicorn_worker_db_variables_prefix)
+ db_variables_overrides[
+ "DISCOURSE_DB_VARIABLES_#{key.sub(unicorn_worker_db_variables_prefix, "")}"
+ ] = value
+ end
+ end
+
+ if db_variables_overrides.any?
+ db_variables_overrides.each { |key, value| ENV[key] = value }
+ ActiveRecord::Base.configurations = Rails.application.config.database_configuration
+ ActiveRecord::Base.connection_handler.clear_all_connections!(:all)
+ ActiveRecord::Base.establish_connection
+ end
+ end
+
# all forking servers must call this
# after fork, otherwise Discourse will be
# in a bad state
diff --git a/spec/lib/discourse_spec.rb b/spec/lib/discourse_spec.rb
index 60412180487..17d16744372 100644
--- a/spec/lib/discourse_spec.rb
+++ b/spec/lib/discourse_spec.rb
@@ -67,6 +67,41 @@ RSpec.describe Discourse do
end
end
+ describe ".after_unicorn_worker_fork" do
+ around do |example|
+ original_env = ENV.to_hash
+ original_config = ActiveRecord::Base.configurations
+ original_show_statement_timeout =
+ ActiveRecord::Base.connection.execute("SHOW statement_timeout").first["statement_timeout"]
+
+ begin
+ example.run
+ ensure
+ ENV.replace(original_env)
+ ActiveRecord::Base.configurations = original_config
+ ActiveRecord::Base.connection_handler.clear_all_connections!(:all)
+ ActiveRecord::Base.establish_connection
+
+ expect(
+ ActiveRecord::Base.connection.execute("SHOW statement_timeout").first[
+ "statement_timeout"
+ ],
+ ).to eq(original_show_statement_timeout)
+ end
+ end
+
+ it "applies worker-specific database variable overrides" do
+ ENV["DISCOURSE_DB_VARIABLES_STATEMENT_TIMEOUT"] = "5s"
+ ENV["DISCOURSE_UNICORN_WORKER_DB_VARIABLES_STATEMENT_TIMEOUT"] = "100s"
+
+ Discourse.after_unicorn_worker_fork
+
+ expect(
+ ActiveRecord::Base.connection.execute("SHOW statement_timeout").first["statement_timeout"],
+ ).to eq("100s")
+ end
+ end
+
describe ".plugins_sorted_by_name" do
before do
Discourse.stubs(:visible_plugins).returns(