From de05c410c54922ab5bd0b5819ad86161c2f0e4c1 Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Fri, 12 Mar 2021 16:18:00 -0500 Subject: [PATCH] DEV: Use fewer threads when watching for SCSS changes (#12393) --- lib/stylesheet/watcher.rb | 91 ++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 49 deletions(-) diff --git a/lib/stylesheet/watcher.rb b/lib/stylesheet/watcher.rb index 1b83891affb..f814d1b782d 100644 --- a/lib/stylesheet/watcher.rb +++ b/lib/stylesheet/watcher.rb @@ -27,67 +27,62 @@ module Stylesheet end def start - Thread.new do begin while true worker_loop end rescue => e - STDERR.puts "CSS change notifier crashed #{e}" + STDERR.puts "CSS change notifier crashed \n#{e}" start end end - root = Rails.root.to_s - - listener_opts = { ignore: /xxxx/ } + listener_opts = { ignore: /xxxx/, only: /\.(css|scss)$/ } listener_opts[:force_polling] = true if ENV['FORCE_POLLING'] - @paths.each do |watch| - Thread.new do - begin - plugins_paths = Dir.glob("#{Rails.root}/plugins/*").map do |file| - if File.symlink?(file) - File.expand_path(File.readlink(file), "#{Rails.root}/plugins") - else - 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) + Thread.new do + begin + plugins_paths = Dir.glob("#{Rails.root}/plugins/*").map do |file| + if File.symlink?(file) + File.expand_path(File.readlink(file), "#{Rails.root}/plugins") + else + file end - rescue => e - STDERR.puts "Failed to listen for CSS changes at: #{watch}\n#{e}" + end.compact + + 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 - listener.start - sleep + rescue => e + STDERR.puts "Failed to listen for CSS changes: \n#{e}" end + listener.start + sleep end end @@ -138,9 +133,7 @@ module Stylesheet def process_change(paths) paths.each do |path| - if path[:basename] =~ /\.(css|scss)$/ - @queue.push path - end + @queue.push path end end