Sha256: 61b1de51489239abfe9a3b77772e9cbd709e4e64f92a1c7486d69953f0704a7f

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

module Backend; class BackendDB
    def initialize conf
        @query = conf.delete :query
        @conf = conf
        setup_db
    end

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

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

        rescue ActiveRecord::StatementInvalid => e
            Log.e "Query error. Trying to reconnect. 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.exc e
        end

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

    def allow_cache
        true
    end

    private

    def build_query dom_name
        @query.sub '$domain', dom_name
    end

    def setup_db
        ActiveRecord::Base.logger = Log.logger

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

        begin
            require gem_name.to_s

        rescue LoadError => e
            Util.die "Error loading database dependency.\nMake sure #{gem_name} it is installed, if it is not, install with 'gem install #{gem_name}'\nError Details: #{e.desc}"
        rescue => e
            Util.die "Error loading database dependency.\n#{e.desc}"
        end

        ActiveRecord::Base.establish_connection @conf
    end
  
end; end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dns_one-0.4.13 lib/dns_one/backend/db.rb
dns_one-0.4.12 lib/dns_one/backend/db.rb
dns_one-0.4.11 lib/dns_one/backend/db.rb
dns_one-0.4.10 lib/dns_one/backend/db.rb
dns_one-0.4.8 lib/dns_one/backend/db.rb
dns_one-0.4.7 lib/dns_one/backend/db.rb