Skip to content

Commit

Permalink
Guard against jobs where enqueue_after_transaction_commit is true
Browse files Browse the repository at this point in the history
  • Loading branch information
smudge committed Apr 5, 2024
1 parent 06711d8 commit 2c01329
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/delayed/active_job_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module Delayed
class ActiveJobAdapter
class UnsafeEnqueueError < RuntimeError; end

def enqueue_after_transaction_commit?
false
end

def enqueue(job)
_enqueue(job)
end
Expand All @@ -11,6 +17,10 @@ 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
raise UnsafeEnqueueError, "The ':delayed' ActiveJob adapter is not compatible with enqueue_after_transaction_commit"
end

opts.merge!({ queue: job.queue_name, priority: job.priority }.compact)
.merge!(job.provider_attributes || {})

Expand Down
13 changes: 13 additions & 0 deletions spec/delayed/active_job_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@ def perform(arg, kwarg:)
end
end

if ActiveJob.gem_version > Gem::Version.new('7.1')
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
end

context 'when using the ActiveJob test adapter' do
let(:queue_adapter) { :test }

Expand Down

0 comments on commit 2c01329

Please sign in to comment.