Class: ManageIQ::PostgresHaAdmin::FailoverMonitor

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

Constant Summary collapse

RAILS_ROOT =
[
  Pathname.new("/var/www/miq/vmdb"),
  Pathname.new(File.expand_path(File.join(__dir__, "../..")))
].detect { |f| File.exist?(f) }
FAILOVER_ATTEMPTS =
10
DB_CHECK_FREQUENCY =
300
FAILOVER_CHECK_FREQUENCY =
60
LOG_FILE =
'/var/www/miq/vmdb/log/ha_admin.log'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_yml_file: '/var/www/miq/vmdb/config/database.yml', failover_yml_file: '/var/www/miq/vmdb/config/failover_databases.yml', ha_admin_yml_file: '/var/www/miq/vmdb/config/ha_admin.yml', logger: nil, environment: 'production') ⇒ FailoverMonitor

Returns a new instance of FailoverMonitor.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 18

def initialize(db_yml_file: '/var/www/miq/vmdb/config/database.yml',
               failover_yml_file: '/var/www/miq/vmdb/config/failover_databases.yml',
               ha_admin_yml_file: '/var/www/miq/vmdb/config/ha_admin.yml',
               logger: nil,
               environment: 'production')
  if logger.respond_to?(:error, :info)
    @logger = logger
  else
    @logger = Logger.new(LOG_FILE)
    @logger.level = Logger::INFO
  end
  @database_yml = DatabaseYml.new(db_yml_file, environment)
  @failover_db = FailoverDatabases.new(failover_yml_file, @logger)
  initialize_settings(ha_admin_yml_file)
end

Instance Attribute Details

#db_check_frequencyObject

Returns the value of attribute db_check_frequency.



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

def db_check_frequency
  @db_check_frequency
end

#failover_attemptsObject

Returns the value of attribute failover_attempts.



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

def failover_attempts
  @failover_attempts
end

#failover_check_frequencyObject

Returns the value of attribute failover_check_frequency.



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

def failover_check_frequency
  @failover_check_frequency
end

Instance Method Details

#active_servers_conninfoObject



65
66
67
68
69
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 65

def active_servers_conninfo
  servers = @failover_db.active_databases_conninfo_hash
  db_yml_params = @database_yml.pg_params_from_database_yml
  servers.map! { |info| db_yml_params.merge(info) }
end

#monitorObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 34

def monitor
  connection = pg_connection(@database_yml.pg_params_from_database_yml)
  if connection
    @failover_db.update_failover_yml(connection)
    connection.finish
    return
  end

  @logger.error("Primary Database is not available. EVM server stop initiated. Starting to execute failover...")
  stop_evmserverd

  if execute_failover
    start_evmserverd
    raise_failover_event
  else
    @logger.error("Failover failed")
  end
end

#monitor_loopObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 53

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

#raise_failover_eventObject



71
72
73
74
75
76
# File 'lib/manageiq/postgres_ha_admin/failover_monitor.rb', line 71

def raise_failover_event
  require "awesome_spawn"
  AwesomeSpawn.run("rake evm:raise_server_event",
                   :chdir  => RAILS_ROOT,
                   :params => ["--", {:event  => "db_failover_executed"}])
end