Sha256: cf5096d6731fa2f87589aca66c843fa01f5b78eaabcff6c2c43f682702041eb7

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

module PandaPal::Plugins
  class ApartmentDelayedJobsPlugin < ::Delayed::Plugin
    callbacks do |lifecycle|
      lifecycle.around(:enqueue) do |job, *args, &block|
        current_tenant = Apartment::Tenant.current

        #make sure enqueue on public tenant unless we are testing since delayed job is set to run immediately
        Apartment::Tenant.switch!('public') unless Rails.env.test?
        job.tenant = current_tenant
        begin
          block.call(job, *args)
        rescue Exception => e
          Rails.logger.error("Error enqueing job #{job.to_s} - #{e.backtrace}")
        ensure
          #switch back to prev tenant
          Apartment::Tenant.switch!(current_tenant)
        end
      end


      lifecycle.before(:perform) do |worker, *args, &block|
        tenant = args.first.tenant
        Apartment::Tenant.switch!(tenant) if tenant.present?
        Rails.logger.debug("Running job with tenant #{Apartment::Tenant.current}")
      end

      lifecycle.around(:invoke_job) do |job, *args, &block|
        block.call(job, *args)
        Apartment::Tenant.switch!('public')
        Rails.logger.debug("Resetting Tenant back to: #{Apartment::Tenant.current}")
      end

      lifecycle.after(:failure) do |job, *args|
        Rails.logger.error("Job failed on tenant: #{Apartment::Tenant.current}")
      end


    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
panda_pal-0.0.7 lib/panda_pal/plugins/apartment_delayed_jobs_plugin.rb
panda_pal-0.0.6 lib/panda_pal/plugins/apartment_delayed_jobs_plugin.rb
panda_pal-0.0.5 lib/panda_pal/plugins/apartment_delayed_jobs_plugin.rb
panda_pal-0.0.4 lib/panda_pal/plugins/apartment_delayed_jobs_plugin.rb
panda_pal-0.0.3 lib/panda_pal/plugins/apartment_delayed_jobs_plugin.rb
panda_pal-0.0.2 lib/panda_pal/plugins/apartment_delayed_jobs_plugin.rb