A fairly common mistake with Rails is to enqueue a job from inside a transaction, with a record as argumemnt, which then lead to a RecordNotFound error when picked up by the queue. This is even one of the arguments advanced for job runners backed by the database such as `solid_queue`, `delayed_job` or `good_job`. But relying on this is undesirable in my opinion as it makes the Active Job abstraction leaky, and if in the future you need to migrate to another backend or even just move the queue to a separate database, you may experience a lot of race conditions of the sort. To resolve this problem globally, we can make Active Job optionally transaction aware, and automatically defer job queueing to `after_commit`. Co-Authored-By: Cristian Bica <cristian.bica@gmail.com>
1.7 KiB
-
Make Active Job transaction aware when used conjointly with Active Record.
A common mistake with Active Job is to enqueue jobs from inside a transaction, causing them to potentially be picked and ran by another process, before the transaction is committed, which result in various errors.
Topic.transaction do topic = Topic.create(...) NewTopicNotificationJob.perform_later(topic) end
Now Active Job will automatically defer the enqueuing to after the transaction is committed, and drop the job if the transaction is rolled back.
Various queue implementations can chose to disable this behavior, and users can disable it, or force it on a per job basis:
class NewTopicNotificationJob < ApplicationJob self.enqueue_after_transaction_commit = false # or `true` end
Jean Boussier, Cristian Bica
-
Do not trigger immediate loading of
ActiveJob::Base
when loadingActiveJob::TestHelper
.Maxime Réty
-
Preserve the serialized timezone when deserializing
ActiveSupport::TimeWithZone
arguments.Joshua Young
-
Remove deprecated
:exponentially_longer
value for the:wait
inretry_on
.Rafael Mendonça França
-
Remove deprecated support to set numeric values to
scheduled_at
attribute.Rafael Mendonça França
-
Deprecate
Rails.application.config.active_job.use_big_decimal_serialize
.Rafael Mendonça França
-
Remove deprecated primitive serializer for
BigDecimal
arguments.Rafael Mendonça França
Please check 7-1-stable for previous changes.