class ActiveJob::QueueAdapters::HutchAdapter
Hutch
adapter for Active Job¶ ↑
Rails.application.config.active_job.queue_adapter = :hutch
Constants
- AJ_ROUTING_KEY
All activejob Message will routing to one RabbitMQ Queue. Because
Hutch
will one Consumer per Queue
Public Class Methods
new()
click to toggle source
# File lib/active_job/queue_adapters/hutch_adapter.rb, line 16 def initialize @monitor = Monitor.new end
register_actice_job_classes()
click to toggle source
Register all ActiveJob
Class to Hutch
. (per queue per consumer)
# File lib/active_job/queue_adapters/hutch_adapter.rb, line 38 def self.register_actice_job_classes # TODO: 需要考虑如何将 AJ 的 Proc queue_name 动态注册到 Hutch Dir.glob(Rails.root.join('app/jobs/**/*.rb')).each { |x| require_dependency x } ActiveJob::Base.descendants.each do |job_clazz| # Need activeJob instance #queue_name job = job_clazz.new # Multi queue only have one consumer next if @@queue_consumers.key?(job.queue_name) @@queue_consumers[job.queue_name] = HutchAdapter.dynamic_consumer(job) Hutch.register_consumer(@@queue_consumers[job.queue_name]) end end
routing_key(job)
click to toggle source
Get an routing_key
# File lib/active_job/queue_adapters/hutch_adapter.rb, line 33 def self.routing_key(job) "#{AJ_ROUTING_KEY}.#{job.queue_name}" end
Private Class Methods
dynamic_consumer(job_instance)
click to toggle source
# File lib/active_job/queue_adapters/hutch_adapter.rb, line 53 def self.dynamic_consumer(job_instance) Class.new do # don't include Hutch::Consumer, we should change the name of consumer to registe extend Hutch::Consumer::ClassMethods include Hutch::Enqueue attr_accessor :broker, :delivery_info queue_name job_instance.queue_name consume HutchAdapter.routing_key(job_instance) def process(job_data) ActiveJob::Base.execute(job_data) end define_singleton_method :name do "#{job_instance.queue_name}_dynamic_consumer".camelize end # inspect name define_singleton_method :inspect do "#{job_instance.queue_name}_dynamic_consumer".camelize end define_singleton_method :to_s do "#{job_instance.queue_name}_dynamic_consumer".camelize end end end
Private Instance Methods
process(job_data)
click to toggle source
# File lib/active_job/queue_adapters/hutch_adapter.rb, line 64 def process(job_data) ActiveJob::Base.execute(job_data) end