Class: Msgr::Connection

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/msgr/connection.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, #log_name

Constructor Details

#initialize(uri, config, dispatcher) ⇒ Connection

Returns a new instance of Connection


13
14
15
16
17
18
# File 'lib/msgr/connection.rb', line 13

def initialize(uri, config, dispatcher)
  @uri        = uri
  @config     = config
  @dispatcher = dispatcher
  @channels   = []
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config


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

def config
  @config
end

#uriObject (readonly)

Returns the value of attribute uri


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

def uri
  @uri
end

Instance Method Details

#bind(routes) ⇒ Object


76
77
78
79
80
81
82
83
84
85
# File 'lib/msgr/connection.rb', line 76

def bind(routes)
  if routes.empty?
    log(:warn) do
      "No routes to bound to. Bind will have no effect:\n" \
      "  #{routes.inspect}"
    end
  else
    bind_all(routes)
  end
end

#bindingsObject


72
73
74
# File 'lib/msgr/connection.rb', line 72

def bindings
  @bindings ||= []
end

#channel(prefetch: 1) ⇒ Object


40
41
42
43
44
45
# File 'lib/msgr/connection.rb', line 40

def channel(prefetch: 1)
  channel = Msgr::Channel.new(config, connection)
  channel.prefetch(prefetch)
  @channels << channel
  channel
end

#closeObject


87
88
89
90
91
# File 'lib/msgr/connection.rb', line 87

def close
  @channels.each(&:close)
  connection.close if @connection
  log(:debug) { 'Closed.' }
end

#connectObject


36
37
38
# File 'lib/msgr/connection.rb', line 36

def connect
  connection
end

#connectionObject


32
33
34
# File 'lib/msgr/connection.rb', line 32

def connection
  @connection ||= ::Bunny.new(config).tap(&:start)
end

#deleteObject


58
59
60
61
62
63
# File 'lib/msgr/connection.rb', line 58

def delete
  return if bindings.empty?
  log(:debug) { "Delete bindings (#{bindings.size})..." }

  bindings.each(&:delete)
end

#exchangeObject


47
48
49
# File 'lib/msgr/connection.rb', line 47

def exchange
  @exchange ||= channel.exchange
end

#publish(message, opts = {}) ⇒ Object


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

def publish(message, opts = {})
  opts[:routing_key] = opts.delete(:to) if opts[:to]

  exchange.publish message.to_s, opts.merge(persistent: true)

  log(:debug) { "Published message to #{opts[:routing_key]}" }
end

#purge(**kwargs) ⇒ Object


65
66
67
68
69
70
# File 'lib/msgr/connection.rb', line 65

def purge(**kwargs)
  return if bindings.empty?
  log(:debug) { "Purge bindings (#{bindings.size})..." }

  bindings.each {|b| b.purge(**kwargs) }
end

#releaseObject


51
52
53
54
55
56
# File 'lib/msgr/connection.rb', line 51

def release
  return if bindings.empty?
  log(:debug) { "Release bindings (#{bindings.size})..." }

  bindings.each(&:release)
end

#running?Boolean

Returns:

  • (Boolean)

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

def running?
  bindings.any?
end