Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to Handle Apartment::TenantNotFound errors #6

Open
Martin-Nyaga opened this issue Apr 8, 2018 · 2 comments
Open

No way to Handle Apartment::TenantNotFound errors #6

Martin-Nyaga opened this issue Apr 8, 2018 · 2 comments

Comments

@Martin-Nyaga
Copy link

We implemented multitenancy on a medium-large rails 4.2 app using apartment, and used this gem to allow jobs to be executed under the correct tenant. However, After using this for sometime, we are seeing Apartment::TenantNotFound errors for jobs scheduled in the future which, due to events in the normal application lifecycle, are run when the tenant has been deleted.

Because this gem is implemented to switch tenants before ActiveJob can set up callbacks and error handling, we are not able to handle the error using ActiveJob's built in error handling:

class ApplicationJob < ActiveJob::Base
  # Would be ideal but does not work
  rescue_from Apartment::TenantNotFound do |e|
    # Handle error ...
  end
end

Given the nature of jobs, it is expected that some of the data might not be available at the time of running the job. This gem should therefore not assume existence of the tenant when the job is running.

In our application, we are using this hacked version of code pulled out of this gem in the meantime:

module Apartment
  module CustomActiveJobExtension
    extend ActiveSupport::Concern

    class_methods do
      def execute(job_data)
        Apartment::Tenant.switch(job_data['tenant']) do
          super
        end
      rescue Apartment::TenantNotFound
        # our custom error handling ...
      end
    end

    def serialize
      super.merge('tenant' => Apartment::Tenant.current)
    end
  end
end

class ActiveJob::Base
  include Apartment::CustomActiveJobExtension
end

It would be better if this were exposed as some kind of configuration; perhaps something like this:

Apartment::ActiveJob.configure do |config|
  # Assign handler as any object with a #call method
  # Examples:
  config.tenant_not_found_error_handler = -> (e) { ExceptionNotifier.notify(e) }
  config.tenant_not_found_error_handler = MyCustomErrorHandler.new
end
@uxxman
Copy link

uxxman commented Oct 17, 2020

@Martin-Nyaga if you are still looking for a fix for this, you can try out https://github.com/uxxman/apartment_job. Since it uses ActiveJob's callbacks instead of overriding the base execute method like this library, you can use all ActiveJob's error handling options, e.g.

rescue_from Apartment::TenantNotFound

@Martin-Nyaga
Copy link
Author

@uxxman thanks for pointing that out. The monkey patch above has worked for us for 2 years now with no issues, so we don't really have any incentive to change. But I do agree that I the around_perform implementation is much cleaner for error handling, I'll definitely keep it in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants