Class: ManageIQ::PostgresHaAdmin::FailoverMonitor

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/manageiq/postgres_ha_admin/failover_monitor.rb

Constant Summary collapse

FAILOVER_ATTEMPTS =
10
DB_CHECK_FREQUENCY =
120
FAILOVER_CHECK_FREQUENCY =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log_prefix, #logger

Constructor Details

#initialize(config_path = "") ⇒ FailoverMonitor

Returns a new instance of FailoverMonitor.



15
16
17
18
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 15

def initialize(config_path = "")
  initialize_settings(config_path)
  @config_handlers = []
end

Instance Attribute Details

#config_handlersObject (readonly)

Returns the value of attribute config_handlers.



13
14
15
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 13

def config_handlers
  @config_handlers
end

#db_check_frequencyObject

Returns the value of attribute db_check_frequency.



12
13
14
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 12

def db_check_frequency
  @db_check_frequency
end

#failover_attemptsObject

Returns the value of attribute failover_attempts.



12
13
14
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 12

def failover_attempts
  @failover_attempts
end

#failover_check_frequencyObject

Returns the value of attribute failover_check_frequency.



12
13
14
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 12

def failover_check_frequency
  @failover_check_frequency
end

Instance Method Details

#active_servers_conninfo(handler, server_store) ⇒ Object



62
63
64
65
66
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 62

def active_servers_conninfo(handler, server_store)
  servers = server_store.connection_info_list
  current_params = handler.read
  servers.map! { |info| current_params.merge(info) }
end

#add_handler(handler) ⇒ Object



20
21
22
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 20

def add_handler(handler)
  @config_handlers << [handler, ServerStore.new]
end

#monitorObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 24

def monitor
  config_handlers.each do |handler, server_store|
    begin
      connection = pg_connection(handler.read)
      if connection
        server_store.update_servers(connection, handler.name)
        connection.finish
        next
      end

      log_settings
      server_store.log_current_server_store(handler.name)
      logger.error("#{log_prefix(__callee__)} Primary Database is not available for #{handler.name}. Starting to execute failover...")
      handler.do_before_failover

      new_conn_info = execute_failover(handler, server_store)

      # Upon success, we pass a connection hash
      handler.do_after_failover(new_conn_info) if new_conn_info
    rescue => e
      logger.error("#{log_prefix(__callee__)} Received #{e.class} error while monitoring #{handler.name}: #{e.message}")
      logger.error(e.backtrace)
    end
  end
end

#monitor_loopObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 50

def monitor_loop
  loop do
    begin
      monitor
    rescue => err
      logger.error("#{log_prefix(__callee__)} #{err.class}: #{err}")
      logger.error(err.backtrace.join("\n"))
    end
    sleep(db_check_frequency)
  end
end