From ca1208a63669d4d4ad7452367008d40fa090f645 Mon Sep 17 00:00:00 2001 From: Daniel Waterworth Date: Thu, 2 Apr 2020 15:19:42 +0100 Subject: [PATCH] Revert "REFACTOR: Move the multisite middleware to the front" Looks like this is causing problems. Follow-up-to: a91843f0dc7b97e700dc85505404eafd62b7f8c5 --- .../initializers/004-rails_multisite.rb | 8 ++++++++ ....rb => 200-message_bus_request_tracker.rb} | 17 ++-------------- lib/middleware/request_tracker.rb | 20 +++++++++++++------ 3 files changed, 24 insertions(+), 21 deletions(-) rename lib/freedom_patches/rails_multisite.rb => config/initializers/004-rails_multisite.rb (77%) rename config/initializers/{200-first_middlewares.rb => 200-message_bus_request_tracker.rb} (58%) diff --git a/lib/freedom_patches/rails_multisite.rb b/config/initializers/004-rails_multisite.rb similarity index 77% rename from lib/freedom_patches/rails_multisite.rb rename to config/initializers/004-rails_multisite.rb index c4627daa2a7..21543492ea8 100644 --- a/lib/freedom_patches/rails_multisite.rb +++ b/config/initializers/004-rails_multisite.rb @@ -26,3 +26,11 @@ module RailsMultisite end end end + +if Rails.configuration.multisite + Rails.configuration.middleware.swap( + RailsMultisite::Middleware, + RailsMultisite::Middleware, + RailsMultisite::DiscoursePatches.config + ) +end diff --git a/config/initializers/200-first_middlewares.rb b/config/initializers/200-message_bus_request_tracker.rb similarity index 58% rename from config/initializers/200-first_middlewares.rb rename to config/initializers/200-message_bus_request_tracker.rb index 6ae5816fbaf..c0ebcfcb1b6 100644 --- a/config/initializers/200-first_middlewares.rb +++ b/config/initializers/200-message_bus_request_tracker.rb @@ -1,11 +1,9 @@ # frozen_string_literal: true -# we want MesageBus to be close to the front +# we want MesageBus in the absolute front # this is important cause the vast majority of web requests go to it # this allows us to avoid full middleware crawls each time -# -# We aren't manipulating the middleware stack directly because of -# https://github.com/rails/rails/pull/27936 +# Pending https://github.com/rails/rails/pull/27936 session_operations = Rails::Configuration::MiddlewareStackProxy.new([ [:delete, MessageBus::Rack::Middleware], [:unshift, MessageBus::Rack::Middleware], @@ -23,14 +21,3 @@ if Rails.env != 'development' || ENV['TRACK_REQUESTS'] MethodProfiler.ensure_discourse_instrumentation! end end - -if Rails.configuration.multisite - # Multisite needs to be first, because the request tracker and message bus - # rely on it - session_operations = Rails::Configuration::MiddlewareStackProxy.new([ - [:delete, RailsMultisite::Middleware], - [:unshift, RailsMultisite::Middleware, RailsMultisite::DiscoursePatches.config], - ]) - - Rails.configuration.middleware = Rails.configuration.middleware + session_operations -end diff --git a/lib/middleware/request_tracker.rb b/lib/middleware/request_tracker.rb index 4fbe3d1b2a3..41ad8a60b41 100644 --- a/lib/middleware/request_tracker.rb +++ b/lib/middleware/request_tracker.rb @@ -51,6 +51,14 @@ class Middleware::RequestTracker @app = app end + def self.log_request_on_site(data, host) + RailsMultisite::ConnectionManagement.with_hostname(host) do + unless Discourse.pg_readonly_mode? + log_request(data) + end + end + end + def self.log_request(data) status = data[:status] track_view = data[:track_view] @@ -127,6 +135,7 @@ class Middleware::RequestTracker # we got to skip this on error ... its just logging data = self.class.get_data(env, result, info) rescue nil + host = RailsMultisite::ConnectionManagement.host(env) if data if result && (headers = result[1]) @@ -137,7 +146,7 @@ class Middleware::RequestTracker @@detailed_request_loggers.each { |logger| logger.call(env, data) } end - log_later(data) + log_later(data, host) end end @@ -287,11 +296,10 @@ class Middleware::RequestTracker end end - def log_later(data) - Scheduler::Defer.later("Track view") do - unless Discourse.pg_readonly_mode? - self.class.log_request(data) - end + def log_later(data, host) + Scheduler::Defer.later("Track view", _db = nil) do + self.class.log_request_on_site(data, host) end end + end