Sha256: 1241da4ca344c5df0e1354b6eb0a316ed93e0ab7af2b88d9d536d3ebfdfa443d

Contents?: true

Size: 1.89 KB

Versions: 21

Compression:

Stored size: 1.89 KB

Contents

module DnsOne; module Backend; class DB

    def initialize conf
        @query = conf.delete :query
        @conf = conf
        setup_db
    end

    def find dom_name, tries = 1
        return if tries > 3

        # http://jakeyesbeck.com/2016/02/14/ruby-threads-and-active-record-connections/
        res = ActiveRecord::Base.connection_pool.with_connection do
            ActiveRecord::Base.connection.execute build_query(dom_name)
        end

        first_record = res&.first
        record_values = first_record&.values
        record_values&.first

    rescue ActiveRecord::StatementInvalid => e
        Log.e "SQL query error. Trying to reconnect #{tries}. Details:\n#{e.desc}"
        # http://geoff.evason.name/2015/01/18/postgres-ssl-connection-has-been-closed-unexpectedly
        ActiveRecord::Base.connection.reconnect! 
        find dom_name, (tries+1)
    rescue => e
        Log.e "SQL query error. Details:\n#{e.desc}"
    end

    def allow_cache
        true
    end

    private

    def build_query dom_name
        @query.sub "'$domain'", 
                    ActiveRecord::Base.connection.quote(dom_name)
    end

    def setup_db
        #require_deps
        ActiveRecord::Base.logger = Log.logger
        ActiveRecord::Base.establish_connection @conf
    end

    def require_deps
        gem_name = { 
            'postgresql' => 'pg',
            'mysql'      => 'mysql',
            'mysql2'     => 'mysql2'
        }[ @conf[:adapter] ]
          
        unless gem_name
            Util.die "Database adapter '#{@conf[:adapter]}' not supported. Aborting."
        end

        ['active_record', gem_name].each do |req_name|
            begin
                require req_name
            rescue StandardError, LoadError => e
                Util.die "Error on 'require #{req_name}', install with 'gem install #{req_name}'.\nError Details: #{e.desc}"
            end
        end
    end
  
end; end; end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
dns_one-0.4.48 lib/dns_one/backend/db.rb
dns_one-0.4.47 lib/dns_one/backend/db.rb
dns_one-0.4.46 lib/dns_one/backend/db.rb
dns_one-0.4.45 lib/dns_one/backend/db.rb
dns_one-0.4.44 lib/dns_one/backend/db.rb
dns_one-0.4.43 lib/dns_one/backend/db.rb
dns_one-0.4.42 lib/dns_one/backend/db.rb
dns_one-0.4.41 lib/dns_one/backend/db.rb
dns_one-0.4.40 lib/dns_one/backend/db.rb
dns_one-0.4.39 lib/dns_one/backend/db.rb
dns_one-0.4.37 lib/dns_one/backend/db.rb
dns_one-0.4.36 lib/dns_one/backend/db.rb
dns_one-0.4.35 lib/dns_one/backend/db.rb
dns_one-0.4.34 lib/dns_one/backend/db.rb
dns_one-0.4.33 lib/dns_one/backend/db.rb
dns_one-0.4.32 lib/dns_one/backend/db.rb
dns_one-0.4.31 lib/dns_one/backend/db.rb
dns_one-0.4.30 lib/dns_one/backend/db.rb
dns_one-0.4.29 lib/dns_one/backend/db.rb
dns_one-0.4.28 lib/dns_one/backend/db.rb