From baf41790dd28dbe4db347221114b293b3f81b375 Mon Sep 17 00:00:00 2001 From: Daniel Waterworth Date: Tue, 3 Sep 2024 00:56:46 -0500 Subject: [PATCH] PERF: Don't do initialization for every DB if RAILS_DB is set (#28668) --- config/initializers/100-logster.rb | 2 +- lib/freedom_patches/rails_multisite.rb | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/config/initializers/100-logster.rb b/config/initializers/100-logster.rb index 9248e35e326..0f4c1a06e73 100644 --- a/config/initializers/100-logster.rb +++ b/config/initializers/100-logster.rb @@ -81,7 +81,7 @@ store.redis_prefix = Proc.new { redis.namespace } store.redis_raw_connection = redis.without_namespace severities = [Logger::WARN, Logger::ERROR, Logger::FATAL, Logger::UNKNOWN] -RailsMultisite::ConnectionManagement.each_connection do +RailsMultisite::ConnectionManagement.safe_each_connection do error_rate_per_minute = begin SiteSetting.alert_admins_if_errors_per_minute diff --git a/lib/freedom_patches/rails_multisite.rb b/lib/freedom_patches/rails_multisite.rb index 608bf580b03..a4ae7899997 100644 --- a/lib/freedom_patches/rails_multisite.rb +++ b/lib/freedom_patches/rails_multisite.rb @@ -2,8 +2,16 @@ module RailsMultisite class ConnectionManagement + def self.each_active_connection(&blk) + if ENV["RAILS_DB"] + blk.call(current_db) + else + each_connection(&blk) + end + end + def self.safe_each_connection - self.each_connection do |db| + each_active_connection do |db| begin yield(db) if block_given? rescue PG::ConnectionBad, PG::UnableToSend, PG::ServerError