mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PREF: optimise preloading application
We preload to ensure as much memory as possible is reused from unicorn master to various workers using copy-on-write (sidekiq, unicorn) This migrates the preloading code into the Discourse module for easier reuse and adds 3 notable preloading changes 1. We attempt to localize a string on each site, ensuring we warmup the i18n 2. We preload all our templates (compiling .erb to class) 3. We warm-up our search tokenizer which uses cppjieba which is a large memory consumer, this will only cause a warmup on CJK sites or sites with the special site setting enabled.
This commit is contained in:
@@ -53,43 +53,15 @@ initialized = false
|
||||
before_fork do |server, worker|
|
||||
|
||||
unless initialized
|
||||
# load up the yaml for the localization bits, in master process
|
||||
I18n.t(:posts)
|
||||
|
||||
# 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
|
||||
|
||||
schema_cache = ActiveRecord::Base.connection.schema_cache
|
||||
|
||||
# load up schema cache for all multisite assuming all dbs have
|
||||
# an identical schema
|
||||
RailsMultisite::ConnectionManagement.each_connection do
|
||||
dup_cache = schema_cache.dup
|
||||
# this line is not really needed, but just in case the
|
||||
# underlying implementation changes lets give it a shot
|
||||
dup_cache.connection = nil
|
||||
ActiveRecord::Base.connection.schema_cache = dup_cache
|
||||
end
|
||||
|
||||
# router warm up
|
||||
Rails.application.routes.recognize_path('abc') rescue nil
|
||||
|
||||
# preload discourse version
|
||||
Discourse.git_version
|
||||
Discourse.git_branch
|
||||
Discourse.full_version
|
||||
Discourse.preload_rails!
|
||||
|
||||
# V8 does not support forking, make sure all contexts are disposed
|
||||
ObjectSpace.each_object(MiniRacer::Context) { |c| c.dispose }
|
||||
|
||||
# get rid of rubbish so we don't share it
|
||||
# longer term we will use compact! here
|
||||
GC.start
|
||||
GC.start
|
||||
GC.start
|
||||
|
||||
initialized = true
|
||||
|
||||
Reference in New Issue
Block a user