Class: Gitdocs::Manager

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.listen_method:notification, :polling

Returns:

  • (:notification, :polling)


19
20
21
22
# File 'lib/gitdocs/manager.rb', line 19

def self.listen_method
  return :polling if Listen::Adapter.select == Listen::Adapter::Polling
  :notification
end

.restart_synchronizationvoid

This method returns an undefined value.



14
15
16
# File 'lib/gitdocs/manager.rb', line 14

def self.restart_synchronization
  Thread.main.raise(Restart, 'restarting ... ')
end

.start(web_port) ⇒ void

This method returns an undefined value.

Parameters:

  • web_port (Integer)


9
10
11
# File 'lib/gitdocs/manager.rb', line 9

def self.start(web_port)
  Manager.new.start(web_port)
end

Instance Method Details

#start(web_port) ⇒ void

This method returns an undefined value.

Parameters:

  • web_port (Integer)


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
62
63
64
65
66
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/gitdocs/manager.rb', line 26

def start(web_port)
  Gitdocs.log_info("Starting Gitdocs v#{VERSION}...")
  Gitdocs.log_info(
    "Using configuration root: '#{Initializer.root_dirname}'"
  )

  Celluloid.boot unless Celluloid.running?
  @supervisor = Celluloid::SupervisionGroup.run!

  # Start the web server ###################################################
  app =
    Rack::Builder.new do
      use Rack::Static,
          urls: %w(/css /js /img /doc),
          root: File.expand_path('../public', __FILE__)
      use Rack::MethodOverride
      map('/settings') { run SettingsApp }
      map('/')         { run BrowserApp }
    end
  @supervisor.add(
    Reel::Rack::Server,
    as: :reel_rack_server,
    args: [
      app,
      {
        Host:  '127.0.0.1',
        Port:  web_port,
        quiet: true
      }
    ]
  )

  # Start the synchronizers ################################################
  @synchronization_supervisor = Celluloid::SupervisionGroup.run!
  Share.all.each do |share|
    @synchronization_supervisor.add(
      Synchronizer, as: share.id.to_s, args: [share]
    )
  end

  # Start the repository listeners #########################################
  @listener =
    Listen.to(
      *Share.paths,
      ignore: /#{File::SEPARATOR}\.git#{File::SEPARATOR}/
    ) do |modified, added, removed|
      all_changes = modified + added + removed
      changed_repository_paths =
        Share.paths.select do |directory|
          all_changes.any? { |x| x.start_with?(directory) }
        end

      changed_repository_paths.each do |directory|
        actor_id = Share.find_by_path(directory).id.to_s
        Celluloid::Actor[actor_id].async.synchronize
      end
    end
  @listener.start

  # ... and wait ###########################################################
  sleep

rescue Interrupt, SystemExit
  Gitdocs.log_info('Interrupt received...')
rescue Exception => e # rubocop:disable RescueException
  Gitdocs.log_error(
    "#{e.class.inspect} - #{e.inspect} - #{e.message.inspect}"
  )
  Gitdocs.log_error(e.backtrace.join("\n"))
  Notifier.error(
    'Unexpected exit',
    'Something went wrong. Please see the log for details.'
  )
  raise
ensure
  Gitdocs.log_info('stopping listeners...')
  @listener.stop if @listener

  Gitdocs.log_info('stopping synchronizers...')
  @synchronization_supervisor.terminate if @synchronization_supervisor

  Gitdocs.log_info('terminate supervisor...')
  @supervisor.terminate if @supervisor

  Gitdocs.log_info('disconnect notifier...')
  Notifier.disconnect

  Gitdocs.log_info("Gitdocs is terminating...goodbye\n\n")
end