Sha256: 4745aa07e9991e5d5058df839d4c0067c518eda03e258e65e5e2fb7326e3e3da

Contents?: true

Size: 934 Bytes

Versions: 1

Compression:

Stored size: 934 Bytes

Contents

# frozen_string_literal: true

require 'concurrent'

module PgbConnectionReaperRb
  module Reaper
    def initialize(*args)
      super
      
      config = get_db_config
      @connected_at = Concurrent.monotonic_time
      @pgb_ttl = self.class.type_cast_config_to_integer(config.fetch(:pgb_ttl, nil))
    end

    attr_reader :connected_at, :pgb_ttl

    def reconnect!(*args)
      super(*args)
      @connected_at = Concurrent.monotonic_time
    end

    def verify!(*args)
      super(*args)
      reconnect! if eligible?
    end

    private

    def age
      Concurrent.monotonic_time - @connected_at
    end

    def eligible?
      pgb_ttl && age > pgb_ttl
    end

    def get_db_config
      if ActiveRecord::Base.respond_to?(:connection_db_config)
        ActiveRecord::Base.connection_db_config.configuration_hash
      else
        ActiveRecord::Base.connection_config
      end
    rescue
      {}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pgb_connection_reaper_rb-0.1.1 lib/pgb_connection_reaper_rb/reaper.rb