Class: Gitdocs::Manager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_root, debug) {|@config| ... } ⇒ Manager

Returns a new instance of Manager.

Yields:



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

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?
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/gitdocs/manager.rb', line 7

def config
  @config
end

#debugObject (readonly)

Returns the value of attribute debug.



7
8
9
# File 'lib/gitdocs/manager.rb', line 7

def debug
  @debug
end

Instance Method Details

#log(msg, level = :info) ⇒ Object

Logs and outputs to file or stdout based on debugging state log(“message”)



59
60
61
# File 'lib/gitdocs/manager.rb', line 59

def log(msg, level = :info)
  @debug ? puts(msg) : @logger.send(level, msg)
end

#remove_by_id(id) ⇒ Object



82
83
84
# File 'lib/gitdocs/manager.rb', line 82

def remove_by_id(id)
  config.remove_by_id(id)
end

#restartObject



45
46
47
48
49
50
51
# File 'lib/gitdocs/manager.rb', line 45

def restart
  Thread.new do
    Thread.main.raise Restart, 'restarting ... '
    sleep 0.1 while EM.reactor_running?
    start
  end
end

#sharesObject



71
72
73
# File 'lib/gitdocs/manager.rb', line 71

def shares
  config.shares
end

#start(web_port = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gitdocs/manager.rb', line 16

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(', ')}")

  begin
    EM.run do
      log('Starting EM loop...')

      @runners = Runner.start_all(shares)
      repositories = shares.map { |x| Repository.new(x) }
      Server.start_and_wait(self, web_port, repositories)
    end
  rescue Restart
    retry
  end
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(
    'Unexpected exit',
    'Something went wrong. Please see the log for details.'
  )
  raise
ensure
  log("Gitdocs is terminating...goodbye\n\n")
end

#start_web_frontendObject



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

def start_web_frontend
  config.start_web_frontend
end

#stopObject



53
54
55
# File 'lib/gitdocs/manager.rb', line 53

def stop
  EM.stop
end

#update_all(new_config) ⇒ Object



76
77
78
79
# File 'lib/gitdocs/manager.rb', line 76

def update_all(new_config)
  config.update_all(new_config)
  EM.add_timer(0.1) { restart }
end

#web_frontend_portObject



67
68
69
# File 'lib/gitdocs/manager.rb', line 67

def web_frontend_port
  config.web_frontend_port
end