DEV: Warm ActiveRecord cache for Sidekiq after Rails initializes. (#14253)

DEV: Warm ActiveRecord cache for Sidekiq after Rails initializes. (#14253)

Sidekiq was failing to boot in dev due to the following error. It seems like constantizing stuff before the autoloader has kicked in caused stuff to go weird. Root cause has not been identified but there is no reason for us to have to warm up the cache during the initialization of Rails.

2021-09-06T04:28:43.338Z pid=112028 tid=26vc WARN: NameError: uninitialized constant #<Class:0x0000564b365040d8>::RateLimiter
2021-09-06T04:28:43.338Z pid=112028 tid=26vc WARN: /discourse/app/models/post.rb:9:in `<class:Post>'
/discourse/app/models/post.rb:6:in `<top (required)>'
/ruby/2.7.4/lib/ruby/gems/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/kernel.rb:26:in `require'
/ruby/2.7.4/lib/ruby/gems/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/kernel.rb:26:in `require'
diff --git a/config/initializers/100-sidekiq.rb b/config/initializers/100-sidekiq.rb
index d4c1ddf..a14a080 100644
--- a/config/initializers/100-sidekiq.rb
+++ b/config/initializers/100-sidekiq.rb
@@ -60,14 +60,14 @@ if Sidekiq.server?
   # defer queue should simply run in sidekiq
   Scheduler::Defer.async = false
 
-  # warm up AR
-  RailsMultisite::ConnectionManagement.safe_each_connection do
-    (ActiveRecord::Base.connection.tables - %w[schema_migrations versions]).each do |table|
-      table.classify.constantize.first rescue nil
+  Rails.application.config.after_initialize do
+    # warm up AR
+    RailsMultisite::ConnectionManagement.safe_each_connection do
+      (ActiveRecord::Base.connection.tables - %w[schema_migrations versions]).each do |table|
+        table.classify.constantize.first rescue nil
+      end
     end
-  end
 
-  Rails.application.config.after_initialize do
     scheduler_hostname = ENV["UNICORN_SCHEDULER_HOSTNAME"]
 
     if !scheduler_hostname || scheduler_hostname.split(',').include?(Discourse.os_hostname)

GitHub sha: cb63c297b34dd793959bc9bf0c8b3f722d158e83

This commit appears in #14253 which was approved by ZogStriP. It was merged by tgxworld.