Class: Cuniculus::QueueConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/cuniculus/queue_config.rb

Constant Summary collapse

OPTS =
{}.freeze
DEFAULT_MAX_RETRY =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = OPTS) ⇒ QueueConfig

Returns a new instance of QueueConfig.



15
16
17
18
19
# File 'lib/cuniculus/queue_config.rb', line 15

def initialize(opts = OPTS)
  @name = read_opt(opts, "name") || "cun_default"
  @max_retry = read_opt(opts, "max_retry") || DEFAULT_MAX_RETRY
  @thread_pool_size = read_opt(opts, "thread_pool_size")
end

Instance Attribute Details

#max_retryObject (readonly)

Returns the value of attribute max_retry.



13
14
15
# File 'lib/cuniculus/queue_config.rb', line 13

def max_retry
  @max_retry
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/cuniculus/queue_config.rb', line 13

def name
  @name
end

#thread_pool_sizeObject (readonly)

Returns the value of attribute thread_pool_size.



13
14
15
# File 'lib/cuniculus/queue_config.rb', line 13

def thread_pool_size
  @thread_pool_size
end

Instance Method Details

#declare!(channel) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cuniculus/queue_config.rb', line 25

def declare!(channel)
  queue_name = name
  base_q = channel.queue(
    queue_name,
    durable: true,
    exclusive: false,
    arguments: { "x-dead-letter-exchange" => Cuniculus::CUNICULUS_DLX_EXCHANGE }
  )
  base_q.bind(Cuniculus::CUNICULUS_EXCHANGE, { routing_key: name })

  retry_queue_names = (1..max_retry).map { |i| "#{name}_#{i}" }
  max_retry.times do |i|
    queue_name = retry_queue_names[i]

    q = channel.queue(
      queue_name,
      durable: true,
      exclusive: false,
      arguments: {
        "x-dead-letter-exchange" => Cuniculus::CUNICULUS_EXCHANGE,
        "x-dead-letter-routing-key" => name,
        "x-message-ttl" => ((i**4) + (15 * (i + 1))) * 1000
      }
    )
    q.bind(Cuniculus::CUNICULUS_EXCHANGE, { routing_key: queue_name })
  end

  Cuniculus::JobQueue.new(base_q, retry_queue_names)
rescue Bunny::PreconditionFailed => e
  raise Cuniculus.convert_exception_class(e, Cuniculus::RMQQueueConfigurationConflict), "Declaration failed for queue '#{queue_name}'"
end

#read_opt(opts, key) ⇒ Object



21
22
23
# File 'lib/cuniculus/queue_config.rb', line 21

def read_opt(opts, key)
  opts[key.to_s] || opts[key.to_sym]
end