Class: Deadpool::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/deadpool/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ Handler

Returns a new instance of Handler.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/deadpool/handler.rb', line 16

def initialize(config, logger)
  @state             = Deadpool::State.new config[:pool_name], self.class
  # @state             = Deadpool::State.new "Deadpool::Handler - #{config[:pool_name]}"
  @config            = config
  @logger            = logger
  @pool_name         = config[:pool_name]
  @check_interval    = config[:check_interval]
  @max_failed_checks = config[:max_failed_checks]
  @primary_host      = config[:primary_host]
  @secondary_host    = config[:secondary_host]
  @failure_count     = 0
  instantiate_monitor
  instantiate_failover_protocols
  @state.set_state(OK, "Handler initialized.")
end

Instance Attribute Details

#check_intervalObject (readonly)

Returns the value of attribute check_interval.



8
9
10
# File 'lib/deadpool/handler.rb', line 8

def check_interval
  @check_interval
end

#failure_countObject (readonly)

Returns the value of attribute failure_count.



8
9
10
# File 'lib/deadpool/handler.rb', line 8

def failure_count
  @failure_count
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/deadpool/handler.rb', line 7

def logger
  @logger
end

#max_failed_checksObject (readonly)

Returns the value of attribute max_failed_checks.



8
9
10
# File 'lib/deadpool/handler.rb', line 8

def max_failed_checks
  @max_failed_checks
end

#pool_nameObject (readonly)

Returns the value of attribute pool_name.



8
9
10
# File 'lib/deadpool/handler.rb', line 8

def pool_name
  @pool_name
end

#primary_hostObject (readonly)

Returns the value of attribute primary_host.



8
9
10
# File 'lib/deadpool/handler.rb', line 8

def primary_host
  @primary_host
end

#secondary_hostObject (readonly)

Returns the value of attribute secondary_host.



8
9
10
# File 'lib/deadpool/handler.rb', line 8

def secondary_host
  @secondary_host
end

#stateObject (readonly)

Returns the value of attribute state.



8
9
10
# File 'lib/deadpool/handler.rb', line 8

def state
  @state
end

Instance Method Details

#monitor_pool(timer) ⇒ Object



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
# File 'lib/deadpool/handler.rb', line 32

def monitor_pool(timer)
  if @monitor.primary_ok?
    @failure_count = 0
    @state.set_state(OK, "Primary Check OK.")
    logger.info "#{@pool_name} Primary Check Okay.  Failure Count set to 0."
  else
    @failure_count += 1
    @state.set_state(WARNING, "Primary Check failed #{@failure_count} times")
    logger.warn "#{@pool_name} Primary Check Failed.  Failure Count at #{@failure_count}"
  end

  if @failure_count >= @max_failed_checks
    timer.cancel
    @state.set_state(WARNING, "Failure threshold exceeded.  Failover Protocol Initiated.")
    logger.error "#{@pool_name} primary is dead.  Initiating Failover Protocol."

    success = true
    @failover_protocols.each do |failover_protocol|
      success = success && failover_protocol.initiate_failover_protocol!
    end

    if success
      logger.warn "Failover Protocol Finished."
      @state.set_state(WARNING, "Failover Protocol in place.")
      @state.lock
    else
      logger.error "Failover Protocol Failed!"
      @state.set_state(CRITICAL, "Failover Protocol Failed!")
      @state.lock
    end
  end
end

#promote_server(server) ⇒ Object



76
77
78
79
80
81
# File 'lib/deadpool/handler.rb', line 76

def promote_server(server)
  # This will stop at the first failure
  @config[server] && @failover_protocols.all? do |failover_protocol|
    failover_protocol.promote_to_primary @config[server]
  end
end

#system_checkObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/deadpool/handler.rb', line 65

def system_check
  snapshot = Deadpool::StateSnapshot.new @state
  snapshot.add_child @monitor.system_check
  @failover_protocols.each do |failover_protocol|
    # logger.debug failover_protocol.inspect
    snapshot.add_child failover_protocol.system_check
  end

  return snapshot
end