rails/activejob/test/cases/delayed_job_adapter_test.rb
Jean Boussier e922c59207 Implement Active Job enqueue_after_transaction_commit
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>
2024-04-03 16:32:16 +02:00

50 lines
1.6 KiB
Ruby

# frozen_string_literal: true
require "helper"
require "active_job/queue_adapters/delayed_job_adapter"
require "jobs/disable_log_job"
require "jobs/hello_job"
class DelayedJobAdapterTest < ActiveSupport::TestCase
test "does not log arguments when log_arguments is set to false on a job" do
job_id = SecureRandom.uuid
job_wrapper = ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper.new(
"job_class" => DisableLogJob.to_s,
"queue_name" => "default",
"job_id" => job_id,
"arguments" => { "some" => { "job" => "arguments" } }
)
assert_equal "DisableLogJob [#{job_id}] from DelayedJob(default)", job_wrapper.display_name
end
test "logs arguments when log_arguments is set to true on a job" do
job_id = SecureRandom.uuid
arguments = { "some" => { "job" => "arguments" } }
job_wrapper = ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper.new(
"job_class" => HelloJob.to_s,
"queue_name" => "default",
"job_id" => job_id,
"arguments" => arguments
)
assert_equal "HelloJob [#{job_id}] from DelayedJob(default) with arguments: #{arguments}",
job_wrapper.display_name
end
test "shows name for invalid job class" do
job_id = SecureRandom.uuid
job_wrapper = ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper.new(
"job_class" => "NotExistingJob",
"queue_name" => "default",
"job_id" => job_id,
"arguments" => { "some" => { "job" => "arguments" } }
)
assert_equal "NotExistingJob [#{job_id}] from DelayedJob(default)", job_wrapper.display_name
end
end