DEV: Use fewer threads when watching for SCSS changes (#12393)

This commit is contained in:
Penar Musaraj 2021-03-12 16:18:00 -05:00 committed by GitHub
parent 2a18b0b07e
commit de05c410c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,67 +27,62 @@ module Stylesheet
end end
def start def start
Thread.new do Thread.new do
begin begin
while true while true
worker_loop worker_loop
end end
rescue => e rescue => e
STDERR.puts "CSS change notifier crashed #{e}" STDERR.puts "CSS change notifier crashed \n#{e}"
start start
end end
end end
root = Rails.root.to_s listener_opts = { ignore: /xxxx/, only: /\.(css|scss)$/ }
listener_opts = { ignore: /xxxx/ }
listener_opts[:force_polling] = true if ENV['FORCE_POLLING'] listener_opts[:force_polling] = true if ENV['FORCE_POLLING']
@paths.each do |watch| Thread.new do
Thread.new do begin
begin plugins_paths = Dir.glob("#{Rails.root}/plugins/*").map do |file|
plugins_paths = Dir.glob("#{Rails.root}/plugins/*").map do |file| if File.symlink?(file)
if File.symlink?(file) File.expand_path(File.readlink(file), "#{Rails.root}/plugins")
File.expand_path(File.readlink(file), "#{Rails.root}/plugins") else
else file
file
end
end.compact
listener = Listen.to("#{root}/#{watch}", listener_opts) do |modified, added, _|
paths = [modified, added].flatten
paths.compact!
paths.map! do |long|
plugin_name = nil
plugins_paths.each do |plugin_path|
if long.include?("#{plugin_path}/")
plugin_name = File.basename(plugin_path)
break
end
end
target = nil
target_match = long.match(/admin|desktop|mobile|publish/)
if target_match&.length
target = target_match[0]
end
{
basename: File.basename(long),
target: target,
plugin_name: plugin_name
}
end
process_change(paths)
end end
rescue => e end.compact
STDERR.puts "Failed to listen for CSS changes at: #{watch}\n#{e}"
listener = Listen.to(*@paths, listener_opts) do |modified, added, _|
paths = [modified, added].flatten
paths.compact!
paths.map! do |long|
plugin_name = nil
plugins_paths.each do |plugin_path|
if long.include?("#{plugin_path}/")
plugin_name = File.basename(plugin_path)
break
end
end
target = nil
target_match = long.match(/admin|desktop|mobile|publish/)
if target_match&.length
target = target_match[0]
end
{
basename: File.basename(long),
target: target,
plugin_name: plugin_name
}
end
process_change(paths)
end end
listener.start rescue => e
sleep STDERR.puts "Failed to listen for CSS changes: \n#{e}"
end end
listener.start
sleep
end end
end end
@ -138,9 +133,7 @@ module Stylesheet
def process_change(paths) def process_change(paths)
paths.each do |path| paths.each do |path|
if path[:basename] =~ /\.(css|scss)$/ @queue.push path
@queue.push path
end
end end
end end