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.



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

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

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



7
8
9
# File 'lib/msgr/channel.rb', line 7

def channel
  @channel
end

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/msgr/channel.rb', line 7

def config
  @config
end

Instance Method Details

#ack(delivery_tag) ⇒ Object



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

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

#closeObject



61
62
63
# File 'lib/msgr/channel.rb', line 61

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

#exchangeObject



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

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



51
52
53
54
# File 'lib/msgr/channel.rb', line 51

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

#prefetch(count) ⇒ Object



14
15
16
# File 'lib/msgr/channel.rb', line 14

def prefetch(count)
  @channel.prefetch count
end

#prefix(name) ⇒ Object



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

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

#queue(name) ⇒ Object



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

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

#reject(delivery_tag, requeue = true) ⇒ Object



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

def reject(delivery_tag, requeue = true)
  @channel.reject delivery_tag, requeue
  log(:debug) { "Rejected message: #{delivery_tag}" }
end