Merge pull request #43434 from bensheldon/set_perform_now

Allow `ActiveJob::Base.set` to configure jobs when using `.perform_now`
This commit is contained in:
Rafael Mendonça França 2021-10-12 17:16:10 -04:00 committed by GitHub
commit 3bfe4191a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 9 deletions

View File

@ -8,7 +8,7 @@ module ActiveJob
end
def perform_now(...)
@job_class.new(...).perform_now
@job_class.new(...).set(@options).perform_now
end
def perform_later(...)

View File

@ -156,6 +156,16 @@ module ActiveJob
self.enqueued_at = job_data["enqueued_at"]
end
# Configures the job with the given options.
def set(options = {}) # :nodoc:
self.scheduled_at = options[:wait].seconds.from_now.to_f if options[:wait]
self.scheduled_at = options[:wait_until].to_f if options[:wait_until]
self.queue_name = self.class.queue_name_from_part(options[:queue]) if options[:queue]
self.priority = options[:priority].to_i if options[:priority]
self
end
private
def serialize_arguments_if_needed(arguments)
if arguments_serialized?

View File

@ -57,10 +57,7 @@ module ActiveJob
# my_job_instance.enqueue wait_until: Date.tomorrow.midnight
# my_job_instance.enqueue priority: 10
def enqueue(options = {})
self.scheduled_at = options[:wait].seconds.from_now.to_f if options[:wait]
self.scheduled_at = options[:wait_until].to_f if options[:wait_until]
self.queue_name = self.class.queue_name_from_part(options[:queue]) if options[:queue]
self.priority = options[:priority].to_i if options[:priority]
set(options)
self.successfully_enqueued = false
run_callbacks :enqueue do

View File

@ -1,12 +1,17 @@
# frozen_string_literal: true
require "helper"
require "jobs/configuration_job"
require "jobs/hello_job"
require "jobs/prefixed_job"
require "jobs/logging_job"
require "jobs/nested_job"
class QueueNamingTest < ActiveSupport::TestCase
setup do
JobBuffer.clear
end
test "name derived from base" do
assert_equal "default", HelloJob.new.queue_name
end
@ -143,8 +148,15 @@ class QueueNamingTest < ActiveSupport::TestCase
end
end
test "uses queue passed to #set" do
job = HelloJob.set(queue: :some_queue).perform_later
test "is assigned when perform_now" do
ConfigurationJob.set(queue: :some_queue).perform_now
job = JobBuffer.last_value
assert_equal "some_queue", job.queue_name
end
test "is assigned when perform_later" do
ConfigurationJob.set(queue: :some_queue).perform_later
job = JobBuffer.last_value
assert_equal "some_queue", job.queue_name
end
end

View File

@ -1,9 +1,14 @@
# frozen_string_literal: true
require "helper"
require "jobs/configuration_job"
require "jobs/hello_job"
class QueuePriorityTest < ActiveSupport::TestCase
setup do
JobBuffer.clear
end
test "priority unset by default" do
assert_nil HelloJob.priority
end
@ -42,8 +47,15 @@ class QueuePriorityTest < ActiveSupport::TestCase
end
end
test "uses priority passed to #set" do
job = HelloJob.set(priority: 123).perform_later
test "is assigned when perform_now" do
ConfigurationJob.set(priority: 123).perform_now
job = JobBuffer.last_value
assert_equal 123, job.priority
end
test "is assigned when perform_later" do
ConfigurationJob.set(priority: 123).perform_later
job = JobBuffer.last_value
assert_equal 123, job.priority
end
end

View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
require_relative "../support/job_buffer"
class ConfigurationJob < ActiveJob::Base
def perform
JobBuffer.add(self)
end
end