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, error_handler: ErrorHandler.default, logger: Harmoniser.logger) ⇒ Connection

Returns a new instance of Connection.



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

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

Instance Method Details

#closeObject



55
56
57
58
59
60
61
62
63
# File 'lib/harmoniser/connection.rb', line 55

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

#retryable_startObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/harmoniser/connection.rb', line 43

def retryable_start
  retries = 0
  begin
    @bunny.start
  rescue => e
    handle_error(e, {description: "Connection attempt failed", retries: retries})
    sleep(1)
    retries += 1
    retry
  end
end

#startObject



37
38
39
40
41
# File 'lib/harmoniser/connection.rb', line 37

def start
  return @bunny.start unless Harmoniser.server?

  retryable_start
end

#to_sObject



33
34
35
# File 'lib/harmoniser/connection.rb', line 33

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