Class: Cuniculus::Config

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

Constant Summary collapse

ENFORCED_CONN_OPTS =
{
  threaded: false, # No need for a reader thread, since this connection is only used for declaring exchanges and queues.
  automatically_recover: false,
  log_level: ::Logger::ERROR
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cuniculus/config.rb', line 30

def initialize
  @opts = {}

  # ---- Default values
  @queues = { "cun_default" => QueueConfig.new({ "name" => "cun_default" }) }
  @rabbitmq_opts = {
    host: "127.0.0.1",
    port: 5672,
    user: "guest",
    pass: "guest",
    vhost: "/"
  }
  @exchange_name = Cuniculus::CUNICULUS_EXCHANGE
  @dead_queue_ttl = 1000 * 60 * 60 * 24 * 180 # 180 days
  @pub_reconnect_attempts = :infinite
  @pub_reconnect_delay = 1.5
  @pub_reconnect_delay_max = 10
  @pub_shutdown_grace_period = 50
  @pub_pool_size = 5
  ## ---- End of default values
end

Instance Attribute Details

#dead_queue_ttlObject

Returns the value of attribute dead_queue_ttl.



16
17
18
# File 'lib/cuniculus/config.rb', line 16

def dead_queue_ttl
  @dead_queue_ttl
end

#exchange_nameObject

Returns the value of attribute exchange_name.



16
17
18
# File 'lib/cuniculus/config.rb', line 16

def exchange_name
  @exchange_name
end

#optsObject (readonly)

Returns the value of attribute opts.



16
17
18
# File 'lib/cuniculus/config.rb', line 16

def opts
  @opts
end

#pub_pool_sizeObject

Returns the value of attribute pub_pool_size.



16
17
18
# File 'lib/cuniculus/config.rb', line 16

def pub_pool_size
  @pub_pool_size
end

#pub_reconnect_attemptsObject

Returns the value of attribute pub_reconnect_attempts.



16
17
18
# File 'lib/cuniculus/config.rb', line 16

def pub_reconnect_attempts
  @pub_reconnect_attempts
end

#pub_reconnect_delayObject

Returns the value of attribute pub_reconnect_delay.



16
17
18
# File 'lib/cuniculus/config.rb', line 16

def pub_reconnect_delay
  @pub_reconnect_delay
end

#pub_reconnect_delay_maxObject

Returns the value of attribute pub_reconnect_delay_max.



16
17
18
# File 'lib/cuniculus/config.rb', line 16

def pub_reconnect_delay_max
  @pub_reconnect_delay_max
end

#pub_shutdown_grace_periodObject

Returns the value of attribute pub_shutdown_grace_period.



16
17
18
# File 'lib/cuniculus/config.rb', line 16

def pub_shutdown_grace_period
  @pub_shutdown_grace_period
end

#queuesObject (readonly)

Returns the value of attribute queues.



16
17
18
# File 'lib/cuniculus/config.rb', line 16

def queues
  @queues
end

#rabbitmq_optsObject

Returns the value of attribute rabbitmq_opts.



28
29
30
# File 'lib/cuniculus/config.rb', line 28

def rabbitmq_opts
  @rabbitmq_opts
end

Instance Method Details

#add_queue(qopts) ⇒ Object

Configure an additional queue

Note that a single call to ‘add_queue` might lead to the creation of multiple queues on RabbitMQ: one base queue, and an additional queue for every retry attempt. For example, with a queue named `“test”` with `max_retry` set to `4`, 5 queues are created in RabbitMQ.

For tuning ‘prefetch_count`, refer to [this guide](www.cloudamqp.com/blog/2017-12-29-part1-rabbitmq-best-practice.html#prefetch).

If a queue already exists in RabbitMQ, and an attempt is done to add it again through ‘add_queue`, nothing happens, except if the options passed to `add_queue` conflict with the existing queue. For example if a queue exists that is durable, and `add_queue` is called with `“durable” => false`, a `Cuniculus::RMQQueueConfigurationConflict` is raised. To redeclare a queue with conflicting configurations, the original queue has first to be removed from RabbitMQ manually. This can be done, for example, through the management console.

Examples:

Add queue named “critical”

Cuniculus.configure do |cfg|
  cfg.add_queue({ name: "critical", max_retry: 10 })
end

Parameters:

  • qopts (Hash)

    Queue config options.

Options Hash (qopts):

  • "name" (String)

    Name of the queue.

  • "durable" (Boolean) — default: true

    Whether queue is declared as durable in RabbitMQ. Jobs in non-durable queues may be lost if the RabbitMQ goes down.

  • "max_retry" (Integer) — default: 8

    Number of retries for failed jobs in this queue.

  • "prefetch_count" (Integer) — default: 10

    Prefetch count used when consuming jobs from this queue.

  • "thread_pool_size" (Integer) — default: 5

    Thread pool size for receiving jobs.

Raises:

  • (Cuniculus::ConfigError)


107
108
109
110
111
112
# File 'lib/cuniculus/config.rb', line 107

def add_queue(qopts)
  qopts = qopts.transform_keys(&:to_s)
  qname = qopts["name"].to_s
  raise Cuniculus::ConfigError, "Missing 'name' key in queue configuration hash" if qname.strip.empty?
  @queues[qname] = QueueConfig.new(qopts)
end

#declare!Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cuniculus/config.rb', line 114

def declare!
  conn = ::Bunny.new(rabbitmq_opts.merge(ENFORCED_CONN_OPTS))
  conn.start
  ch = conn.create_channel
  declare_exchanges!(ch)
  declare_dead_queue!(ch)
  @queues.each_value { |q| q.declare!(ch) }
  conn.close unless conn.closed?
rescue Bunny::TCPConnectionFailed => ex
  raise Cuniculus.convert_exception_class(ex, Cuniculus::RMQConnectionError)
end

#default_queue=(bool) ⇒ Object

Specify if the default queue ‘cun_default` should be created. `cun_default` is used by workers that don’t explicitly specify a queue with ‘cuniculus_options queue: “another_queue”`.

Parameters:

  • bool (Boolean)

    If false, queue ‘cun_default` is not created. Defaults to `true`.



130
131
132
# File 'lib/cuniculus/config.rb', line 130

def default_queue=(bool)
  @queues.delete("cun_default") unless bool
end