Skip to content

Commit

Permalink
Handle enqueue_after_transaction_commit = true
Browse files Browse the repository at this point in the history
  • Loading branch information
rzane committed Jan 24, 2025
1 parent 7be8d9b commit 81df09e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
7 changes: 6 additions & 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 == :always
if enqueue_after_transaction_commit_enabled?(job)
raise UnsafeEnqueueError, "The ':delayed' ActiveJob adapter is not compatible with enqueue_after_transaction_commit"
end

Expand All @@ -29,6 +29,11 @@ def _enqueue(job, opts = {})
end
end

def enqueue_after_transaction_commit_enabled?(job)
job.class.respond_to?(:enqueue_after_transaction_commit) &&
[true, :always].include?(job.class.enqueue_after_transaction_commit)
end

module EnqueuingPatch
def self.included(klass)
klass.prepend PrependedMethods
Expand Down
28 changes: 27 additions & 1 deletion spec/delayed/active_job_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,33 @@ def perform(arg, kwarg:)
end

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

if ActiveJob.gem_version.release >= Gem::Version.new('8.0')
context 'when the given job sets enqueue_after_transaction_commit to true' do
before do
JobClass.include ActiveJob::EnqueueAfterTransactionCommit # normally run in an ActiveJob railtie
JobClass.enqueue_after_transaction_commit = true
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 false' do
before do
JobClass.include ActiveJob::EnqueueAfterTransactionCommit # normally run in an ActiveJob railtie
JobClass.enqueue_after_transaction_commit = false
end

it 'does not raises an exception on enqueue' do
expect { JobClass.perform_later }.not_to raise_error
end
end
end
Expand Down

0 comments on commit 81df09e

Please sign in to comment.