Skip to content

Commit

Permalink
Tenant job context fine-tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudgg committed Nov 16, 2024
1 parent 83d9807 commit 6f9adba
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ GEM
mini_mime (1.1.5)
mini_portile2 (2.8.8)
minitest (5.25.1)
mission_control-jobs (0.5.0)
mission_control-jobs (0.6.0)
actioncable (>= 7.1)
actionpack (>= 7.1)
activejob (>= 7.1)
Expand Down Expand Up @@ -539,7 +539,7 @@ GEM
slim (5.2.1)
temple (~> 0.10.0)
tilt (>= 2.1.0)
solid_queue (1.0.1)
solid_queue (1.0.2)
activejob (>= 7.1)
activerecord (>= 7.1)
concurrent-ruby (>= 1.3.1)
Expand All @@ -554,16 +554,16 @@ GEM
stimulus-rails (1.3.4)
railties (>= 6.0.0)
stringio (3.1.2)
super_diff (0.13.0)
super_diff (0.14.0)
attr_extras (>= 6.2.4)
diff-lcs
patience_diff
tailwindcss-rails (3.0.0)
railties (>= 7.0.0)
tailwindcss-ruby
tailwindcss-ruby (3.4.14)
tailwindcss-ruby (3.4.14-arm64-darwin)
tailwindcss-ruby (3.4.14-x86_64-linux)
tailwindcss-ruby (3.4.15)
tailwindcss-ruby (3.4.15-arm64-darwin)
tailwindcss-ruby (3.4.15-x86_64-linux)
temple (0.10.3)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
Expand Down
6 changes: 3 additions & 3 deletions app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

class ApplicationJob < ActiveJob::Base
include TenantSwitcher
include TenantContext

# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked
retry_on ActiveRecord::Deadlocked

# Most jobs are safe to ignore if the underlying records are no longer available
# discard_on ActiveJob::DeserializationError
discard_on ActiveJob::DeserializationError
end
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
# frozen_string_literal: true

module TenantSwitcher
module TenantContext
extend ActiveSupport::Concern

included do
around_perform do |job, block|
begin
context = job.arguments.pop
Tenant.switch(context["tenant"]) do
Current.set(context["current"], &block)
end
ensure
job.set_context(context)
end
job.with_context(&block)
end
end

Expand All @@ -24,14 +17,23 @@ def serialize
super
end

def with_context(&block)
context = arguments.pop
Tenant.switch(context["tenant"]) do
Current.set(context["current"], &block)
end
ensure
set_context(context)
end

private

def set_context(context)
return if context_set?(context)

self.arguments << context
end

private

def context_set?(context)
last_argument = self.arguments&.last
return unless last_argument.is_a?(Hash)
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/action_mailer_job_tenant.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

Rails.application.config.after_initialize do
ActionMailer::MailDeliveryJob.include(TenantSwitcher)
ActionMailer::MailDeliveryJob.include(TenantContext)
end
2 changes: 1 addition & 1 deletion config/initializers/active_storage_job_tenant.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

Rails.application.config.after_initialize do
ActiveStorage::BaseJob.include(TenantSwitcher)
ActiveStorage::BaseJob.include(TenantContext)
end
2 changes: 1 addition & 1 deletion lib/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def disconnect
private

def enter(tenant)
return if tenant == current
raise "Unknown tenant '#{tenant}'" unless exists?(tenant)
return if tenant == current
raise "Illegal tenant switch (#{current} => #{tenant})" unless outside?

self.current = tenant
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "rails_helper"

describe TenantSwitcher do
describe TenantContext do
specify "add current attributes and tenant last arguments" do
class DummyJob < ActiveJob::Base
include TenantSwitcher
include TenantContext
def perform(admin, name: nil)
admin.update!(name: name)
end
Expand Down Expand Up @@ -31,7 +31,7 @@ def perform(admin, name: nil)

specify "retry with the same current attributes and tenant last arguments" do
class DummyExceptionJob < ActiveJob::Base
include TenantSwitcher
include TenantContext
retry_on Exception, wait: :polynomially_longer, attempts: 2

def perform(foo)
Expand Down

0 comments on commit 6f9adba

Please sign in to comment.