A platform for community discussion. Free, open, simple.
FIX: Theme site settings not reloading across processes (#34242)
Martin Brennan470b2891c537
diff --git a/app/models/remote_theme.rb b/app/models/remote_theme.rbindex ecfe4d13d43..cd030d4961a 100644--- a/app/models/remote_theme.rb+++ b/app/models/remote_theme.rb@@ -484,7 +484,7 @@ class RemoteTheme < ActiveRecord::Basevalue: value,},guardian: Discourse.system_user.guardian,- ) { |result| }+ )endSiteSetting.refresh!(refresh_site_settings: false, refresh_theme_site_settings: true)diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rbindex ae90df2c9e9..0ef9b1a15ab 100644--- a/lib/site_setting_extension.rb+++ b/lib/site_setting_extension.rb@@ -177,7 +177,7 @@ module SiteSettingExtensionenddef setting_metadata_hash(setting)- setting_hash = {+ {setting:,default: SiteSetting.defaults[setting],description: SiteSetting.description(setting),@@ -433,6 +433,7 @@ module SiteSettingExtensiontheme_site_setting_changes, theme_site_setting_deletions =diff_hash(new_theme_site_settings, theme_site_settings)+theme_site_setting_changes.each do |theme_id, settings|theme_site_settings[theme_id] ||= {}theme_site_settings[theme_id].merge!(settings)@@ -599,6 +600,7 @@ module SiteSettingExtensiontheme_site_settings[theme_id][name] = valnotify_clients!(name, theme_id: theme_id) if client_settings.include?(name)+ notify_changed!clear_cache!(expire_theme_site_setting_cache: true)diff --git a/lib/site_settings/local_process_provider.rb b/lib/site_settings/local_process_provider.rbindex 5e6f7993f37..548a35a0e8f 100644--- a/lib/site_settings/local_process_provider.rb+++ b/lib/site_settings/local_process_provider.rb@@ -21,6 +21,8 @@ class SiteSettings::LocalProcessProviderendend+ attr_accessor :model+def settings@settings[current_site] ||= {}end@@ -46,6 +48,11 @@ class SiteSettings::LocalProcessProvidersettings[name] = settingendsetting.value = value.to_s++ # So we can simulate the MessageBus notifications+ # done by the DbProvider+ @model.notify_changed! if @model+settingenddiff --git a/spec/lib/site_setting_extension_spec.rb b/spec/lib/site_setting_extension_spec.rbindex e626fd69518..c345fbc9ce4 100644--- a/spec/lib/site_setting_extension_spec.rb+++ b/spec/lib/site_setting_extension_spec.rb@@ -41,7 +41,13 @@ RSpec.describe SiteSettingExtension donew_settings(provider_local)end- it "Does not leak state cause changes are not linked" do+ after do+ settings.provider.model = nil+ settings.listen_for_changes = false+ settings2.listen_for_changes = false+ end++ it "does not leak state cause changes are not linked" dot1 =Thread.new do5.times do@@ -89,7 +95,7 @@ RSpec.describe SiteSettingExtension doexpect(settings.hello).to eq(99)end- it "publishes changes cross sites" do+ it "picks up changes from provider on refresh across processes" dosettings.setting(:hello, 1)settings2.setting(:hello, 1)@@ -104,6 +110,30 @@ RSpec.describe SiteSettingExtension doexpect(settings2.hello).to eq(99)end+ it "publishes changes across processes" do+ MessageBus.on++ # Only need to do this once, since both settings will use the same provider. Doing it twice leads to confusion with MessageBus+ settings.provider.model = settings++ settings.listen_for_changes = true+ settings2.listen_for_changes = true++ settings.refresh!+ settings2.refresh!++ settings.setting(:hello, 1)+ settings2.setting(:hello, 1)++ settings.hello = 100++ expect(settings2.hello).to eq(100)++ settings.hello = 99++ expect(settings2.hello).to eq(99)+ end+it "does not override types in the type supervisor" dosettings.setting(:foo, "bar")settings.provider.save(:foo, "bar", SiteSetting.types[:enum])@@ -1048,6 +1078,38 @@ RSpec.describe SiteSettingExtension doexpect(SiteSetting.search_experience(theme_id: theme_2.id)).to eq("search_field")end+ it "publishes changes across processes" do+ MessageBus.on++ settings_tss_instance_1 = new_settings(provider_local)+ settings_tss_instance_2 = new_settings(provider_local)++ settings_tss_instance_1.listen_for_changes = true+ settings_tss_instance_2.listen_for_changes = true++ settings_tss_instance_1.load_settings(File.join(Rails.root, "config", "site_settings.yml"))+ settings_tss_instance_2.load_settings(File.join(Rails.root, "config", "site_settings.yml"))++ settings_tss_instance_1.refresh!+ settings_tss_instance_2.refresh!++ expect(settings_tss_instance_1.enable_welcome_banner(theme_id: theme_1.id)).to eq(false)+ expect(settings_tss_instance_2.enable_welcome_banner(theme_id: theme_1.id)).to eq(false)++ tss_1.update!(value: true)+ settings_tss_instance_1.change_themeable_site_setting(+ theme_1.id,+ :enable_welcome_banner,+ true,+ )++ # Get through the MessageBus queue+ try_until_success(frequency: 0.5) do+ expect(settings_tss_instance_1.enable_welcome_banner(theme_id: theme_1.id)).to eq(true)+ expect(settings_tss_instance_2.enable_welcome_banner(theme_id: theme_1.id)).to eq(true)+ end+ end+describe ".theme_site_settings_json_uncached" doit "returns the correct JSON" doSiteSetting.refresh!diff --git a/spec/support/helpers.rb b/spec/support/helpers.rbindex f693b89b522..9aca8157781 100644--- a/spec/support/helpers.rb+++ b/spec/support/helpers.rb@@ -318,6 +318,17 @@ module HelpersSiteSetting.public_send("#{plugin.enabled_site_setting}=", true)end+ def try_until_success(timeout: 3, frequency: 0.01)+ start ||= Time.zone.now+ backoff ||= frequency+ yield+ rescue RSpec::Expectations::ExpectationNotMetError+ raise if Time.zone.now >= start + timeout.seconds+ sleep backoff+ backoff += frequency+ retry+ end+privatedef directory_from_caller