lib/postgres/vacuum/jobs/monitor_job.rb in postgres-vacuum-monitor-0.10.1 vs lib/postgres/vacuum/jobs/monitor_job.rb in postgres-vacuum-monitor-0.11.0

- old
+ new

@@ -1,15 +1,17 @@ +# frozen_string_literal: true + module Postgres module Vacuum module Jobs class MonitorJob - AUTOVACUUM_LAGGING_EVENT = 'AutoVacuumLagging'.freeze - LONG_TRANSACTIONS = 'LongTransactions'.freeze - BLOCKED_QUERIES = 'BlockedQueries'.freeze - CONNECTION_STATE = 'ConnectionState'.freeze - CONNECTION_IDLE_TIME = 'ConnectionIdleTime'.freeze + AUTOVACUUM_LAGGING_EVENT = 'AutoVacuumLagging' + LONG_TRANSACTIONS = 'LongTransactions' + BLOCKED_QUERIES = 'BlockedQueries' + CONNECTION_STATE = 'ConnectionState' + CONNECTION_IDLE_TIME = 'ConnectionIdleTime' def perform(*) with_each_db_name_and_connection do |name, connection| connection.execute(Postgres::Vacuum::Monitor::Query.long_running_transactions).each do |row| reporter_class.report_event( @@ -49,11 +51,16 @@ current_statement_in_blocking_process: row['current_statement_in_blocking_process'] ) end connection.execute(Postgres::Vacuum::Monitor::Query.connection_state).each do |row| - reporter_class.report_event(CONNECTION_STATE, database_name: name, state: row['state'], connection_count: row['connection_count']) + reporter_class.report_event( + CONNECTION_STATE, + database_name: name, + state: row['state'], + connection_count: row['connection_count'] + ) end connection.execute(Postgres::Vacuum::Monitor::Query.connection_idle_time).each do |row| reporter_class.report_event( CONNECTION_IDLE_TIME, @@ -70,18 +77,25 @@ def reporter_class return @reporter_class_name if @reporter_class_name @reporter_class_name = Postgres::Vacuum::Monitor.configuration.monitor_reporter_class_name&.safe_constantize - raise ConfigurationError.new('Missing or invalid report class name. Check your configuration') if @reporter_class_name.nil? + if @reporter_class_name.nil? + raise ConfigurationError.new('Missing or invalid report class name. Check your configuration') + end @reporter_class_name end def with_each_db_name_and_connection databases = Set.new ActiveRecord::Base.connection_handler.connection_pools.map do |connection_pool| - db_name = connection_pool.spec.config[:database] + db_name = if Postgres::Vacuum::Compatibility.pre_rails_6_1? + connection_pool.spec.config[:database] + else + connection_pool.db_config.configuration_hash[:database] + end + # activerecord allocates a connection pool per call to establish_connection # multiple pools might interact with the same database so we use the # database name to dedup connection_pool.with_connection { |conn| yield(db_name, conn) } if databases.add?(db_name) end