From f0fcc71fac3ed3adecccdd917a41f769cb6f8460 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Fri, 24 Jan 2025 16:10:27 -0500 Subject: [PATCH] Fix Rails 8.0 deprecation (#51) 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`. --- Gemfile.lock | 2 +- delayed.gemspec | 2 +- lib/delayed/active_job_adapter.rb | 7 +++++- spec/delayed/active_job_adapter_spec.rb | 32 +++++++++++++++++++++++-- spec/helper.rb | 7 ++++++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index dc9a1f1..c7e3fbe 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - delayed (0.7.0) + delayed (0.7.1) activerecord (>= 5.2) concurrent-ruby diff --git a/delayed.gemspec b/delayed.gemspec index 0a97ae6..e5b3346 100644 --- a/delayed.gemspec +++ b/delayed.gemspec @@ -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', diff --git a/lib/delayed/active_job_adapter.rb b/lib/delayed/active_job_adapter.rb index bbaf8a0..a2e5f2f 100644 --- a/lib/delayed/active_job_adapter.rb +++ b/lib/delayed/active_job_adapter.rb @@ -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 @@ -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 diff --git a/spec/delayed/active_job_adapter_spec.rb b/spec/delayed/active_job_adapter_spec.rb index 67b7a39..0e2ccf1 100644 --- a/spec/delayed/active_job_adapter_spec.rb +++ b/spec/delayed/active_job_adapter_spec.rb @@ -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 @@ -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 diff --git a/spec/helper.rb b/spec/helper.rb index baec590..87cb8d9 100644 --- a/spec/helper.rb +++ b/spec/helper.rb @@ -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