diff --git a/config/initializers/01-redis.rb b/config/initializers/01-redis.rb index b4997795f..7fddf4304 100644 --- a/config/initializers/01-redis.rb +++ b/config/initializers/01-redis.rb @@ -10,10 +10,7 @@ end if defined?(PhusionPassenger) PhusionPassenger.on_event(:starting_worker_process) do |forked| if forked - # We're in smart spawning mode. - $redis = DiscourseRedis.new - Discourse::Application.config.cache_store.reconnect - MessageBus.after_fork + Discourse.after_fork else # We're in conservative spawning mode. We don't need to do anything. end diff --git a/config/spring.rb b/config/spring.rb index 1db1fcbc3..4e5ef1d97 100644 --- a/config/spring.rb +++ b/config/spring.rb @@ -6,8 +6,6 @@ # spring binstub rake # spring binstub rspec Spring.after_fork do - $redis.client.reconnect - Rails.cache.reconnect - MessageBus.after_fork + Discourse.after_fork end Spring::Commands::Rake.environment_matchers["spec"] = "test" diff --git a/config/unicorn.conf.rb b/config/unicorn.conf.rb index 5367c46e8..daf90a46e 100644 --- a/config/unicorn.conf.rb +++ b/config/unicorn.conf.rb @@ -83,8 +83,5 @@ before_fork do |server, worker| end after_fork do |server, worker| - ActiveRecord::Base.establish_connection - $redis.client.reconnect - Rails.cache.reconnect - MessageBus.after_fork + Discourse.after_fork end diff --git a/lib/backup_restore.rb b/lib/backup_restore.rb index 27a326751..6ce219f1a 100644 --- a/lib/backup_restore.rb +++ b/lib/backup_restore.rb @@ -174,14 +174,7 @@ module BackupRestore end def self.after_fork - # reconnect to redis - $redis.client.reconnect - # reconnect the rails cache (uses redis) - Rails.cache.reconnect - # tells the message we've forked - MessageBus.after_fork - # /!\ HACK /!\ force sidekiq to create a new connection to redis - Sidekiq.instance_variable_set(:@redis, nil) + Discourse.after_fork end def self.backup_tables_count diff --git a/lib/demon/base.rb b/lib/demon/base.rb index ad059c887..d9b2f093c 100644 --- a/lib/demon/base.rb +++ b/lib/demon/base.rb @@ -118,11 +118,7 @@ class Demon::Base end def establish_app - ActiveRecord::Base.connection_handler.clear_active_connections! - ActiveRecord::Base.establish_connection - $redis.client.reconnect - Rails.cache.reconnect - MessageBus.after_fork + Discourse.after_fork Signal.trap("HUP") do begin diff --git a/lib/discourse.rb b/lib/discourse.rb index bcc992c62..01704e3d4 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -234,4 +234,17 @@ module Discourse "/site/read-only" end + # all forking servers must call this + # after fork, otherwise Discourse will be + # in a bad state + def self.after_fork + SiteSetting.after_fork + ActiveRecord::Base.establish_connection + $redis.client.reconnect + Rails.cache.reconnect + MessageBus.after_fork + # /!\ HACK /!\ force sidekiq to create a new connection to redis + Sidekiq.instance_variable_set(:@redis, nil) + end + end diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb index d42d615b3..e5216f1ac 100644 --- a/lib/site_setting_extension.rb +++ b/lib/site_setting_extension.rb @@ -179,6 +179,10 @@ module SiteSettingExtension @@process_id ||= SecureRandom.uuid end + def after_fork + @@process_id = nil + end + def remove_override!(name) provider.destroy(name) current[name] = defaults[name] diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 045e88a39..950e0dd8d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -98,9 +98,7 @@ end Spork.each_run do # This code will be run each time you run your specs. - $redis.client.reconnect - Rails.cache.reconnect - MessageBus.after_fork + Discourse.after_fork end # --- Instructions ---