From 8fe25a695897a04e11356494437f50b67f0c643e Mon Sep 17 00:00:00 2001 From: Dmitry Chestnykh Date: Mon, 30 Sep 2013 03:10:57 +0200 Subject: [PATCH] Replace directory_watcher with listen. Directory_watcher consumed ~25% CPU on big Jekyll projects (depending on the number of watched files), since it polled for changes every second. Listen is easier on CPU, as it uses directory change notifications provided by OS (currently OS X and Linux), falling back to polling when they are not available. --- jekyll.gemspec | 2 +- lib/jekyll/commands/build.rb | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/jekyll.gemspec b/jekyll.gemspec index f726d4d1..f9b46f73 100644 --- a/jekyll.gemspec +++ b/jekyll.gemspec @@ -25,7 +25,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency('liquid', "~> 2.5.2") s.add_runtime_dependency('classifier', "~> 1.3") - s.add_runtime_dependency('directory_watcher', "~> 1.4.1") + s.add_runtime_dependency('listen', "~> 1.3.1") s.add_runtime_dependency('maruku', "~> 0.5") s.add_runtime_dependency('pygments.rb', "~> 0.5.0") s.add_runtime_dependency('commander', "~> 4.1.3") diff --git a/lib/jekyll/commands/build.rb b/lib/jekyll/commands/build.rb index fe49b54b..901806c7 100644 --- a/lib/jekyll/commands/build.rb +++ b/lib/jekyll/commands/build.rb @@ -31,25 +31,24 @@ module Jekyll # # Returns nothing. def self.watch(site, options) - require 'directory_watcher' + require 'listen' + require 'pathname' source = options['source'] - destination = options['destination'] + destination = Pathname.new(options['destination']) + .relative_path_from(Pathname.new(source)) + .to_path Jekyll.logger.info "Auto-regeneration:", "enabled" - dw = DirectoryWatcher.new(source, :glob => self.globs(source, destination), :pre_load => true) - dw.interval = 1 - - dw.add_observer do |*args| + Listen.to(source, :ignore => %r{#{Regexp.escape(destination)}}) do |modified, added, removed| t = Time.now.strftime("%Y-%m-%d %H:%M:%S") - print Jekyll.logger.formatted_topic("Regenerating:") + "#{args.size} files at #{t} " + n = modified.length + added.length + removed.length + print Jekyll.logger.formatted_topic("Regenerating:") + "#{n} files at #{t} " self.process_site(site) puts "...done." end - dw.start - unless options['serving'] trap("INT") do puts " Halting auto-regeneration."