Skip to content

Commit

Permalink
okay, this is a working em impl
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed Dec 10, 2011
1 parent 3fa049d commit babfad0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 50 deletions.
3 changes: 2 additions & 1 deletion gitdocs.gemspec
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion 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'
Expand Down
2 changes: 1 addition & 1 deletion lib/gitdocs/configuration.rb
Expand Up @@ -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)
Expand Down
47 changes: 18 additions & 29 deletions lib/gitdocs/manager.rb
Expand Up @@ -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

Expand Down
45 changes: 28 additions & 17 deletions lib/gitdocs/runner.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -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
Expand Down

0 comments on commit babfad0

Please sign in to comment.