DEV: Improve `bin/unicorn` boot time in development environment (#12900)

DEV: Improve bin/unicorn boot time in development environment (#12900)

This commit makes a few changes to improve boot time in development environments. It will have no effect on production boot times.

  • Skip the SchemaCache warmup. In development mode, the SchemaCache is refreshed every time there is a code change, so warmup is of limited use.

  • Skip warming up PrettyText. This adds ~2s to each web worker’s boot time. The vast majority of requests do not use PrettyText, so it is more efficient to defer its warmup until it’s needed

  • Skip the intentional 1 second pause during Unicorn worker forking. The comment (which also exists in Unicorn’s documentation) suggests this works around a Unix signal handling bug, but I haven’t been able to locate any more information. Skipping it in dev will significantly speed up boot. If we start to see issues, we can revert this change.

On my machine, this improves /bin/unicorn boot time from >10s to ~4s

diff --git a/config/unicorn.conf.rb b/config/unicorn.conf.rb
index 44e6735..ffcafcb 100644
--- a/config/unicorn.conf.rb
+++ b/config/unicorn.conf.rb
@@ -251,18 +251,19 @@ before_fork do |server, worker|
   # to the implementation of standard Unix signal handlers, this
   # helps (but does not completely) prevent identical, repeated signals
   # from being lost when the receiving process is busy.
-  sleep 1
+  sleep 1 if !Rails.env.development?
 end
 
 after_fork do |server, worker|
   DiscourseEvent.trigger(:web_fork_started)
+  Discourse.after_fork
 
   # warm up v8 after fork, that way we do not fork a v8 context
   # it may cause issues if bg threads in a v8 isolate randomly stop
   # working due to fork
-  Discourse.after_fork
   begin
-    PrettyText.cook("warm up **pretty text**")
+    # Skip warmup in development mode - it makes boot take ~2s longer
+    PrettyText.cook("warm up **pretty text**") if !Rails.env.development?
   rescue => e
     Rails.logger.error("Failed to warm up pretty text: #{e}")
   end
diff --git a/lib/discourse.rb b/lib/discourse.rb
index a04949f..a3ae1ad 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -903,14 +903,19 @@ module Discourse
   def self.preload_rails!
     return if @preloaded_rails
 
-    # load up all models and schema
-    (ActiveRecord::Base.connection.tables - %w[schema_migrations versions]).each do |table|
-      table.classify.constantize.first rescue nil
-    end
+    if !Rails.env.development?
+      # Skipped in development because the schema cache gets reset on every code change anyway
+      # Better to rely on the filesystem-based db:schema:cache:dump
 
-    # ensure we have a full schema cache in case we missed something above
-    ActiveRecord::Base.connection.data_sources.each do |table|
-      ActiveRecord::Base.connection.schema_cache.add(table)
+      # load up all models and schema
+      (ActiveRecord::Base.connection.tables - %w[schema_migrations versions]).each do |table|
+        table.classify.constantize.first rescue nil
+      end
+
+      # ensure we have a full schema cache in case we missed something above
+      ActiveRecord::Base.connection.data_sources.each do |table|
+        ActiveRecord::Base.connection.schema_cache.add(table)
+      end
     end
 
     schema_cache = ActiveRecord::Base.connection.schema_cache

GitHub sha: cac7725e

This commit appears in #12900 which was approved by OsamaSayegh. It was merged by davidtaylorhq.