Class: Cuniculus::QueueConfig

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

Constant Summary collapse

DEFAULT_MAX_RETRY =
8
DEFAULT_PREFETCH_COUNT =
10
DEFAULT_QUEUE_NAME =
"cun_default"
DEFAULT_THREAD_POOL_SIZE =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ QueueConfig

Returns a new instance of QueueConfig.



16
17
18
19
20
21
22
23
24
# File 'lib/cuniculus/queue_config.rb', line 16

def initialize(opts = {})
  opts = opts.transform_keys(&:to_s)
  @durable = read_opt(opts["durable"], true)
  @name = read_opt(opts["name"], DEFAULT_QUEUE_NAME)
  @max_retry = read_opt(opts["max_retry"], DEFAULT_MAX_RETRY)
  @prefetch_count = read_opt(opts["prefetch_count"], DEFAULT_PREFETCH_COUNT)
  @thread_pool_size = read_opt(opts["thread_pool_size"], DEFAULT_THREAD_POOL_SIZE)
  freeze
end

Instance Attribute Details

#durableObject (readonly)

Returns the value of attribute durable.



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

def durable
  @durable
end

#max_retryObject (readonly)

Returns the value of attribute max_retry.



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

def max_retry
  @max_retry
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#prefetch_countObject (readonly)

Returns the value of attribute prefetch_count.



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

def prefetch_count
  @prefetch_count
end

#thread_pool_sizeObject (readonly)

Returns the value of attribute thread_pool_size.



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

def thread_pool_size
  @thread_pool_size
end

Instance Method Details

#declare!(channel) ⇒ Object



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
56
57
58
59
60
# File 'lib/cuniculus/queue_config.rb', line 30

def declare!(channel)
  queue_name = name
  base_q = channel.queue(
    queue_name,
    durable: durable,
    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: durable,
      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(val, default) ⇒ Object



26
27
28
# File 'lib/cuniculus/queue_config.rb', line 26

def read_opt(val, default)
  val.nil? ? default : val
end