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
This commit is contained in:
David Taylor 2022-11-15 22:02:13 +00:00 committed by GitHub
parent 64109e057d
commit ca72d8d030
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 2 deletions

View File

@ -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