Class: SensuGenerator::Restarter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers) ⇒ Restarter

Returns a new instance of Restarter.



8
9
10
11
12
13
14
15
# File 'lib/sensu_generator/restarter.rb', line 8

def initialize(servers)
  @delay = 0
  @delay_inc = 600
  @config  = Application.config
  @trigger = Application.trigger
  @logger  = Application.logger
  @servers = servers
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#need_to_apply_new_configs?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
# File 'lib/sensu_generator/restarter.rb', line 46

def need_to_apply_new_configs?
  result = (@trigger.difference_between_touches > 120 || @trigger.last_touch_age > 120) && @trigger.modified_since_last_update?
  logger.debug "\n  Trigger:\n\tdifference_between_touches: #{@trigger.difference_between_touches}"\
                "\n\tlast_touch_age: #{@trigger.last_touch_age}"\
                "\n\tmodified_since_last_update?: #{@trigger.modified_since_last_update?}"\
                "\n\tNeeds to apply configuration to Sensu? #{result}"
  result
end

#perform_restartObject



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
44
# File 'lib/sensu_generator/restarter.rb', line 17

def perform_restart
  servers_updated = []

  @servers.each do |server|
    begin
      if @servers.size < config.get[:sensu][:minimal_to_restart]
        msg = "Sensu-servers count < #{config.get[:sensu][:minimal_to_restart]}. Restart will not be performed. Next try after #{@delay + @delay_inc}s."
        raise RestarterError, msg
        @delay += @delay_inc if @delay < 3600
        sleep @delay
        break
      end

      server.sync && server.restart && servers_updated << server.address

      if server == @servers.last
        if servers_updated.size == @servers.size
          @trigger.clear
        else
          raise RestarterError, "Could not synchronize or restart #{(@servers.map(&:address) - servers_updated).join(',')}"
        end
      end
    rescue => e
      logger.error "Restarter error: #{e.inspect} #{e.backtrace}"
      next
    end
  end
end