Class: Surfliner::Mq::Connection
- Inherits:
-
Object
- Object
- Surfliner::Mq::Connection
- Includes:
- Util::Assert
- Defined in:
- lib/surfliner/mq/connection.rb
Overview
An object encapsulating a RabbitMQ connection.
Instance Attribute Summary collapse
-
#channel ⇒ Bunny::Channel
readonly
The channel being listened to.
-
#config ⇒ ConnectionConfig
readonly
The configuration.
-
#logger ⇒ Logger
readonly
The logger.
-
#session ⇒ Bunny::Session
readonly
The current RabbitMQ session.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the session.
-
#connect {|connection| ... } ⇒ self
Opens this connection and creates a channel.
-
#host ⇒ String
The RabbitMQ hostname.
-
#initialize(logger:, config: ConnectionConfig.from_env) ⇒ Connection
constructor
Initializes a new
MqConnection. -
#open? ⇒ true, false
True if the session is open, false otherwise.
-
#port ⇒ String
The RabbitMQ port.
-
#status ⇒ Symbol?
The session status, or nil if there is no session.
-
#topic_from(config) ⇒ Topic
Returns a client for the specified topic.
-
#with_topic(config = TopicConfig.from_env) {|MqTopic| ... } ⇒ Object
Opens a session, yields a client for the specified topic, and closes the session after the provided block completes.
Methods included from Util::Assert
Constructor Details
#initialize(logger:, config: ConnectionConfig.from_env) ⇒ Connection
Initializes a new MqConnection.
26 27 28 29 |
# File 'lib/surfliner/mq/connection.rb', line 26 def initialize(logger:, config: ConnectionConfig.from_env) @logger = not_nil!(logger) @config = not_nil!(config) end |
Instance Attribute Details
#channel ⇒ Bunny::Channel (readonly)
Returns The channel being listened to.
17 18 19 |
# File 'lib/surfliner/mq/connection.rb', line 17 def channel @channel end |
#config ⇒ ConnectionConfig (readonly)
Returns The configuration.
20 21 22 |
# File 'lib/surfliner/mq/connection.rb', line 20 def config @config end |
#logger ⇒ Logger (readonly)
Returns The logger.
11 12 13 |
# File 'lib/surfliner/mq/connection.rb', line 11 def logger @logger end |
#session ⇒ Bunny::Session (readonly)
Returns The current RabbitMQ session.
14 15 16 |
# File 'lib/surfliner/mq/connection.rb', line 14 def session @session end |
Instance Method Details
#close ⇒ Object
Closes the session.
72 73 74 75 76 |
# File 'lib/surfliner/mq/connection.rb', line 72 def close logger.info("closing session") # Note: This will also close any open channels session&.close(config.await_response_on_close) end |
#connect {|connection| ... } ⇒ self
Opens this connection and creates a channel. If a block is given, yields the open connection, closing the connection and channel afterwards; otherwise, returns the open connection.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/surfliner/mq/connection.rb', line 37 def connect init_connection return self unless block_given? begin yield self ensure close end end |
#host ⇒ String
Returns The RabbitMQ hostname.
89 90 91 |
# File 'lib/surfliner/mq/connection.rb', line 89 def host config.host end |
#open? ⇒ true, false
Returns True if the session is open, false otherwise.
79 80 81 |
# File 'lib/surfliner/mq/connection.rb', line 79 def open? session&.status == :open end |
#port ⇒ String
Returns The RabbitMQ port.
94 95 96 |
# File 'lib/surfliner/mq/connection.rb', line 94 def port config.port end |
#status ⇒ Symbol?
Returns The session status, or nil if there is no session.
84 85 86 |
# File 'lib/surfliner/mq/connection.rb', line 84 def status session&.status end |
#topic_from(config) ⇒ Topic
Returns a client for the specified topic. Note that this does not open or close the connection.
65 66 67 68 69 |
# File 'lib/surfliner/mq/connection.rb', line 65 def topic_from(config) raise IOError, "RabbitMQ session not open" unless open? Topic.from_config(config, channel:, logger:) end |
#with_topic(config = TopicConfig.from_env) {|MqTopic| ... } ⇒ Object
Opens a session, yields a client for the specified topic, and closes the session after the provided block completes.
53 54 55 56 57 |
# File 'lib/surfliner/mq/connection.rb', line 53 def with_topic(config = TopicConfig.from_env) connect do |cxn| yield cxn.topic_from(config) end end |