mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 04:11:33 +08:00
PERF: Optimize themes:update task (#29290)
- Add concurrency when running on multisite clusters (default 10, configurable via THEME_UPDATE_CONCURRENCY env) - Add a version cache for the duration of the rake task. This avoids duplicating work when many sites in the cluster have the same theme installed, and it is already up-to-date - Updates output to be more concurrent friendly (all `puts`, no `print`)
This commit is contained in:
parent
a5497b74be
commit
b3b0695bb1
4
Gemfile
4
Gemfile
|
@ -243,8 +243,6 @@ if ENV["IMPORT"] == "1"
|
||||||
gem "reverse_markdown"
|
gem "reverse_markdown"
|
||||||
gem "tiny_tds"
|
gem "tiny_tds"
|
||||||
gem "csv"
|
gem "csv"
|
||||||
|
|
||||||
gem "parallel", require: false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
group :generic_import, optional: true do
|
group :generic_import, optional: true do
|
||||||
|
@ -289,3 +287,5 @@ group :migrations, optional: true do
|
||||||
end
|
end
|
||||||
|
|
||||||
gem "dry-initializer", "~> 3.1"
|
gem "dry-initializer", "~> 3.1"
|
||||||
|
|
||||||
|
gem "parallel"
|
||||||
|
|
|
@ -677,6 +677,7 @@ DEPENDENCIES
|
||||||
omniauth-google-oauth2
|
omniauth-google-oauth2
|
||||||
omniauth-oauth2
|
omniauth-oauth2
|
||||||
omniauth-twitter
|
omniauth-twitter
|
||||||
|
parallel
|
||||||
parallel_tests
|
parallel_tests
|
||||||
pg
|
pg
|
||||||
pry-byebug
|
pry-byebug
|
||||||
|
|
|
@ -60,7 +60,7 @@ task "themes:install:archive" => :environment do |task, args|
|
||||||
RemoteTheme.update_zipped_theme(filename, File.basename(filename), update_components:)
|
RemoteTheme.update_zipped_theme(filename, File.basename(filename), update_components:)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_themes
|
def update_themes(version_cache: Concurrent::Map.new)
|
||||||
Theme
|
Theme
|
||||||
.includes(:remote_theme)
|
.includes(:remote_theme)
|
||||||
.where(enabled: true, auto_update: true)
|
.where(enabled: true, auto_update: true)
|
||||||
|
@ -69,16 +69,26 @@ def update_themes
|
||||||
theme.transaction do
|
theme.transaction do
|
||||||
remote_theme = theme.remote_theme
|
remote_theme = theme.remote_theme
|
||||||
next if remote_theme.blank? || remote_theme.remote_url.blank?
|
next if remote_theme.blank? || remote_theme.remote_url.blank?
|
||||||
|
prefix = "[db:#{RailsMultisite::ConnectionManagement.current_db}] '#{theme.name}' - "
|
||||||
|
puts "#{prefix} checking..."
|
||||||
|
|
||||||
print "Checking '#{theme.name}' for '#{RailsMultisite::ConnectionManagement.current_db}'... "
|
cache_key =
|
||||||
|
"#{remote_theme.remote_url}:#{remote_theme.branch}:#{Digest::SHA256.hexdigest(remote_theme.private_key.to_s)}"
|
||||||
|
|
||||||
|
if version_cache[cache_key] == remote_theme.remote_version && !remote_theme.out_of_date?
|
||||||
|
puts "#{prefix} up to date (cached from previous lookup)"
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
remote_theme.update_remote_version
|
remote_theme.update_remote_version
|
||||||
|
|
||||||
|
version_cache.put_if_absent(cache_key, remote_theme.remote_version)
|
||||||
|
|
||||||
if remote_theme.out_of_date?
|
if remote_theme.out_of_date?
|
||||||
puts "updating from #{remote_theme.local_version[0..7]} to #{remote_theme.remote_version[0..7]}"
|
puts "#{prefix} updating from #{remote_theme.local_version[0..7]} to #{remote_theme.remote_version[0..7]}"
|
||||||
remote_theme.update_from_remote(already_in_transaction: true)
|
remote_theme.update_from_remote(already_in_transaction: true)
|
||||||
else
|
else
|
||||||
puts "up to date"
|
puts "#{prefix} up to date"
|
||||||
end
|
end
|
||||||
|
|
||||||
if remote_theme.last_error_text.present?
|
if remote_theme.last_error_text.present?
|
||||||
|
@ -99,7 +109,14 @@ task "themes:update": %w[environment assets:precompile:theme_transpiler] do
|
||||||
if ENV["RAILS_DB"].present?
|
if ENV["RAILS_DB"].present?
|
||||||
update_themes
|
update_themes
|
||||||
else
|
else
|
||||||
RailsMultisite::ConnectionManagement.each_connection { update_themes }
|
version_cache = Concurrent::Map.new
|
||||||
|
|
||||||
|
concurrency = ENV["THEME_UPDATE_CONCURRENCY"]&.to_i || 10
|
||||||
|
puts "Updating themes with concurrency: #{concurrency}" if concurrency > 1
|
||||||
|
|
||||||
|
Parallel.each(RailsMultisite::ConnectionManagement.all_dbs, in_threads: concurrency) do |db|
|
||||||
|
RailsMultisite::ConnectionManagement.with_connection(db) { update_themes(version_cache:) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user