Class: SensuGenerator::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu_generator/application.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:, logger:, notifier:, trigger:) ⇒ Application

Returns a new instance of Application.



23
24
25
26
27
28
29
# File 'lib/sensu_generator/application.rb', line 23

def initialize(config:, logger:, notifier:, trigger:)
  @@logger   = logger
  @@notifier = notifier
  @@config   = config
  @@trigger  = trigger
  @threads = []
end

Class Method Details

.configObject



14
15
16
# File 'lib/sensu_generator/application.rb', line 14

def config
  @@config
end

.loggerObject



6
7
8
# File 'lib/sensu_generator/application.rb', line 6

def logger
  @@logger
end

.notifierObject



10
11
12
# File 'lib/sensu_generator/application.rb', line 10

def notifier
  @@notifier
end

.triggerObject



18
19
20
# File 'lib/sensu_generator/application.rb', line 18

def trigger
  @@trigger
end

Instance Method Details

#configObject



39
40
41
# File 'lib/sensu_generator/application.rb', line 39

def config
  @@config
end

#loggerObject



31
32
33
# File 'lib/sensu_generator/application.rb', line 31

def logger
  @@logger
end

#notifierObject



35
36
37
# File 'lib/sensu_generator/application.rb', line 35

def notifier
  @@notifier
end

#runObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sensu_generator/application.rb', line 89

def run
  logger.info "Starting application #{VERSION}v in #{config.get[:mode]} mode"
  threads = %w(generator)
  if config.get[:mode] == 'server'
    threads << 'restarter'
    threads << 'server' if config.get[:server][:port]
  end
  threads.each do |thr|
    @threads << run_thread(thr)
  end

  loop do
    @threads.each do |thr|
      unless thr.alive?
        @threads.delete thr
        @threads << run_thread(thr.name)
      logger.error "#{thr.name.capitalize} is NOT ALIVE. Trying to restart."
      end
    end
    sleep 60
  end
end

#run_generatorObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sensu_generator/application.rb', line 60

def run_generator
  logger.info "Starting generator..."
  generator.flush_results if config.get[:mode] == 'server'
  state = ConsulState.new
  loop do
    logger.info 'Generator is alive!'
    if state.changed? && state.actualized?
      generator.services = state.changes
      list = generator.generate!
      logger.info "#{list.size} files processed: #{list.join(', ')}"
      if config.get[:mode] == 'server' && list.empty? && state.changes.any? { |svc| svc.name == config.get[:sensu][:service] }
        logger.info "Sensu-server service state was changed"
        trigger.touch
      end
    end
    sleep 60
    state.actualize
  end
rescue => e
  raise ApplicationError, "Generator error:\n\t #{e.to_s}\n\t #{e.backtrace}"
end

#run_restarterObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/sensu_generator/application.rb', line 47

def run_restarter
  logger.info "Starting restarter..."
  loop do
    logger.info 'Restarter is alive!'
    if restarter.need_to_apply_new_configs?
      restarter.perform_restart
    end
    sleep 60
  end
rescue => e
  raise ApplicationError, "Restarter error:\n\t #{e.to_s}\n\t #{e.backtrace}"
end

#run_serverObject



82
83
84
85
86
87
# File 'lib/sensu_generator/application.rb', line 82

def run_server
  server = Server.new
rescue => e
  server&.close
  raise ApplicationError, "Server error:\n\t #{e.to_s}\n\t #{e.backtrace}"
end

#triggerObject



43
44
45
# File 'lib/sensu_generator/application.rb', line 43

def trigger
  @@trigger
end