Class: ActiveJob::QueueAdapters::HutchAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_job/queue_adapters/hutch_adapter.rb

Overview

Hutch adapter for Active Job

Read more about Hutch here.

Rails.application.config.active_job.queue_adapter = :hutch

Constant Summary collapse

AJ_ROUTING_KEY =

All activejob Message will routing to one RabbitMQ Queue. Because Hutch will one Consumer per Queue

"active_job"
@@queue_consumers =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHutchAdapter

Returns a new instance of HutchAdapter.



16
17
18
# File 'lib/active_job/queue_adapters/hutch_adapter.rb', line 16

def initialize
  @monitor = Monitor.new
end

Class Method Details

.register_actice_job_classesObject

Register all ActiveJob Class to Hutch. (per queue per consumer)



38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object

Get an routing_key



33
34
35
# File 'lib/active_job/queue_adapters/hutch_adapter.rb', line 33

def self.routing_key(job)
  "#{AJ_ROUTING_KEY}.#{job.queue_name}"
end

Instance Method Details

#enqueue(job) ⇒ Object

:nodoc:



20
21
22
23
24
# File 'lib/active_job/queue_adapters/hutch_adapter.rb', line 20

def enqueue(job) #:nodoc:
  @monitor.synchronize do
    @@queue_consumers[job.queue_name].enqueue(job.serialize)
  end
end

#enqueue_at(job, timestamp) ⇒ Object

:nodoc:



26
27
28
29
30
# File 'lib/active_job/queue_adapters/hutch_adapter.rb', line 26

def enqueue_at(job, timestamp) #:nodoc:
  @monitor.synchronize do
    @@queue_consumers[job.queue_name].enqueue_at(timestamp, job.serialize)
  end
end