Class: Msgr::Channel

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

Constant Summary collapse

EXCHANGE_NAME =
'msgr'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, #log_name

Constructor Details

#initialize(config, connection) ⇒ Channel

Returns a new instance of Channel.



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

def initialize(config, connection)
  @config  = config
  @channel = connection.create_channel
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



9
10
11
# File 'lib/msgr/channel.rb', line 9

def channel
  @channel
end

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/msgr/channel.rb', line 9

def config
  @config
end

Instance Method Details

#ack(delivery_tag) ⇒ Object



48
49
50
51
# File 'lib/msgr/channel.rb', line 48

def ack(delivery_tag)
  @channel.ack delivery_tag
  log(:debug) { "Acked message: #{delivery_tag}" }
end

#closeObject



58
59
60
# File 'lib/msgr/channel.rb', line 58

def close
  @channel.close if @channel.open?
end

#exchangeObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/msgr/channel.rb', line 20

def exchange
  @exchange ||= begin
    @channel.topic(prefix(EXCHANGE_NAME), durable: true).tap do |ex|
      log(:debug) do
        "Created exchange #{ex.name} (type: #{ex.type}, " \
          "durable: #{ex.durable?}, auto_delete: #{ex.auto_delete?})"
      end
    end
  end
end

#nack(delivery_tag) ⇒ Object



53
54
55
56
# File 'lib/msgr/channel.rb', line 53

def nack(delivery_tag)
  @channel.nack delivery_tag, false, true
  log(:debug) { "Nacked message: #{delivery_tag}" }
end

#prefetch(count) ⇒ Object



16
17
18
# File 'lib/msgr/channel.rb', line 16

def prefetch(count)
  @channel.prefetch count
end

#prefix(name) ⇒ Object



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

def prefix(name)
  if config[:prefix].present?
    "#{config[:prefix]}-#{name}"
  else
    name
  end
end

#queue(name, **opts) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/msgr/channel.rb', line 31

def queue(name, **opts)
  @channel.queue(prefix(name), durable: true, **opts).tap do |queue|
    log(:debug) do
      "Create queue #{queue.name} (durable: #{queue.durable?}, " \
        "auto_delete: #{queue.auto_delete?})"
    end
  end
end