Skip to content

Commit 6daba18

Browse files
committed
Allow queue adapters to provide a custom name
This gives queue adapters more freedom to name and organize their code. For example, if `FancyQueue` wants to have their adapter at `FancyQueue::ActiveJobAdapter`, the name would be `active_job` before this change. After this change, they can implement `queue_adapter_name` to return `fancy_queue`.
1 parent 138a9d2 commit 6daba18

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

activejob/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Allow queue adapters to provide a custom name by implementing `queue_adapter_name`
2+
3+
*Sander Verdonschot*
4+
15
* Log background job enqueue callers
26

37
Add `verbose_enqueue_logs` configuration option to display the caller

activejob/lib/active_job/queue_adapter.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ def queue_adapter=(name_or_adapter)
4343
assign_adapter(name_or_adapter.to_s, queue_adapter)
4444
else
4545
if queue_adapter?(name_or_adapter)
46-
adapter_class = name_or_adapter.is_a?(Module) ? name_or_adapter : name_or_adapter.class
47-
adapter_name = "#{adapter_class.name.demodulize.remove('Adapter').underscore}"
46+
adapter_name = extract_adapter_name(name_or_adapter)
4847
assign_adapter(adapter_name, name_or_adapter)
4948
else
5049
raise ArgumentError
@@ -63,6 +62,13 @@ def assign_adapter(adapter_name, queue_adapter)
6362
def queue_adapter?(object)
6463
QUEUE_ADAPTER_METHODS.all? { |meth| object.respond_to?(meth) }
6564
end
65+
66+
def extract_adapter_name(adapter)
67+
return adapter.queue_adapter_name if adapter.respond_to?(:queue_adapter_name)
68+
69+
adapter_class = adapter.is_a?(Module) ? adapter : adapter.class
70+
"#{adapter_class.name.demodulize.remove('Adapter').underscore}"
71+
end
6672
end
6773
end
6874
end

activejob/test/cases/queue_adapter_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,18 @@ def enqueue_at(*); end
6969
child_job.queue_adapter = StubThreeAdapter
7070
assert_equal "stub_three", child_job.queue_adapter_name
7171
end
72+
73+
class StubFourAdapter
74+
def enqueue(*); end
75+
def enqueue_at(*); end
76+
def queue_adapter_name
77+
"fancy_name"
78+
end
79+
end
80+
81+
test "should use the name provided by the adapter" do
82+
child_job = Class.new(ActiveJob::Base)
83+
child_job.queue_adapter = StubFourAdapter.new
84+
assert_equal "fancy_name", child_job.queue_adapter_name
85+
end
7286
end

0 commit comments

Comments
 (0)