FIX: Sidekiq fails to start if any of the multisite has problems.

FIX: Sidekiq fails to start if any of the multisite has problems.
From e564fe1866ecf9711e403e7c9cef4f2c752b7d69 Mon Sep 17 00:00:00 2001
From: Guo Xiang Tan <tgx_world@hotmail.com>
Date: Wed, 12 Dec 2018 11:29:48 +0800
Subject: [PATCH] FIX: Sidekiq fails to start if any of the multisite has
 problems.


diff --git a/config/initializers/004-rails_multisite.rb b/config/initializers/004-rails_multisite.rb
new file mode 100644
index 0000000..2154349
--- /dev/null
+++ b/config/initializers/004-rails_multisite.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+module RailsMultisite
+  class ConnectionManagement
+    def self.safe_each_connection
+      self.each_connection do |db|
+        begin
+          yield(db) if block_given?
+        rescue => e
+          STDERR.puts "URGENT: Failed to initialize site #{db}: "\
+            "#{e.class} #{e.message}\n#{e.backtrace.join("\n")}"
+
+          # the show must go on, don't stop startup if multisite fails
+        end
+      end
+    end
+  end
+
+  class DiscoursePatches
+    def self.config
+      {
+        db_lookup: lambda do |env|
+          env["PATH_INFO"] == "/srv/status" ? "default" : nil
+        end
+      }
+    end
+  end
+end
+
+if Rails.configuration.multisite
+  Rails.configuration.middleware.swap(
+    RailsMultisite::Middleware,
+    RailsMultisite::Middleware,
+    RailsMultisite::DiscoursePatches.config
+  )
+end
diff --git a/config/initializers/005-site_settings.rb b/config/initializers/005-site_settings.rb
index 9209ff5..033db48 100644
--- a/config/initializers/005-site_settings.rb
+++ b/config/initializers/005-site_settings.rb
@@ -4,7 +4,7 @@
 Discourse.git_version
 
 reload_settings = lambda {
-  RailsMultisite::ConnectionManagement.each_connection do
+  RailsMultisite::ConnectionManagement.safe_each_connection do
     begin
       SiteSetting.refresh!
 
@@ -13,10 +13,6 @@ reload_settings = lambda {
       end
     rescue ActiveRecord::StatementInvalid
       # This will happen when migrating a new database
-    rescue => e
-      STDERR.puts "URGENT: Failed to initialize site #{RailsMultisite::ConnectionManagement.current_db}:"\
-        "#{e.message}\n#{e.backtrace.join("\n")}"
-      # the show must go on, don't stop startup if multisite fails
     end
   end
 }
diff --git a/config/initializers/014-rails_multisite.rb b/config/initializers/014-rails_multisite.rb
deleted file mode 100644
index 630b07f..0000000
--- a/config/initializers/014-rails_multisite.rb
+++ /dev/null
@@ -1,19 +0,0 @@
-# frozen_string_literal: true
-
-class RailsMultisite::DiscoursePatches
-  def self.config
-    {
-      db_lookup: lambda do |env|
-        env["PATH_INFO"] == "/srv/status" ? "default" : nil
-      end
-    }
-  end
-end
-
-if Rails.configuration.multisite
-  Rails.configuration.middleware.swap(
-    RailsMultisite::Middleware,
-    RailsMultisite::Middleware,
-    RailsMultisite::DiscoursePatches.config
-  )
-end
diff --git a/config/initializers/100-sidekiq.rb b/config/initializers/100-sidekiq.rb
index 1e2ff8d..efc76be 100644
--- a/config/initializers/100-sidekiq.rb
+++ b/config/initializers/100-sidekiq.rb
@@ -46,7 +46,7 @@ if Sidekiq.server?
   Scheduler::Defer.async = false
 
   # warm up AR
-  RailsMultisite::ConnectionManagement.each_connection do
+  RailsMultisite::ConnectionManagement.safe_each_connection do
     (ActiveRecord::Base.connection.tables - %w[schema_migrations]).each do |table|
       table.classify.constantize.first rescue nil
     end

GitHub

1 Like

Nice, happy to have the code reuse here.