lib/gitdocs/manager.rb in gitdocs-0.5.0 vs lib/gitdocs/manager.rb in gitdocs-0.6.0

- old
+ new

@@ -2,85 +2,115 @@ module Gitdocs Restart = Class.new(RuntimeError) class Manager - attr_reader :config, :debug + # @param (see #start) + # @return (see #start) + def self.start(web_port) + Manager.new.start(web_port) + end - def initialize(config_root, debug) - @config = Configuration.new(config_root) - @logger = Logger.new(File.expand_path('log', @config.config_root)) - @debug = debug - yield @config if block_given? + # @return [void] + def self.restart_synchronization + Thread.main.raise(Restart, 'restarting ... ') end - def start(web_port = nil) - log("Starting Gitdocs v#{VERSION}...") - log("Using configuration root: '#{config.config_root}'") - log("Shares: (#{shares.length}) #{shares.map(&:inspect).join(', ')}") + # @return [:notification, :polling] + def self.listen_method + return :polling if Listen::Adapter.select == Listen::Adapter::Polling + :notification + end - begin - EM.run do - log('Starting EM loop...') + # @param [Integer] web_port + # @return [void] + def start(web_port) + Gitdocs.log_info("Starting Gitdocs v#{VERSION}...") + Gitdocs.log_info( + "Using configuration root: '#{Initializer.root_dirname}'" + ) - @runners = Runner.start_all(shares) - repositories = shares.map { |x| Repository.new(x) } - Server.start_and_wait(self, web_port, repositories) + 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 - rescue Restart - retry + @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 + Gitdocs.log_info('Interrupt received...') rescue Exception => e # rubocop:disable RescueException - # Report all errors in log - log("#{e.class.inspect} - #{e.inspect} - #{e.message.inspect}", :error) - log(e.backtrace.join("\n"), :error) - Gitdocs::Notifier.error( + 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 - log("Gitdocs is terminating...goodbye\n\n") - end + Gitdocs.log_info('stopping listeners...') + @listener.stop if @listener - def restart - Thread.new do - Thread.main.raise Restart, 'restarting ... ' - sleep 0.1 while EM.reactor_running? - start - end - end + Gitdocs.log_info('stopping synchronizers...') + @synchronization_supervisor.terminate if @synchronization_supervisor - def stop - EM.stop - end + Gitdocs.log_info('terminate supervisor...') + @supervisor.terminate if @supervisor - # Logs and outputs to file or stdout based on debugging state - # log("message") - def log(msg, level = :info) - @debug ? puts(msg) : @logger.send(level, msg) - end + Gitdocs.log_info('disconnect notifier...') + Notifier.disconnect - def start_web_frontend - config.start_web_frontend - end - - def web_frontend_port - config.web_frontend_port - end - - def shares - config.shares - end - - # @see Gitdocs::Configuration#update_all - def update_all(new_config) - config.update_all(new_config) - EM.add_timer(0.1) { restart } - end - - # @see Gitdocs::Configuration#remove_by_id - def remove_by_id(id) - config.remove_by_id(id) + Gitdocs.log_info("Gitdocs is terminating...goodbye\n\n") end end end