Class: Harmoniser::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/harmoniser/connection.rb

Constant Summary collapse

DEFAULT_CONNECTION_OPTS =
{
  connection_name: "harmoniser@#{VERSION}",
  connection_timeout: 5,
  host: "127.0.0.1",
  password: "guest",
  port: 5672,
  read_timeout: 5,
  tls_silence_warnings: true,
  username: "guest",
  verify_peer: false,
  vhost: "/",
  write_timeout: 5
}

Instance Method Summary collapse

Constructor Details

#initialize(opts, logger: Harmoniser.logger) ⇒ Connection

Returns a new instance of Connection.



24
25
26
27
28
29
# File 'lib/harmoniser/connection.rb', line 24

def initialize(opts, logger: Harmoniser.logger)
  @logger = logger
  @bunny = Bunny.new(maybe_dynamic_opts(opts)).tap do |bunny|
    attach_callbacks(bunny)
  end
end

Instance Method Details

#closeObject



48
49
50
51
52
53
54
55
56
# File 'lib/harmoniser/connection.rb', line 48

def close
  @logger.info("Connection will be closed: connection = `#{self}`")
  @bunny.close.tap do
    @logger.info("Connection closed: connection = `#{self}`")
  end
rescue => e
  @logger.error("Connection#close failed: error_class = `#{e.class}`, error_message = `#{e.message}`")
  false
end

#startObject

TODO Only perform retries when Harmoniser.server?



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/harmoniser/connection.rb', line 36

def start
  retries = 0
  begin
    with_signal_handler { @bunny.start }
  rescue => e
    @logger.error("Connection attempt failed: retries = `#{retries}`, error_class = `#{e.class}`, error_message = `#{e.message}`")
    with_signal_handler { sleep(1) }
    retries += 1
    retry
  end
end

#to_sObject



31
32
33
# File 'lib/harmoniser/connection.rb', line 31

def to_s
  "<#{self.class.name}>:#{object_id} #{user}@#{host}:#{port}, connection_name = `#{connection_name}`, vhost = `#{vhost}`"
end