diff --git a/gitdocs.gemspec b/gitdocs.gemspec index 7924862..3063327 100644 --- a/gitdocs.gemspec +++ b/gitdocs.gemspec @@ -18,7 +18,8 @@ Gem::Specification.new do |s| s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] - s.add_dependency 'rb-fsevent', '~> 0.4.3.1' + #s.add_dependency 'em-dir-watcher', '~> 0.9.4' + s.add_dependency 'joshbuddy-guard', '~> 0.8.8' s.add_dependency 'thin' s.add_dependency 'renee', '~> 0.3.7' s.add_dependency 'redcarpet' diff --git a/lib/gitdocs.rb b/lib/gitdocs.rb index 9fd8784..9848607 100644 --- a/lib/gitdocs.rb +++ b/lib/gitdocs.rb @@ -1,10 +1,10 @@ require 'thread' -require 'rb-fsevent' require 'growl' require 'yajl' require 'dante' require 'socket' require 'shell_tools' +require 'guard' require 'gitdocs/version' require 'gitdocs/configuration' diff --git a/lib/gitdocs/configuration.rb b/lib/gitdocs/configuration.rb index 2ba0357..4c6730b 100644 --- a/lib/gitdocs/configuration.rb +++ b/lib/gitdocs/configuration.rb @@ -35,7 +35,7 @@ def available_branches end class Config < ActiveRecord::Base - attr_accessible :load_browser_on_startup + attr_accessible :load_browser_on_startup, :start_web_frontend end def add_path(path, opts = nil) diff --git a/lib/gitdocs/manager.rb b/lib/gitdocs/manager.rb index b13e5cc..86fb70f 100644 --- a/lib/gitdocs/manager.rb +++ b/lib/gitdocs/manager.rb @@ -17,37 +17,26 @@ def run puts "Shares: #{config.shares.map(&:inspect).join(", ")}" if self.debug # Start the repo watchers runners = [] - threads = config.shares.map do |share| - t = Thread.new(runners) { |r| - runner = Runner.new(share) - r << runner - runner.run - } - t.abort_on_exception = true - t - end - trap("USR1") { puts "stopping threads: #{threads.map(&:alive?)}"; runners.each { |r| r.listener.stop } } - sleep 1 - unless @pid + EM.run do + threads = config.shares.map { |share| Runner.new(share).run } + trap("USR1") { EM.stop_reactor } # Start the web front-end - @pid = fork { Server.new(self, *runners).start } - at_exit { Process.kill("KILL", @pid) rescue nil } - end - puts "Watch threads: #{threads.map { |t| "Thread status: '#{t.status}', running: #{t.alive?}" }}" if self.debug - puts "Joined #{threads.size} watch threads...running" if self.debug - i = 0 - web_started = false - begin - TCPSocket.open('127.0.0.1', 8888).close - web_started = true - rescue Errno::ECONNREFUSED - sleep 0.2 - i += 1 - retry if i <= 20 + if self.config.global.start_web_frontend + Server.new(self, *runners).start + i = 0 + web_started = false + begin + TCPSocket.open('127.0.0.1', 8888).close + web_started = true + rescue Errno::ECONNREFUSED + sleep 0.2 + i += 1 + retry if i <= 20 + end + system("open http://localhost:8888/") if self.config.global.load_browser_on_startup && web_started + end end - system("open http://localhost:8888/") if self.config.global.load_browser_on_startup && web_started - threads.each(&:join) - sleep(60) if threads.empty? + sleep(10) if runners.empty? end end diff --git a/lib/gitdocs/runner.rb b/lib/gitdocs/runner.rb index 2076c3b..7bfbf97 100644 --- a/lib/gitdocs/runner.rb +++ b/lib/gitdocs/runner.rb @@ -18,29 +18,40 @@ def run @current_remote = @share.remote_name @current_branch = @share.branch_name @current_revision = sh_string("git rev-parse HEAD") + mutex = Mutex.new info("Running gitdocs!", "Running gitdocs in `#{@root}'") - mutex = Mutex.new # Pull changes from remote repository - Thread.new do - loop do + syncer = proc do + EM.defer(proc do mutex.synchronize { sync_changes } - sleep @polling_interval - end - end.abort_on_exception = true - - # Listen for changes in local repository - @listener = FSEvent.new - @listener.watch(@root) do |directories| - directories.uniq! - directories.delete_if {|d| d =~ /\/\.git/} - unless directories.empty? - mutex.synchronize { push_changes } - end + end, proc do + EM.add_timer(@polling_interval) { + syncer.call + } + end) end - at_exit { @listener.stop } - @listener.run + syncer.call + # Listen for changes in local repository + + EM.defer(proc{ + listener = Guard::Listener.select_and_init(@root, :watch_all_modifications => true) + listener.on_change { |directories| + directories.uniq! + directories.delete_if {|d| d =~ /\/\.git/} + unless directories.empty? + EM.next_tick do + mutex.synchronize { push_changes } + end + end + } + listener.start + }, proc{EM.stop_reactor}) + end + + def clear_state + @state = nil end def sync_changes diff --git a/test/test_helper.rb b/test/test_helper.rb index 8c5e9b2..f0ff039 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -32,7 +32,7 @@ def with_clones(count = 3) begin puts "RUNNING!" Gitdocs.run(conf_path) do |conf| - conf.global.update_attributes(:load_browser_on_startup => false) + conf.global.update_attributes(:load_browser_on_startup => false, :start_web_frontend => false) conf.add_path(path, :polling_interval => 0.1, :notification => false) end rescue