Skip to content

Commit

Permalink
Fix Rails 8.0 deprecation (#51)
Browse files Browse the repository at this point in the history
I configured deprecations to raise exceptions.

Once I did that, I had to suppress deprecations from `enqueue_after_transaction_commit = :always` and `enqueue_after_transaction_commit = :never`.

Those values are still supported, but deprecated. Rails 8.0 now expects `true` or `false`. I've updated delayed to ensure that `enqueue_after_transaction_commit` is not set to `true`.
  • Loading branch information
rzane authored Jan 24, 2025
1 parent 7e184ba commit f0fcc71
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
delayed (0.7.0)
delayed (0.7.1)
activerecord (>= 5.2)
concurrent-ruby

Expand Down
2 changes: 1 addition & 1 deletion delayed.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']
spec.summary = 'a multi-threaded, SQL-driven ActiveJob backend used at Betterment to process millions of background jobs per day'

spec.version = '0.7.0'
spec.version = '0.7.1'
spec.metadata = {
'changelog_uri' => 'https://github.com/betterment/delayed/blob/main/CHANGELOG.md',
'bug_tracker_uri' => 'https://github.com/betterment/delayed/issues',
Expand Down
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
32 changes: 30 additions & 2 deletions spec/delayed/active_job_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ def perform(arg, kwarg:)
end

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

Expand All @@ -306,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
7 changes: 7 additions & 0 deletions spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

require 'rake'

if ActiveSupport.gem_version >= Gem::Version.new('7.1')
frameworks = [ActiveModel, ActiveRecord, ActionMailer, ActiveJob, ActiveSupport]
frameworks.each { |framework| framework.deprecator.behavior = :raise }
else
ActiveSupport::Deprecation.behavior = :raise
end

if ENV['DEBUG_LOGS']
Delayed.logger = Logger.new($stdout)
else
Expand Down

0 comments on commit f0fcc71

Please sign in to comment.