Sha256: a52cc87f1587085b00325716fe3599a2639b965bcc3858fe4a7969d88ddd412f

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module RailsFailover
  module ActiveRecord
    class Railtie < ::Rails::Railtie
      initializer "rails_failover.init", after: "active_record.initialize_database" do |app|
        app.config.active_record_rails_failover = false
        config = RailsFailover::ActiveRecord.config
        next unless config[:replica_host] && config[:replica_port]

        app.config.active_record_rails_failover = true
        ::ActiveSupport.on_load(:active_record) do
          begin
            ::ActiveRecord::Base.connection.verify!
          rescue ::ActiveRecord::NoDatabaseError
            # Do nothing since database hasn't been created
          rescue ::PG::Error, ::ActiveRecord::ConnectionNotEstablished
            Handler.instance.verify_primary(RailsFailover::ActiveRecord.writing_role)
          end
        end
      end

      initializer "rails_failover.insert_middleware" do |app|
        next unless app.config.active_record_rails_failover

        ActionDispatch::DebugExceptions.register_interceptor do |request, exception|
          RailsFailover::ActiveRecord::Interceptor.handle(request, exception)
        end

        if !skip_middleware?(app.config)
          app.middleware.unshift(RailsFailover::ActiveRecord::Middleware)
        end
      end

      def skip_middleware?(config)
        config.try(:skip_rails_failover_active_record_middleware)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_failover-2.1.1 lib/rails_failover/active_record/railtie.rb