Class: Surfliner::Mq::Topic

Inherits:
Object
  • Object
show all
Includes:
Util::Assert
Defined in:
lib/surfliner/mq/topic.rb

Overview

Encapsulates a RabbitMQ topic

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Assert

#not_nil!

Constructor Details

#initialize(name, channel:, logger:, options: {}) ⇒ Topic

Returns a new instance of Topic.

Parameters:

  • name (String)

    The name of the topic

  • channel (Bunny::Channel)

    The channel to use

  • logger (Logger)

    the logger

  • options (Hash) (defaults to: {})

    RabbitMQ topic options. (See Bunny::Channel#topic)



25
26
27
28
29
30
# File 'lib/surfliner/mq/topic.rb', line 25

def initialize(name, channel:, logger:, options: {})
  @name = not_nil!(name, "topic name")
  @channel = not_nil!(channel, "channel")
  @logger = not_nil!(logger, "logger")
  @options = not_nil!(options, "options")
end

Instance Attribute Details

#channelBunny::Channel (readonly)

Returns The channel being used.

Returns:

  • (Bunny::Channel)

    The channel being used



13
14
15
# File 'lib/surfliner/mq/topic.rb', line 13

def channel
  @channel
end

#loggerLogger (readonly)

Returns The logger.

Returns:

  • (Logger)

    The logger



16
17
18
# File 'lib/surfliner/mq/topic.rb', line 16

def logger
  @logger
end

#nameString (readonly)

Returns The name of the topic.

Returns:

  • (String)

    The name of the topic



10
11
12
# File 'lib/surfliner/mq/topic.rb', line 10

def name
  @name
end

#optionsHash (readonly)

Returns RabbitMQ topic options. (See Bunny::Channel#topic).

Returns:

  • (Hash)

    RabbitMQ topic options. (See Bunny::Channel#topic)



19
20
21
# File 'lib/surfliner/mq/topic.rb', line 19

def options
  @options
end

Class Method Details

.from_config(config, channel:, logger:) ⇒ Object

Creates a new MqTopic from the specified configuration

Parameters:

  • config (TopicConfig)

    the configuration

  • channel (Bunny::Channel)

    The channel to use

  • logger (Logger)

    the logger



78
79
80
# File 'lib/surfliner/mq/topic.rb', line 78

def from_config(config, channel:, logger:)
  new(config.name, channel:, logger:, options: config.options)
end

Instance Method Details

#bind_queue(config = QueueConfig.from_env, routing_key: default_routing_key) ⇒ Bunny::Queue

Creates or looks up the specified queue and binds it to receive messages with the specified routing key.

Parameters:

  • config (QueueConfig) (defaults to: QueueConfig.from_env)

    queue configuration

  • routing_key (String) (defaults to: default_routing_key)

    platform routing key to bind on

Returns:

  • (Bunny::Queue)

    the queue



51
52
53
# File 'lib/surfliner/mq/topic.rb', line 51

def bind_queue(config = QueueConfig.from_env, routing_key: default_routing_key)
  queue(config).tap { |q| q.bind(exchange, routing_key:) }
end

#default_routing_keyString?

Returns the default (environment-variable-based) routing key

Variable Sample value Description
RABBITMQ_PLATFORM_ROUTING_KEY surfliner.metadata.daylight RabbitMQ routing key

Returns:

  • (String, nil)

    the configured default routing key



69
70
71
# File 'lib/surfliner/mq/topic.rb', line 69

def default_routing_key
  ENV.fetch("RABBITMQ_PLATFORM_ROUTING_KEY")
end

#exchangeBunny::Exchange

Returns The exchange for the topic.

Returns:

  • (Bunny::Exchange)

    The exchange for the topic



33
34
35
# File 'lib/surfliner/mq/topic.rb', line 33

def exchange
  @exchange ||= channel.topic(name, options)
end

#publish(payload, routing_key: default_routing_key) ⇒ Bunny::Exchange

Publishes the specified payload

Parameters:

  • payload (String)

    the payload to publish

  • routing_key (String) (defaults to: default_routing_key)

    platform routing key to publish to

Returns:

  • (Bunny::Exchange)

    see #exchange



41
42
43
44
# File 'lib/surfliner/mq/topic.rb', line 41

def publish(payload, routing_key: default_routing_key)
  logger.info "Publishing to #{routing_key} with payload: #{payload}"
  exchange.publish(payload, routing_key:)
end

#queue(config) ⇒ Bunny::Queue

Creates or looks up the specified queue.

Parameters:

Returns:

  • (Bunny::Queue)

    the queue



58
59
60
# File 'lib/surfliner/mq/topic.rb', line 58

def queue(config)
  channel.queue(config.name, config.options)
end