Class: Warren::Config::Consumers

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

Overview

Manages the configuration of consumers. By default, consumer configuration is held in config/warren_consumers.yml

Constant Summary collapse

DEFAULT_PATH =

Default path to the consumer configuration file

'config/warren_consumers.yml'
WRITE_ONLY_TRUNCATE =
'w'

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Consumers

Returns a new instance of Consumers.



16
17
18
19
# File 'lib/warren/config/consumers.rb', line 16

def initialize(path)
  @path = path
  @config = load_config
end

Instance Method Details

#add_consumer(name, desc:, queue:, bindings:, subscribed_class:, delay:) ⇒ Hash

Register a new consumer

rubocop:todo Metrics/ParameterLists

Parameters:

  • name (String)

    The name of the consumer to register

  • desc (String)

    Description of the consumer (Primarily for documentation)

  • queue (String)

    Name of the queue to attach to

  • bindings (Array<Hash>)

    Array of binding configuration hashed

  • delay (Integer)

    Delay on the generated delay exchange

Returns:

  • (Hash)

    The consumer configuration hash



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/warren/config/consumers.rb', line 68

def add_consumer(name, desc:, queue:, bindings:, subscribed_class:, delay:)
  dead_letter_exchange = "#{name}.dead-letters"
  @config[name] = {
    'desc' => desc,
    'queue' => queue_config(queue, bindings, dead_letter_exchange),
    'subscribed_class' => subscribed_class,
    # This smells wrong. I don't like the call back out to the App namespace
    'dead_letters' => queue_config(dead_letter_exchange,
                                   Warren::App::ExchangeConfig.default_dead_letter(dead_letter_exchange)),
    'delay' => delay_exchange_configuration(ttl: delay, original_queue: queue, consumer_name: name),
    'worker_count' => 3
  }
end

#all_consumersArray<string>

Returns a list of all registered consumers

Returns:

  • (Array<string>)

    An array of registered consumer names



52
53
54
# File 'lib/warren/config/consumers.rb', line 52

def all_consumers
  @config.keys
end

#consumer(name) ⇒ Object



43
44
45
# File 'lib/warren/config/consumers.rb', line 43

def consumer(name)
  @config.fetch(name) { raise StandardError, "Unknown consumer '#{name}'" }
end

#consumer_exist?(name) ⇒ Boolean

Checks whether a consumer has already been registered

Parameters:

  • name (String)

    The name of the consumer to check

Returns:

  • (Boolean)

    True if the consumer exists



39
40
41
# File 'lib/warren/config/consumers.rb', line 39

def consumer_exist?(name)
  @config.key?(name)
end

#saveVoid

Save the configuration to ‘@path`

Returns:

  • (Void)


26
27
28
29
30
# File 'lib/warren/config/consumers.rb', line 26

def save
  File.open(@path, WRITE_ONLY_TRUNCATE) do |file|
    file.write YAML.dump(@config)
  end
end