diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md index 8e18faeb02128c9b8db49fe5803f77234fdb86ec..4acbe9a4e9f145afffb6f73710cddf4f3fd51a58 100644 --- a/activejob/CHANGELOG.md +++ b/activejob/CHANGELOG.md @@ -1,3 +1,10 @@ +* Return false instead of the job instance when `enqueue` is aborted. + + This will be the behavior in Rails 6.1 but it can be controlled now with + `config.active_job.return_false_on_aborted_enqueue`. + + *Kir Shatrov* + * Keep executions for each specific declaration Each `retry_on` declaration has now its own specific executions counter. Before it was diff --git a/activejob/lib/active_job/enqueuing.rb b/activejob/lib/active_job/enqueuing.rb index 58e6d3348d0d57b6b3c64f7ff8ea20951c5a04a3..ce118c1e8a36bf71f87c4e4ab62dd2f95250e036 100644 --- a/activejob/lib/active_job/enqueuing.rb +++ b/activejob/lib/active_job/enqueuing.rb @@ -49,21 +49,29 @@ def enqueue(options = {}) 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." + ActiveSupport::Deprecation.warn( + "Rails 6.0 will return false when the enqueing is aborted. Make sure your code doesn't depend on it" \ + " returning the instance of the job and set `config.active_job.return_false_on_aborted_enqueue = true`" \ + " to remove the deprecations." + ) + self end end diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 960a43406b7c80164e9b53582e1bdf1f15ee6599..029ae1a5ff93a4a1954ae7ddf1d8558f5248817a 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -782,6 +782,8 @@ There are a few configuration options available in Active Support: * `config.active_job.custom_serializers` allows to set custom argument serializers. Defaults to `[]`. +* `config.active_job.return_false_on_aborted_enqueue` change the return value of `#enqueue` to false instead of the job instance when the enqueuing is aborted. Defaults to `false`. + ### Configuring Action Cable * `config.action_cable.url` accepts a string for the URL for where diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index d6f8c4f47cdc707e4bcdc5792612afc6dfcaf101..f30e5d2a0f9a3a70507a3fe4a90030dee032cd86 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -127,6 +127,10 @@ def load_defaults(target_version) if respond_to?(:action_dispatch) action_dispatch.use_cookies_with_metadata = true end + + if respond_to?(:active_job) + active_job.return_false_on_aborted_enqueue = true + end else raise "Unknown version #{target_version.to_s.inspect}" end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 3635de71629ca6d0d5344ef85ad2eafdab130305..9fff764eca7701c039f550db5e9767ad6540f738 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -2139,6 +2139,33 @@ class ::DummySerializer < ActiveJob::Serializers::ObjectSerializer; end assert_equal false, ActionView::Template.finalize_compiled_template_methods end + test "ActiveJob::Base.return_false_on_aborted_enqueue is true by default" do + app "development" + + assert_equal true, ActiveJob::Base.return_false_on_aborted_enqueue + end + + test "ActiveJob::Base.return_false_on_aborted_enqueue is false in the 5.x defaults" do + remove_from_config '.*config\.load_defaults.*\n' + add_to_config 'config.load_defaults "5.2"' + + app "development" + + assert_equal false, ActiveJob::Base.return_false_on_aborted_enqueue + end + + test "ActiveJob::Base.return_false_on_aborted_enqueue can be configured in the new framework defaults" do + remove_from_config '.*config\.load_defaults.*\n' + + app_file "config/initializers/new_framework_defaults_6_0.rb", <<-RUBY + Rails.application.config.active_job.return_false_on_aborted_enqueue = true + RUBY + + app "development" + + assert_equal true, ActiveJob::Base.return_false_on_aborted_enqueue + end + test "ActiveRecord::Base.filter_attributes should equal to filter_parameters" do app_file "config/initializers/filter_parameters_logging.rb", <<-RUBY Rails.application.config.filter_parameters += [ :password, :credit_card_number ]