Class: Surfliner::Mq::Connection

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

Overview

An object encapsulating a RabbitMQ connection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Assert

#not_nil!

Constructor Details

#initialize(logger:, config: ConnectionConfig.from_env) ⇒ Connection

Initializes a new MqConnection.

Parameters:

  • logger (Logger)

    the logger

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

    the configuration



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

#channelBunny::Channel (readonly)

Returns The channel being listened to.

Returns:

  • (Bunny::Channel)

    The channel being listened to



17
18
19
# File 'lib/surfliner/mq/connection.rb', line 17

def channel
  @channel
end

#configConnectionConfig (readonly)

Returns The configuration.

Returns:



20
21
22
# File 'lib/surfliner/mq/connection.rb', line 20

def config
  @config
end

#loggerLogger (readonly)

Returns The logger.

Returns:

  • (Logger)

    The logger



11
12
13
# File 'lib/surfliner/mq/connection.rb', line 11

def logger
  @logger
end

#sessionBunny::Session (readonly)

Returns The current RabbitMQ session.

Returns:

  • (Bunny::Session)

    The current RabbitMQ session



14
15
16
# File 'lib/surfliner/mq/connection.rb', line 14

def session
  @session
end

Instance Method Details

#closeObject

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.

Yield Parameters:

  • connection (self)

    an open connection

Returns:

  • (self)

    an open connection

Raises:

  • (IOError)

    if already connected



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

#hostString

Returns The RabbitMQ hostname.

Returns:

  • (String)

    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.

Returns:

  • (true, false)

    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

#portString

Returns The RabbitMQ port.

Returns:

  • (String)

    The RabbitMQ port



94
95
96
# File 'lib/surfliner/mq/connection.rb', line 94

def port
  config.port
end

#statusSymbol?

Returns The session status, or nil if there is no session.

Returns:

  • (Symbol, nil)

    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.

Parameters:

Returns:

  • (Topic)

    A client for the topic

Raises:

  • (IOError)

    if the connection is not open



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.

Parameters:

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

    topic configuration

Yields:

  • (MqTopic)

    A client for the topic



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