influitive / apartment

Database multi-tenancy for Rack (and Rails) applications
2.67k stars 463 forks source link

A copy of `Generic Elevator Class` has been removed from the module tree but is still active! #580

Closed alencarandre closed 5 years ago

alencarandre commented 5 years ago

Steps to reproduce

When gives some error, for instance, try read a file and not found, after fix without restart server, gives this error:

A copy of Elevator has been removed from the module tree but is still active!

image

Expected behavior

When fix the issue, return normal execution

Actual behavior

Gives error

System configuration

alencarandre commented 5 years ago

Well, the truth, I do not know reproduce that, this is happening without a pattern, I can't recognize a pattern. It happened without gives an error ¯_(ツ)_/¯

Here, my elevator class

class Pzcommerce::Elevator < Apartment::Elevators::Generic
  def parse_tenant_name(request)
    host = request.host

    request_discovery = Pzcommerce::RequestDiscovery.new(request)

    domain = request_discovery.domain
    subdomain = request_discovery.subdomain
    master_domain = request_discovery.master_domain

    tenant = nil
    tenant_group = nil

    if "#{domain}" == "#{master_domain}"
      if "#{subdomain}" != "master"
        tenant_group = TenantGroup.find_by(subdomain: subdomain)
        raise_route_error(request) if tenant_group.blank?
      end
    else
      host = domain if "#{subdomain}" == "www"

      tenant = Tenant.unscoped.find_by(domain: host)
      raise_route_error(request) if tenant.blank?

      tenant_group = tenant.tenant_group
    end

    TenantGroup.current = tenant_group
    Tenant.current = tenant

    return Tenant.current.try(:apartment_name) || 'public'
  end

  private

  def raise_route_error(request)
    raise ActionController::RoutingError, "Domain #{request.host} Not Found"
  end
end

Here, my apartment.rb on initialize

require 'apartment/elevators/generic'

Apartment.configure do |config|
  config.excluded_models = %w{ Tenant TenantGroup }
  config.tenant_names = lambda { Tenant.pluck :apartment_name }
end

Rails.application.config.middleware.use Pzcommerce::Elevator

My application.rb on initialize

require_relative 'boot'

require 'rails/all'

Bundler.require(*Rails.groups)

module PzcommerceRails
  class Application < Rails::Application
    config.load_defaults 6.0

    config.middleware.delete ActionDispatch::HostAuthorization
  end
end
alencarandre commented 5 years ago

It seems to me a rails issue, and then I opened there as well https://github.com/rails/rails/issues/35066

mikecmpbll commented 5 years ago

at what file path is Pzcommerce::Elevator defined?

as an aside, i've seen this kind of autoloading error before related to spring, the application preloader. are you using that by any chance?