Skip to content

Commit 3bfe419

Browse files
authored
Merge pull request rails#43434 from bensheldon/set_perform_now
Allow `ActiveJob::Base.set` to configure jobs when using `.perform_now`
2 parents 1714716 + c4fcb4e commit 3bfe419

File tree

6 files changed

+49
-9
lines changed

6 files changed

+49
-9
lines changed

activejob/lib/active_job/configured_job.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def initialize(job_class, options = {})
88
end
99

1010
def perform_now(...)
11-
@job_class.new(...).perform_now
11+
@job_class.new(...).set(@options).perform_now
1212
end
1313

1414
def perform_later(...)

activejob/lib/active_job/core.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ def deserialize(job_data)
156156
self.enqueued_at = job_data["enqueued_at"]
157157
end
158158

159+
# Configures the job with the given options.
160+
def set(options = {}) # :nodoc:
161+
self.scheduled_at = options[:wait].seconds.from_now.to_f if options[:wait]
162+
self.scheduled_at = options[:wait_until].to_f if options[:wait_until]
163+
self.queue_name = self.class.queue_name_from_part(options[:queue]) if options[:queue]
164+
self.priority = options[:priority].to_i if options[:priority]
165+
166+
self
167+
end
168+
159169
private
160170
def serialize_arguments_if_needed(arguments)
161171
if arguments_serialized?

activejob/lib/active_job/enqueuing.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ def job_or_instantiate(*args) # :doc:
5757
# my_job_instance.enqueue wait_until: Date.tomorrow.midnight
5858
# my_job_instance.enqueue priority: 10
5959
def enqueue(options = {})
60-
self.scheduled_at = options[:wait].seconds.from_now.to_f if options[:wait]
61-
self.scheduled_at = options[:wait_until].to_f if options[:wait_until]
62-
self.queue_name = self.class.queue_name_from_part(options[:queue]) if options[:queue]
63-
self.priority = options[:priority].to_i if options[:priority]
60+
set(options)
6461
self.successfully_enqueued = false
6562

6663
run_callbacks :enqueue do

activejob/test/cases/queue_naming_test.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# frozen_string_literal: true
22

33
require "helper"
4+
require "jobs/configuration_job"
45
require "jobs/hello_job"
56
require "jobs/prefixed_job"
67
require "jobs/logging_job"
78
require "jobs/nested_job"
89

910
class QueueNamingTest < ActiveSupport::TestCase
11+
setup do
12+
JobBuffer.clear
13+
end
14+
1015
test "name derived from base" do
1116
assert_equal "default", HelloJob.new.queue_name
1217
end
@@ -143,8 +148,15 @@ class QueueNamingTest < ActiveSupport::TestCase
143148
end
144149
end
145150

146-
test "uses queue passed to #set" do
147-
job = HelloJob.set(queue: :some_queue).perform_later
151+
test "is assigned when perform_now" do
152+
ConfigurationJob.set(queue: :some_queue).perform_now
153+
job = JobBuffer.last_value
154+
assert_equal "some_queue", job.queue_name
155+
end
156+
157+
test "is assigned when perform_later" do
158+
ConfigurationJob.set(queue: :some_queue).perform_later
159+
job = JobBuffer.last_value
148160
assert_equal "some_queue", job.queue_name
149161
end
150162
end

activejob/test/cases/queue_priority_test.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# frozen_string_literal: true
22

33
require "helper"
4+
require "jobs/configuration_job"
45
require "jobs/hello_job"
56

67
class QueuePriorityTest < ActiveSupport::TestCase
8+
setup do
9+
JobBuffer.clear
10+
end
11+
712
test "priority unset by default" do
813
assert_nil HelloJob.priority
914
end
@@ -42,8 +47,15 @@ class QueuePriorityTest < ActiveSupport::TestCase
4247
end
4348
end
4449

45-
test "uses priority passed to #set" do
46-
job = HelloJob.set(priority: 123).perform_later
50+
test "is assigned when perform_now" do
51+
ConfigurationJob.set(priority: 123).perform_now
52+
job = JobBuffer.last_value
53+
assert_equal 123, job.priority
54+
end
55+
56+
test "is assigned when perform_later" do
57+
ConfigurationJob.set(priority: 123).perform_later
58+
job = JobBuffer.last_value
4759
assert_equal 123, job.priority
4860
end
4961
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../support/job_buffer"
4+
5+
class ConfigurationJob < ActiveJob::Base
6+
def perform
7+
JobBuffer.add(self)
8+
end
9+
end

0 commit comments

Comments
 (0)