diff options
5 files changed, 56 insertions, 1 deletions
diff --git a/activejob/lib/active_job/callbacks.rb b/activejob/lib/active_job/callbacks.rb index 334b24fb3b..e0e48d3ef5 100644 --- a/activejob/lib/active_job/callbacks.rb +++ b/activejob/lib/active_job/callbacks.rb @@ -29,6 +29,9 @@ module ActiveJob included do define_callbacks :perform define_callbacks :enqueue + + class_attribute :return_false_on_aborted_enqueue, instance_accessor: false, instance_predicate: false + self.return_false_on_aborted_enqueue = false end # These methods will be included into any Active Job object, adding diff --git a/activejob/lib/active_job/enqueuing.rb b/activejob/lib/active_job/enqueuing.rb index 53cb98fc71..5be5618390 100644 --- a/activejob/lib/active_job/enqueuing.rb +++ b/activejob/lib/active_job/enqueuing.rb @@ -46,14 +46,25 @@ module ActiveJob self.scheduled_at = options[:wait_until].to_f if options[:wait_until] self.queue_name = self.class.queue_name_from_part(options[:queue]) if options[:queue] self.priority = options[:priority].to_i if options[:priority] + successfully_enqueued = false run_callbacks :enqueue do if scheduled_at self.class.queue_adapter.enqueue_at self, scheduled_at else self.class.queue_adapter.enqueue self end + successfully_enqueued = true + end + if successfully_enqueued + self + else + if self.class.return_false_on_aborted_enqueue + false + else + ActiveSupport::Deprecation.warn "this will return false, set config.active_job.return_false_on_aborted_enqueue = true to remove deprecation." + self + end end - self end end end diff --git a/activejob/test/cases/callbacks_test.rb b/activejob/test/cases/callbacks_test.rb index df6ce16858..267d58a7c8 100644 --- a/activejob/test/cases/callbacks_test.rb +++ b/activejob/test/cases/callbacks_test.rb @@ -2,6 +2,7 @@ require "helper" require "jobs/callback_job" +require "jobs/abort_before_enqueue_job" require "active_support/core_ext/object/inclusion" @@ -22,4 +23,32 @@ class CallbacksTest < ActiveSupport::TestCase assert "CallbackJob ran around_enqueue_start".in? enqueued_callback_job.history assert "CallbackJob ran around_enqueue_stop".in? enqueued_callback_job.history end + + test "#enqueue returns false when before_enqueue aborts callback chain and return_false_on_aborted_enqueue = true" do + begin + prev = ActiveJob::Base.return_false_on_aborted_enqueue + ActiveJob::Base.return_false_on_aborted_enqueue = true + assert_equal false, AbortBeforeEnqueueJob.new.enqueue + ensure + ActiveJob::Base.return_false_on_aborted_enqueue = prev + end + end + + test "#enqueue returns self when before_enqueue aborts callback chain and return_false_on_aborted_enqueue = false" do + begin + prev = ActiveJob::Base.return_false_on_aborted_enqueue + ActiveJob::Base.return_false_on_aborted_enqueue = false + job = AbortBeforeEnqueueJob.new + assert_deprecated do + assert_equal job, job.enqueue + end + ensure + ActiveJob::Base.return_false_on_aborted_enqueue = prev + end + end + + test "#enqueue returns self when the job was enqueued" do + job = CallbackJob.new + assert_equal job, job.enqueue + end end diff --git a/activejob/test/jobs/abort_before_enqueue_job.rb b/activejob/test/jobs/abort_before_enqueue_job.rb new file mode 100644 index 0000000000..fd278eccf4 --- /dev/null +++ b/activejob/test/jobs/abort_before_enqueue_job.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AbortBeforeEnqueueJob < ActiveJob::Base + before_enqueue { throw(:abort) } + + def perform + raise "This should never be called" + end +end diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_0.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_0.rb.tt index 54eb0cb1d2..5cca8ae570 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_0.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_0.rb.tt @@ -15,3 +15,6 @@ # This option is not backwards compatible with earlier Rails versions. # It's best enabled when your entire app is migrated and stable on 6.0. # Rails.application.config.action_dispatch.use_cookies_with_metadata = true + +# Return false instead of self when #enqueue method was aborted from the callback +Rails.application.config.active_job.return_false_on_aborted_enqueue = true |