覚書: Rails: GoodJob アダプタの割り当てと enqueue と discrete
GoodJob のコードの一部を読んだ際の覚書です。
Rails 7.0.5
GoodJob 3.15.10
アダプタの割り当て
Active Job の QueueAdapter でアダプタを割り当てる。
(後日、実際に呼び出している箇所についての補足を書きました)
https://github.com/rails/rails/blob/v7.0.5/activejob/lib/active_job/queue_adapter.rb#LL40
module ActiveJob
module QueueAdapter # :nodoc:
module ClassMethods
def queue_adapter=(name_or_adapter)
case name_or_adapter
when Symbol, String
queue_adapter = ActiveJob::QueueAdapters.lookup(name_or_adapter).new
assign_adapter(name_or_adapter.to_s, queue_adapter)
ActiveJob::QueueAdapters
https://github.com/rails/rails/blob/v7.0.5/activejob/lib/active_job/queue_adapters.rb#L136-L138
module ActiveJob
module QueueAdapters
class << self
# Returns adapter for specified name.
#
# ActiveJob::QueueAdapters.lookup(:sidekiq)
# # => ActiveJob::QueueAdapters::SidekiqAdapter
def lookup(name)
const_get(name.to_s.camelize << ADAPTER)
end
Sidekiq などのアダプタは ActiveJob 側にあるが、GoodJob は Gem 側にある。
なぜこうなっているのかは分かりません。
https://github.com/rails/rails/tree/v7.0.5/activejob/lib/active_job/queue_adapters
https://github.com/bensheldon/good_job/blob/v3.15.10/lib/active_job/queue_adapters/good_job_adapter.rb
https://github.com/bensheldon/good_job/blob/v3.15.10/lib/good_job/adapter.rb
module ActiveJob # :nodoc:
module QueueAdapters # :nodoc:
# See {GoodJob::Adapter} for details.
class GoodJobAdapter < GoodJob::Adapter
end
end
end
enqueue
https://api.rubyonrails.org/classes/ActiveJob/Enqueuing.html#method-i-enqueue
https://github.com/rails/rails/blob/v7.0.5/activejob/lib/active_job/enqueuing.rb
module ActiveJob
module Enqueuing
module ClassMethods
def enqueue(options = {})
...
run_callbacks :enqueue do
if scheduled_at
queue_adapter.enqueue_at self, scheduled_at
else
queue_adapter.enqueue self
end
https://github.com/bensheldon/good_job/blob/v3.15.10/lib/good_job/adapter.rb
module GoodJob
class Adapter
def enqueue(active_job)
enqueue_at(active_job, nil)
end
...
def enqueue_at(active_job, timestamp)
...
execution = GoodJob::Execution.enqueue(
active_job,
scheduled_at: scheduled_at,
create_with_advisory_lock: will_execute_inline
)
https://github.com/bensheldon/good_job/blob/v3.15.10/app/models/good_job/execution.rb#L307
module GoodJob
class Execution < BaseExecution
def self.enqueue(active_job, scheduled_at: nil, create_with_advisory_lock: false)
ActiveSupport::Notifications.instrument("enqueue_job.good_job", { active_job: active_job, scheduled_at: scheduled_at, create_with_advisory_lock: create_with_advisory_lock }) do |instrument_payload|
...
instrument_payload[:execution] = execution
execution.save!
discrete
テーブル設計で discrete が気になったので検索してみました。
下記で今までの経緯と関連する提案が確認できます。
以上です。
この記事が気に入ったらサポートをしてみませんか?