mirror of https://github.com/rails/rails
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:
commit
3bfe4191a3
|
@ -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(...)
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require_relative "../support/job_buffer"
|
||||
|
||||
class ConfigurationJob < ActiveJob::Base
|
||||
def perform
|
||||
JobBuffer.add(self)
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue