Merge pull request #45664 from sambostock/teach-active-job-to-set-configs-on-itself

Teach `ActiveJob` to set configs on itself
This commit is contained in:
Jean Boussier 2022-08-01 10:51:19 +02:00 committed by GitHub
commit 73d2cc86d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 4 deletions

@ -46,7 +46,7 @@ module ActiveJob
# :singleton-method:
# If false, Rails will preserve the legacy serialization of BigDecimal job arguments as Strings.
# If true, Rails will use the new BigDecimalSerializer to (de)serialize BigDecimal losslessly.
# This behavior will be removed in Rails 7.2.
# Legacy serialization will be removed in Rails 7.2, along with this config.
singleton_class.attr_accessor :use_big_decimal_serializer
self.use_big_decimal_serializer = false
end

@ -25,6 +25,15 @@ class Railtie < Rails::Railtie # :nodoc:
options = app.config.active_job
options.queue_adapter ||= :async
config.after_initialize do
options.each do |k, v|
k = "#{k}="
if ActiveJob.respond_to?(k)
ActiveJob.send(k, v)
end
end
end
ActiveSupport.on_load(:active_job) do
# Configs used in other initializers
options = options.except(
@ -32,9 +41,13 @@ class Railtie < Rails::Railtie # :nodoc:
:custom_serializers
)
options.each do |k, v|
options.each do |k, v|
k = "#{k}="
send(k, v) if respond_to? k
if ActiveJob.respond_to?(k)
ActiveJob.send(k, v)
elsif respond_to? k
send(k, v)
end
end
end

@ -87,6 +87,20 @@ def initialize(*)
# {configuration guide}[https://guides.rubyonrails.org/configuring.html#versioned-default-values]
# for the default values associated with a particular version.
def load_defaults(target_version)
# To introduce a change in behavior, follow these steps:
# 1. Add an accessor on the target object (e.g. the ActiveJob class for global Active Job config).
# 2. Set a default value there preserving existing behavior for existing applications.
# 3. Implement the behavior change based on the config value.
# 4. In the section below corresponding to the next release of Rails, set the new value.
# 5. Add a commented out section in the `new_framework_defaults` template, setting the new value.
# 6. Update the guide in `configuration.md`.
# To remove configurable deprecated behavior, follow these steps:
# 1. Update or remove the entry in the guides.
# 2. Remove the references below.
# 3. Remove the legacy code paths and config check.
# 4. Remove the config accessor.
case target_version.to_s
when "5.0"
if respond_to?(:action_controller)
@ -279,7 +293,7 @@ def load_defaults(target_version)
end
if respond_to?(:active_job)
active_job.use_big_decimal_serializer = false
active_job.use_big_decimal_serializer = true
end
if respond_to?(:active_support)

@ -2656,6 +2656,43 @@ class ::DummySerializer < ActiveJob::Serializers::ObjectSerializer; end
assert_includes ActiveJob::Serializers.serializers, DummySerializer
end
test "use_big_decimal_serializer is enabled in new apps" do
app "development"
# When loaded, ActiveJob::Base triggers the :active_job load hooks, which is where config is attached.
# Referencing the constant auto-loads it.
ActiveJob::Base
assert ActiveJob.use_big_decimal_serializer, "use_big_decimal_serializer should be enabled in new apps"
end
test "use_big_decimal_serializer is disabled if using defaults prior to 7.1" do
remove_from_config '.*config\.load_defaults.*\n'
add_to_config 'config.load_defaults "7.0"'
app "development"
# When loaded, ActiveJob::Base triggers the :active_job load hooks, which is where config is attached.
# Referencing the constant auto-loads it.
ActiveJob::Base
assert_not ActiveJob.use_big_decimal_serializer, "use_big_decimal_serializer should be disabled in defaults prior to 7.1"
end
test "use_big_decimal_serializer can be enabled in config" do
remove_from_config '.*config\.load_defaults.*\n'
add_to_config 'config.load_defaults "7.0"'
app_file "config/initializers/new_framework_defaults_7_1.rb", <<-RUBY
Rails.application.config.active_job.use_big_decimal_serializer = true
RUBY
app "development"
# When loaded, ActiveJob::Base triggers the :active_job load hooks, which is where config is attached.
# Referencing the constant auto-loads it.
ActiveJob::Base
assert ActiveJob.use_big_decimal_serializer, "use_big_decimal_serializer should be enabled if set in config"
end
test "active record job queue is set" do
app "development"