Class: Gitdocs::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/gitdocs/runner.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(share) ⇒ Runner



11
12
13
14
15
16
# File 'lib/gitdocs/runner.rb', line 11

def initialize(share)
  @share = share
  @polling_interval = share.polling_interval
  @notifier         = Gitdocs::Notifier.new(@share.notification)
  @repository       = Gitdocs::Repository.new(share)
end

Class Method Details

.start_all(shares) ⇒ Object



5
6
7
8
9
# File 'lib/gitdocs/runner.rb', line 5

def self.start_all(shares)
  runners = shares.map { |share| Runner.new(share) }
  runners.each(&:run)
  runners
end

Instance Method Details

#clear_stateObject



63
64
65
# File 'lib/gitdocs/runner.rb', line 63

def clear_state
  @state = nil
end

#rootObject



18
19
20
# File 'lib/gitdocs/runner.rb', line 18

def root
  @repository.root
end

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gitdocs/runner.rb', line 22

def run
  return false unless @repository.valid?

  @last_synced_revision = @repository.current_oid

  mutex = Mutex.new

  @notifier.info('Running gitdocs!', "Running gitdocs in '#{root}'")

  # Pull changes from remote repository
  syncer = proc do
    EM.defer(proc do
      mutex.synchronize { sync_changes }
    end, proc do
      EM.add_timer(@polling_interval) do
        syncer.call
      end
    end)
  end
  syncer.call
  # Listen for changes in local repository

  EM.defer(proc do
    listener = Guard::Listener.select_and_init(
      root, watch_all_modifications: true
    )
    listener.on_change do |directories|
      directories.uniq!
      directories.delete_if { |d| d =~ /\/\.git/ }
      unless directories.empty?
        EM.next_tick do
          EM.defer(proc do
            mutex.synchronize { sync_changes }
          end, proc {})
        end
      end
    end
    listener.start
  end, proc { EM.stop_reactor })
end

#sync_changesObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gitdocs/runner.rb', line 67

def sync_changes
  # Commit #################################################################
  @repository.commit if @share.sync_type == 'full'

  # Fetch ##################################################################
  fetch_result = @repository.fetch
  return unless fetch_result == :ok
  return if @share.sync_type == 'fetch'

  # Merge ##################################################################
  merge_result = @repository.merge
  merge_result = latest_author_count if merge_result == :ok
  @notifier.merge_notification(merge_result, root)
  return if merge_result.is_a?(String)

  # Push ###################################################################
  result = @repository.push
  result = latest_author_count if result == :ok
  @notifier.push_notification(result, root)
rescue => e
  # Rescue any standard exceptions which come from the push related
  # commands. This will prevent problems on a single share from killing
  # the entire daemon.
  @notifier.error("Unexpected error syncing changes in #{root}", "#{e}")
end