BUGFIX: Correct after_fork semantics

After fork SiteSettings was not getting a new process id,
causing site settings not to refresh properly in unicorn

This code also centralizes the logic
This commit is contained in:
Sam 2014-03-28 13:48:14 +11:00
parent 956b14a4ab
commit f3cc7360e0
8 changed files with 23 additions and 27 deletions

View file

@ -10,10 +10,7 @@ end
if defined?(PhusionPassenger) if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked| PhusionPassenger.on_event(:starting_worker_process) do |forked|
if forked if forked
# We're in smart spawning mode. Discourse.after_fork
$redis = DiscourseRedis.new
Discourse::Application.config.cache_store.reconnect
MessageBus.after_fork
else else
# We're in conservative spawning mode. We don't need to do anything. # We're in conservative spawning mode. We don't need to do anything.
end end

View file

@ -6,8 +6,6 @@
# spring binstub rake # spring binstub rake
# spring binstub rspec # spring binstub rspec
Spring.after_fork do Spring.after_fork do
$redis.client.reconnect Discourse.after_fork
Rails.cache.reconnect
MessageBus.after_fork
end end
Spring::Commands::Rake.environment_matchers["spec"] = "test" Spring::Commands::Rake.environment_matchers["spec"] = "test"

View file

@ -83,8 +83,5 @@ before_fork do |server, worker|
end end
after_fork do |server, worker| after_fork do |server, worker|
ActiveRecord::Base.establish_connection Discourse.after_fork
$redis.client.reconnect
Rails.cache.reconnect
MessageBus.after_fork
end end

View file

@ -174,14 +174,7 @@ module BackupRestore
end end
def self.after_fork def self.after_fork
# reconnect to redis Discourse.after_fork
$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)
end end
def self.backup_tables_count def self.backup_tables_count

View file

@ -118,11 +118,7 @@ class Demon::Base
end end
def establish_app def establish_app
ActiveRecord::Base.connection_handler.clear_active_connections! Discourse.after_fork
ActiveRecord::Base.establish_connection
$redis.client.reconnect
Rails.cache.reconnect
MessageBus.after_fork
Signal.trap("HUP") do Signal.trap("HUP") do
begin begin

View file

@ -234,4 +234,17 @@ module Discourse
"/site/read-only" "/site/read-only"
end 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 end

View file

@ -179,6 +179,10 @@ module SiteSettingExtension
@@process_id ||= SecureRandom.uuid @@process_id ||= SecureRandom.uuid
end end
def after_fork
@@process_id = nil
end
def remove_override!(name) def remove_override!(name)
provider.destroy(name) provider.destroy(name)
current[name] = defaults[name] current[name] = defaults[name]

View file

@ -98,9 +98,7 @@ end
Spork.each_run do Spork.each_run do
# This code will be run each time you run your specs. # This code will be run each time you run your specs.
$redis.client.reconnect Discourse.after_fork
Rails.cache.reconnect
MessageBus.after_fork
end end
# --- Instructions --- # --- Instructions ---