Sha256: 3474e614618575741635cc51e155fbfc6545be18348ed5fa2fd6e3a86562fd9e

Contents?: true

Size: 1.7 KB

Versions: 9

Compression:

Stored size: 1.7 KB

Contents

require 'stringio'

module PgHaMigrations
  class BlockingDatabaseTransactionsReporter
    CHECK_DURATION = "30 seconds"

    def self.run
      blocking_transactions = get_blocking_transactions
      has_transactions = blocking_transactions.values.flatten.present?
      _puts(report(blocking_transactions)) if has_transactions
    end

    def self.report(transactions)
      report = StringIO.new
      report << "Potentially blocking transactions:\n"
      transactions.each do |db_description, blocking_transactions|
        report << "#{db_description}:\n"
        if blocking_transactions.empty?
          report << "\t(no long running transactions)\n\n"
        else
          blocking_transactions.each do |transaction|
            report << "\t#{transaction.description}\n\n"
          end
        end

        if blocking_transactions.any?(&:concurrent_index_creation?)
          report << <<~eos.lines.map { |line| "\t#{line}" }.join
            Warning: concurrent indexes are currently being built. If you have any other
                     migrations in this deploy that will attempt to create additional
                     concurrent indexes on the same physical database (even if the table
                     being indexes is on another dimension) those migrations will not be
                     able to complete until the in-progress index creations finish.\n
          eos
          report << "\n" # Blank line intentional
        end
      end
      report.string
    end

    def self.get_blocking_transactions
      {
        "Primary database" => PgHaMigrations::BlockingDatabaseTransactions.find_blocking_transactions(CHECK_DURATION)
      }
    end

    def self._puts(msg)
      puts msg
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pg_ha_migrations-1.8.0 lib/pg_ha_migrations/blocking_database_transactions_reporter.rb
pg_ha_migrations-1.7.0 lib/pg_ha_migrations/blocking_database_transactions_reporter.rb
pg_ha_migrations-1.6.0 lib/pg_ha_migrations/blocking_database_transactions_reporter.rb
pg_ha_migrations-1.5.0 lib/pg_ha_migrations/blocking_database_transactions_reporter.rb
pg_ha_migrations-1.4.0 lib/pg_ha_migrations/blocking_database_transactions_reporter.rb
pg_ha_migrations-1.3.0 lib/pg_ha_migrations/blocking_database_transactions_reporter.rb
pg_ha_migrations-1.2.5 lib/pg_ha_migrations/blocking_database_transactions_reporter.rb
pg_ha_migrations-1.2.4 lib/pg_ha_migrations/blocking_database_transactions_reporter.rb
pg_ha_migrations-1.2.3 lib/pg_ha_migrations/blocking_database_transactions_reporter.rb