Skip to content

Commit 582f91e

Browse files
committed
Deprecate setting config.active_job.enqueue_after_transaction_commit
Also change the configuration to be a boolean instead of a symbol. The value of this setting isn't delegated to the queue adapter anymore, since the behavior that this setting controls is too dangerous to leave this change of behavior as something that users don't control. Fixes rails#52675.
1 parent c60be92 commit 582f91e

File tree

14 files changed

+56
-109
lines changed

14 files changed

+56
-109
lines changed

activejob/lib/active_job/enqueue_after_transaction_commit.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,29 @@ module ActiveJob
44
module EnqueueAfterTransactionCommit # :nodoc:
55
private
66
def raw_enqueue
7+
enqueue_after_transaction_commit = self.class.enqueue_after_transaction_commit
8+
79
after_transaction = case self.class.enqueue_after_transaction_commit
810
when :always
11+
ActiveJob.deprecator.warn(<<~MSG.squish)
12+
Setting `#{self.class.name}.enqueue_after_transaction_commit = :always` is deprecated and will be removed in Rails 8.1.
13+
Set to `true` to always enqueue the job after the transaction is committed.
14+
MSG
915
true
1016
when :never
17+
ActiveJob.deprecator.warn(<<~MSG.squish)
18+
Setting #{self.class.name}.enqueue_after_transaction_commit = :never` is deprecated and will be removed in Rails 8.1.
19+
Set to `false` to never enqueue the job after the transaction is committed.
20+
MSG
21+
false
22+
when :default
23+
ActiveJob.deprecator.warn(<<~MSG.squish)
24+
Setting `#{self.class.name}.enqueue_after_transaction_commit = :default` is deprecated and will be removed in Rails 8.1.
25+
Set to `false` to never enqueue the job after the transaction is committed.
26+
MSG
1127
false
12-
else # :default
13-
queue_adapter.enqueue_after_transaction_commit?
28+
else
29+
enqueue_after_transaction_commit
1430
end
1531

1632
if after_transaction

activejob/lib/active_job/enqueuing.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ module Enqueuing
4848
# automatically defers the enqueue to after the transaction commits.
4949
#
5050
# It can be set on a per job basis:
51-
# - `:always` forces the job to be deferred.
52-
# - `:never` forces the job to be queued immediately.
53-
# - `:default` lets the queue adapter define the behavior (recommended).
54-
class_attribute :enqueue_after_transaction_commit, instance_accessor: false, instance_predicate: false, default: :never
51+
# - true forces the job to be deferred.
52+
# - false forces the job to be queued immediately.
53+
class_attribute :enqueue_after_transaction_commit, instance_accessor: false, instance_predicate: false, default: false
5554
end
5655

5756
# Includes the +perform_later+ method for job initialization.

activejob/lib/active_job/queue_adapters/abstract_adapter.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ module QueueAdapters
77
# Active Job supports multiple job queue systems. ActiveJob::QueueAdapters::AbstractAdapter
88
# forms the abstraction layer which makes this possible.
99
class AbstractAdapter
10-
# Defines whether enqueuing should happen implicitly to after commit when called
11-
# from inside a transaction. Most adapters should return true, but some adapters
12-
# that use the same database as Active Record and are transaction aware can return
13-
# false to continue enqueuing jobs as part of the transaction.
14-
def enqueue_after_transaction_commit?
15-
true
16-
end
17-
1810
def enqueue(job)
1911
raise NotImplementedError
2012
end

activejob/lib/active_job/queue_adapters/delayed_job_adapter.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ module QueueAdapters
1616
#
1717
# Rails.application.config.active_job.queue_adapter = :delayed_job
1818
class DelayedJobAdapter < AbstractAdapter
19-
def initialize(enqueue_after_transaction_commit: false)
20-
@enqueue_after_transaction_commit = enqueue_after_transaction_commit
21-
end
22-
23-
def enqueue_after_transaction_commit? # :nodoc:
24-
@enqueue_after_transaction_commit
25-
end
26-
2719
def enqueue(job) # :nodoc:
2820
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, priority: job.priority)
2921
job.provider_job_id = delayed_job.id

activejob/lib/active_job/queue_adapters/inline_adapter.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ module QueueAdapters
1111
#
1212
# Rails.application.config.active_job.queue_adapter = :inline
1313
class InlineAdapter < AbstractAdapter
14-
def enqueue_after_transaction_commit? # :nodoc:
15-
false
16-
end
17-
1814
def enqueue(job) # :nodoc:
1915
Base.execute(job.serialize)
2016
end

activejob/lib/active_job/queue_adapters/queue_classic_adapter.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ module QueueAdapters
1919
#
2020
# Rails.application.config.active_job.queue_adapter = :queue_classic
2121
class QueueClassicAdapter < AbstractAdapter
22-
def initialize(enqueue_after_transaction_commit: false)
23-
@enqueue_after_transaction_commit = enqueue_after_transaction_commit
24-
end
25-
26-
def enqueue_after_transaction_commit? # :nodoc:
27-
@enqueue_after_transaction_commit
28-
end
29-
3022
def enqueue(job) # :nodoc:
3123
qc_job = build_queue(job.queue_name).enqueue("#{JobWrapper.name}.perform", job.serialize)
3224
job.provider_job_id = qc_job["id"] if qc_job.is_a?(Hash)

activejob/lib/active_job/queue_adapters/test_adapter.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,9 @@ module QueueAdapters
1212
#
1313
# Rails.application.config.active_job.queue_adapter = :test
1414
class TestAdapter < AbstractAdapter
15-
attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter, :reject, :queue, :at, :enqueue_after_transaction_commit)
15+
attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter, :reject, :queue, :at)
1616
attr_writer(:enqueued_jobs, :performed_jobs)
1717

18-
def initialize(enqueue_after_transaction_commit: true)
19-
@enqueue_after_transaction_commit = enqueue_after_transaction_commit
20-
end
21-
22-
def enqueue_after_transaction_commit? # :nodoc:
23-
@enqueue_after_transaction_commit
24-
end
25-
2618
# Provides a store of all the enqueued jobs with the TestAdapter so you can check them.
2719
def enqueued_jobs
2820
@enqueued_jobs ||= []

activejob/lib/active_job/railtie.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,22 @@ class Railtie < Rails::Railtie # :nodoc:
3131
ActiveJob::Base.include EnqueueAfterTransactionCommit
3232

3333
if app.config.active_job.key?(:enqueue_after_transaction_commit)
34-
ActiveJob::Base.enqueue_after_transaction_commit = app.config.active_job.delete(:enqueue_after_transaction_commit)
34+
ActiveJob.deprecator.warn(<<~MSG.squish)
35+
`config.active_job.enqueue_after_transaction_commit` is deprecated and will be removed in Rails 8.1.
36+
This configuration can still be set on individual jobs using `self.enqueue_after_transaction_commit=`,
37+
but due the nature of this behavior, it is not recommended to be set globally.
38+
MSG
39+
40+
value = case app.config.active_job.enqueue_after_transaction_commit
41+
when :always
42+
true
43+
when :never
44+
false
45+
else
46+
false
47+
end
48+
49+
ActiveJob::Base.enqueue_after_transaction_commit = value
3550
end
3651
end
3752
end
@@ -54,7 +69,8 @@ class Railtie < Rails::Railtie # :nodoc:
5469
# Configs used in other initializers
5570
options = options.except(
5671
:log_query_tags_around_perform,
57-
:custom_serializers
72+
:custom_serializers,
73+
:enqueue_after_transaction_commit
5874
)
5975

6076
options.each do |k, v|

activejob/test/cases/enqueue_after_transaction_commit_test.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def run_after_commit_callbacks
2929
end
3030

3131
class EnqueueAfterCommitJob < ActiveJob::Base
32-
self.enqueue_after_transaction_commit = :always
32+
self.enqueue_after_transaction_commit = true
3333

3434
def perform
3535
# noop
@@ -38,10 +38,6 @@ def perform
3838

3939
class ErrorEnqueueAfterCommitJob < EnqueueErrorJob
4040
class EnqueueErrorAdapter
41-
def enqueue_after_transaction_commit?
42-
true
43-
end
44-
4541
def enqueue(...)
4642
raise ActiveJob::EnqueueError, "There was an error enqueuing the job"
4743
end
@@ -52,7 +48,7 @@ def enqueue_at(...)
5248
end
5349

5450
self.queue_adapter = EnqueueErrorAdapter.new
55-
self.enqueue_after_transaction_commit = :always
51+
self.enqueue_after_transaction_commit = true
5652

5753
def perform
5854
# noop

activejob/test/cases/queue_adapter_test.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
module ActiveJob
66
module QueueAdapters
77
class StubOneAdapter
8-
def enqueue_after_transaction_commit?; false; end
98
def enqueue(*); end
109
def enqueue_at(*); end
1110
end
1211

1312
class StubTwoAdapter
14-
def enqueue_after_transaction_commit?; false; end
1513
def enqueue(*); end
1614
def enqueue_at(*); end
1715
end
@@ -74,7 +72,6 @@ class QueueAdapterTest < ActiveJob::TestCase
7472

7573
module StubThreeAdapter
7674
class << self
77-
def enqueue_after_transaction_commit?; false; end
7875
def enqueue(*); end
7976
def enqueue_at(*); end
8077
end
@@ -87,7 +84,6 @@ def enqueue_at(*); end
8784
end
8885

8986
class StubFourAdapter
90-
def enqueue_after_transaction_commit?; false; end
9187
def enqueue(*); end
9288
def enqueue_at(*); end
9389
def queue_adapter_name

0 commit comments

Comments
 (0)