PERF: Terminate rake process during ember build (#15920)

The `assets:precompile` rake task loads the full Ruby app, which can consume around 500mb of RAM by itself. Using `exec` to run `ember build` allows us to free up the Ruby memory and make more space for `ember build`
This commit is contained in:
David Taylor 2022-02-14 11:49:46 +00:00 committed by GitHub
parent efb584e32f
commit 6ab4d26d84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,7 @@
# frozen_string_literal: true
EMBER_CLI = ENV["EMBER_CLI_PROD_ASSETS"] != "0"
task 'assets:precompile:before' do
require 'uglifier'
@ -9,6 +11,14 @@ task 'assets:precompile:before' do
raise "rake assets:precompile should only be run in RAILS_ENV=production, you are risking unminified assets"
end
if EMBER_CLI && !(ENV["EMBER_CLI_COMPILE_DONE"] == "1")
# Using exec to free up Rails app memory during ember build
exec <<~SCRIPT
NODE_OPTIONS='--max-old-space-size=2048' yarn --cwd app/assets/javascripts/discourse run ember build -prod && \
EMBER_CLI_COMPILE_DONE=1 bin/rake assets:precompile
SCRIPT
end
# Ensure we ALWAYS do a clean build
# We use many .erbs that get out of date quickly, especially with plugins
STDERR.puts "Purging temp files"
@ -35,7 +45,7 @@ task 'assets:precompile:before' do
require 'sprockets'
require 'digest/sha1'
if ENV['EMBER_CLI_PROD_ASSETS'] != "0"
if EMBER_CLI
# Remove the assets that Ember CLI will handle for us
Rails.configuration.assets.precompile.reject! do |asset|
asset.is_a?(String) && is_ember_cli_asset?(asset)
@ -82,7 +92,7 @@ task 'assets:flush_sw' => 'environment' do
end
def is_ember_cli_asset?(name)
return false if ENV['EMBER_CLI_PROD_ASSETS'] == '0'
return false if !EMBER_CLI
%w(application.js admin.js ember_jquery.js pretty-text-bundle.js start-discourse.js vendor.js).include?(name)
end
@ -244,13 +254,6 @@ def copy_ember_cli_assets
assets = {}
files = {}
log_task_duration('ember build -prod') {
unless system("NODE_OPTIONS='--max-old-space-size=2048' yarn --cwd #{ember_dir} run ember build -prod")
STDERR.puts "Error running ember build"
exit 1
end
}
# Copy assets and generate manifest data
log_task_duration('Copy assets and generate manifest data') {
Dir["#{ember_cli_assets}**/*"].each do |f|
@ -317,7 +320,7 @@ end
task 'assets:precompile' => 'assets:precompile:before' do
copy_ember_cli_assets if ENV['EMBER_CLI_PROD_ASSETS'] != '0'
copy_ember_cli_assets if EMBER_CLI
refresh_days = GlobalSetting.refresh_maxmind_db_during_precompile_days