Skip to content

Commit

Permalink
Handle proper values for ActiveJob.enqueue_after_transaction_commit
Browse files Browse the repository at this point in the history
  • Loading branch information
warmerzega committed Aug 13, 2024
1 parent fd97a38 commit 0c0c847
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ appraise 'rails-7-1' do
gem 'activerecord', '~> 7.1.0'
end

appraise 'rails-7-2' do
gem 'actionmailer', '~> 7.2.0'
gem 'activejob', '~> 7.2.0'
gem 'activerecord', '~> 7.2.0'
end

appraise 'rails-main' do
gem 'actionmailer', github: 'rails/rails', glob: 'actionmailer/*.gemspec'
gem 'activejob', github: 'rails/rails', glob: 'activejob/*.gemspec'
Expand Down
2 changes: 1 addition & 1 deletion lib/delayed/active_job_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def enqueue_at(job, timestamp)
private

def _enqueue(job, opts = {})
if job.class.respond_to?(:enqueue_after_transaction_commit) && job.class.enqueue_after_transaction_commit
if job.class.respond_to?(:enqueue_after_transaction_commit) && job.class.enqueue_after_transaction_commit == :always
raise UnsafeEnqueueError, "The ':delayed' ActiveJob adapter is not compatible with enqueue_after_transaction_commit"
end

Expand Down
15 changes: 13 additions & 2 deletions spec/delayed/active_job_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,27 @@ def perform(arg, kwarg:)
end

if ActiveJob.gem_version.release >= Gem::Version.new('7.2')
context 'when the given job sets enqueue_after_transaction_commit to true' do
context 'when the given job sets enqueue_after_transaction_commit to :always' do
before do
JobClass.include ActiveJob::EnqueueAfterTransactionCommit # normally run in an ActiveJob railtie
JobClass.enqueue_after_transaction_commit = true
JobClass.enqueue_after_transaction_commit = :always
end

it 'raises an exception on enqueue' do
expect { JobClass.perform_later }.to raise_error(Delayed::ActiveJobAdapter::UnsafeEnqueueError)
end
end

context 'when the given job sets enqueue_after_transaction_commit to :never' do
before do
JobClass.include ActiveJob::EnqueueAfterTransactionCommit # normally run in an ActiveJob railtie
JobClass.enqueue_after_transaction_commit = :never
end

it 'does not raises an exception on enqueue' do
expect { JobClass.perform_later }.not_to raise_error(Delayed::ActiveJobAdapter::UnsafeEnqueueError)
end
end
end

context 'when using the ActiveJob test adapter' do
Expand Down

0 comments on commit 0c0c847

Please sign in to comment.