From ca72d8d0304e051958183692949b18fbe843cde3 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Tue, 15 Nov 2022 22:02:13 +0000 Subject: [PATCH] PERF: Adjust node memory threshold for assets:precompile (#19040) Previously we were forcing node's max-old-space-size to be 2GB. This override was added in a01b1dd6 to avoid issues caused by a lower default node heap_size_limit on machines with less memory. This commit makes that `max-old-space-size` override more specific so that it only applies to machines with less memory. Other machines will go use Node's defaults. The override is also lowered to 1GB. This is still high enough for the build to complete, while reducing memory usage. https://meta.discourse.org/t/245547 --- lib/tasks/assets.rake | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake index 90820357255..34a3e45ac78 100644 --- a/lib/tasks/assets.rake +++ b/lib/tasks/assets.rake @@ -9,14 +9,20 @@ task 'assets:precompile:before' do end if ENV["EMBER_CLI_COMPILE_DONE"] != "1" - compile_command = "NODE_OPTIONS='--max-old-space-size=2048' yarn --cwd app/assets/javascripts/discourse run ember build -prod" + compile_command = "yarn --cwd app/assets/javascripts/discourse run ember build -prod" + + if check_node_heap_size_limit < 1024 + STDERR.puts "Detected low Node.js heap_size_limit. Using --max-old-space-size=1024." + compile_command = "NODE_OPTIONS='--max-old-space-size=1024' #{compile_command}" + end + only_assets_precompile_remaining = (ARGV.last == "assets:precompile") if only_assets_precompile_remaining # Using exec to free up Rails app memory during ember build exec "#{compile_command} && EMBER_CLI_COMPILE_DONE=1 bin/rake assets:precompile" else - system compile_command + system compile_command, exception: true end end @@ -91,6 +97,12 @@ task 'assets:flush_sw' => 'environment' do end end +def check_node_heap_size_limit + output, status = Open3.capture2("node", "-e", "console.log(v8.getHeapStatistics().heap_size_limit/1024/1024)") + raise "Failed to fetch node memory limit" if status != 0 + output.to_f +end + def assets_path "#{Rails.root}/public/assets" end