aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.travis.yml2
-rw-r--r--activejob/lib/active_job/callbacks.rb3
-rw-r--r--activejob/lib/active_job/enqueuing.rb13
-rw-r--r--activejob/test/cases/callbacks_test.rb29
-rw-r--r--activejob/test/jobs/abort_before_enqueue_job.rb9
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_0.rb.tt3
6 files changed, 57 insertions, 2 deletions
diff --git a/.travis.yml b/.travis.yml
index 9097edbfdd..2d0ec1efcf 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -35,7 +35,7 @@ bundler_args: --jobs 3 --retry 3
before_install:
- "rm ${BUNDLE_GEMFILE}.lock"
- "travis_retry gem update --system"
- - "travis_retry gem install bundler"
+ - "travis_retry gem install bundler -v '2.0.0.pre.2'"
- "[[ -z $encrypted_0fb9444d0374_key && -z $encrypted_0fb9444d0374_iv ]] || openssl aes-256-cbc -K $encrypted_0fb9444d0374_key -iv $encrypted_0fb9444d0374_iv -in activestorage/test/service/configurations.yml.enc -out activestorage/test/service/configurations.yml -d"
- "[[ $GEM != 'ac:integration' ]] || yarn install"
- "[[ $GEM != 'av:ujs' ]] || nvm install node"
diff --git a/activejob/lib/active_job/callbacks.rb b/activejob/lib/active_job/callbacks.rb
index 61317c7cfc..6b82ea6cab 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 b5b9f23c00..58e6d3348d 100644
--- a/activejob/lib/active_job/enqueuing.rb
+++ b/activejob/lib/active_job/enqueuing.rb
@@ -48,14 +48,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