Skip to content

Commit

Permalink
Support ActiveJob 7.2 enqueue_after_transaction_commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuay03 committed Apr 15, 2024
1 parent 4a98d2a commit 5ddaac8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Add support for `ActiveJob::Base.enqueue_after_transaction_commit`. ([@joshuay03][])

- Add ActionCable adapter. ([@arthurWD][])

## 1.0.1 (2023-12-01) ❄️
Expand Down Expand Up @@ -143,3 +145,5 @@ This, for example, makes Isolator compatible with Rails multi-database apps.
[@Mange]: https://github.com/Mange
[@tomgi]: https://github.com/tomgi
[@tagirahmad]: https://github.com/tagirahmad
[@arthurWD]: https://github.com/arthurWD
[@joshuay03]: https://github.com/joshuay03
1 change: 1 addition & 0 deletions lib/isolator/adapter_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def call(target: nil, method_name: nil, **options)
self.exception_class = options[:exception_class] if options.key?(:exception_class)
self.exception_message = options[:exception_message] if options.key?(:exception_message)
self.details_message = options[:details_message] if options.key?(:details_message)
self.disabled = options[:disabled] if options.key?(:disabled)
end

mod = build_mod(method_name, adapter)
Expand Down
7 changes: 6 additions & 1 deletion lib/isolator/adapters/background_jobs/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
details_message: ->(obj) {
"#{obj.class.name}" \
"#{obj.arguments.any? ? " (#{obj.arguments.join(", ")})" : ""}"
}
},
disabled: begin
config = ActiveJob::Base.try(:enqueue_after_transaction_commit)
config == :always || (config == :default &&
ActiveJob::Base.queue_adapter.try(:enqueue_after_transaction_commit?) == true)
end
1 change: 1 addition & 0 deletions lib/isolator/adapters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Isolator
module Adapters
# Used as a "template" for adapters
module Base
attr_writer :disabled
attr_accessor :exception_class, :exception_message, :details_message

def disable!
Expand Down
52 changes: 52 additions & 0 deletions spec/isolator/adapters/background_jobs/active_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,57 @@
specify do
expect { ActiveJobWorker.perform_later("test") }.to raise_error(Isolator::BackgroundJobError, /ActiveJobWorker.+test/)
end

context "with enqueue_after_transaction_commit configured", if: ActiveRecord.version >= Gem::Version.new("7.2") do
context "configured to :always" do
before do
allow(ActiveJob::Base).to receive(:enqueue_after_transaction_commit) { :always }
end

specify do
expect { ActiveJobWorker.perform_later("test") }.to_not raise_error
end
end

context "configured to :default" do
before do
allow(ActiveJob::Base).to receive(:enqueue_after_transaction_commit) { :default }
end

context "with queue_adapter enqueue_after_transaction_commit enabled" do
let (:queue_adapter) { double("queue_adapter", enqueue_after_transaction_commit?: true) }

before do
allow(ActiveJob::Base).to receive(:queue_adapter) { queue_adapter }
end

specify do
expect { ActiveJobWorker.perform_later("test") }.to_not raise_error
end
end

context "with queue_adapter enqueue_after_transaction_commit disabled" do
let (:queue_adapter) { double("queue_adapter", enqueue_after_transaction_commit?: false) }

before do
allow(ActiveJob::Base).to receive(:queue_adapter) { queue_adapter }
end

specify do
expect { ActiveJobWorker.perform_later("test") }.to raise_error(Isolator::BackgroundJobError, /ActiveJobWorker.+test/)
end
end
end

context "configured to :never" do
before do
allow(ActiveJob::Base).to receive(:enqueue_after_transaction_commit) { :never }
end

specify do
expect { ActiveJobWorker.perform_later("test") }.to raise_error(Isolator::BackgroundJobError, /ActiveJobWorker.+test/)
end
end
end
end
end

0 comments on commit 5ddaac8

Please sign in to comment.