Class: Carrot::AMQP::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/carrot/amqp/queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(carrot, name, opts = {}) ⇒ Queue

Returns a new instance of Queue.



6
7
8
9
10
11
12
13
14
15
# File 'lib/carrot/amqp/queue.rb', line 6

def initialize(carrot, name, opts = {})
  @opts   = opts
  @name   = name
  @carrot = carrot

  @opts[:queue] = name
  server.send_frame(
    Protocol::Queue::Declare.new(@opts.merge(:nowait => true))
  )
end

Instance Attribute Details

#carrotObject (readonly)

Returns the value of attribute carrot.



3
4
5
# File 'lib/carrot/amqp/queue.rb', line 3

def carrot
  @carrot
end

#delivery_tagObject

Returns the value of attribute delivery_tag.



4
5
6
# File 'lib/carrot/amqp/queue.rb', line 4

def delivery_tag
  @delivery_tag
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/carrot/amqp/queue.rb', line 3

def name
  @name
end

#optsObject

Returns the value of attribute opts.



4
5
6
# File 'lib/carrot/amqp/queue.rb', line 4

def opts
  @opts
end

Instance Method Details

#ackObject



37
38
39
40
41
# File 'lib/carrot/amqp/queue.rb', line 37

def ack
  server.send_frame(
    Protocol::Basic::Ack.new(:delivery_tag => delivery_tag)
  )
end

#bind(exchange, opts = {}) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/carrot/amqp/queue.rb', line 65

def bind(exchange, opts = {})
  exchange           = exchange.respond_to?(:name) ? exchange.name : exchange
  bindings[exchange] = opts
  server.send_frame(
    Protocol::Queue::Bind.new({ :queue => name, :exchange => exchange, :routing_key => opts.delete(:key), :nowait => true }.merge(opts))
  )
end

#consumer_countObject



51
52
53
# File 'lib/carrot/amqp/queue.rb', line 51

def consumer_count
  status.last
end

#delete(opts = {}) ⇒ Object



84
85
86
87
88
89
# File 'lib/carrot/amqp/queue.rb', line 84

def delete(opts = {})
  server.send_frame(
    Protocol::Queue::Delete.new({ :queue => name, :nowait => true }.merge(opts))
  )
  carrot.queues.delete(name)
end

#message_countObject



47
48
49
# File 'lib/carrot/amqp/queue.rb', line 47

def message_count
  status.first
end

#pop(opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/carrot/amqp/queue.rb', line 17

def pop(opts = {})
  self.delivery_tag = nil
  server.send_frame(
    Protocol::Basic::Get.new({ :queue => name, :consumer_tag => name, :no_ack => !opts.delete(:ack), :nowait => true }.merge(opts))
  )
  method = server.next_method
  return unless method.kind_of?(Protocol::Basic::GetOk)

  self.delivery_tag = method.delivery_tag

  header = server.next_payload

  msg = ''
  while msg.length < header.size
    msg << server.next_payload
  end

  msg
end

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



43
44
45
# File 'lib/carrot/amqp/queue.rb', line 43

def publish(data, opts = {})
  exchange.publish(data, opts)
end

#purge(opts = {}) ⇒ Object



91
92
93
94
95
# File 'lib/carrot/amqp/queue.rb', line 91

def purge(opts = {})
  server.send_frame(
    Protocol::Queue::Purge.new({ :queue => name, :nowait => true }.merge(opts))
  )
end

#serverObject



97
98
99
# File 'lib/carrot/amqp/queue.rb', line 97

def server
  carrot.server
end

#statusObject



55
56
57
58
59
60
61
62
63
# File 'lib/carrot/amqp/queue.rb', line 55

def status
  server.send_frame(
    Protocol::Queue::Declare.new(opts)
  )
  method = server.next_method
  return [nil, nil] if method.kind_of?(Protocol::Connection::Close)

  [method.message_count, method.consumer_count]
end

#unbind(exchange, opts = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/carrot/amqp/queue.rb', line 73

def unbind(exchange, opts = {})
  exchange = exchange.respond_to?(:name) ? exchange.name : exchange
  bindings.delete(exchange)

  server.send_frame(
    Protocol::Queue::Unbind.new({
      :queue => name, :exchange => exchange, :routing_key => opts.delete(:key), :nowait => true }.merge(opts)
    )
  )
end