Class: IronMQ::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_mq/queues.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, res) ⇒ Queue

Returns a new instance of Queue.



72
73
74
75
# File 'lib/iron_mq/queues.rb', line 72

def initialize(client, res)
  @client = client
  @data = res
end

Instance Method Details

#[](key) ⇒ Object



81
82
83
# File 'lib/iron_mq/queues.rb', line 81

def [](key)
  raw[key]
end

#clearObject



105
106
107
# File 'lib/iron_mq/queues.rb', line 105

def clear()
  @client.queues.clear(:name => name)
end

#delete(id, options = {}) ⇒ Object



162
163
164
# File 'lib/iron_mq/queues.rb', line 162

def delete(id, options={})
  @client.messages.delete(id, options.merge(:queue_name => name))
end

#delete_queueObject



109
110
111
# File 'lib/iron_mq/queues.rb', line 109

def delete_queue()
  @client.queues.delete(:name=>name)
end

#get(options = {}) ⇒ Object



133
134
135
# File 'lib/iron_mq/queues.rb', line 133

def get(options={})
  @client.messages.get(options.merge(:queue_name => name))
end

#idObject



85
86
87
# File 'lib/iron_mq/queues.rb', line 85

def id
  raw["id"]
end

#load_queueObject

Used if lazy loading



98
99
100
101
102
103
# File 'lib/iron_mq/queues.rb', line 98

def load_queue
  q = @client.queues.get(:name => name)
  @client.logger.debug "GOT Q: " + q.inspect
  @data = q.raw
  q
end

#nameObject



89
90
91
# File 'lib/iron_mq/queues.rb', line 89

def name
  raw["name"]
end

#poll(options = {}, &blk) ⇒ Object

This will continuously poll for a message and pass it to the block. For example:

queue.poll { |msg| puts msg.body }

options:

  • :sleep_duration=>seconds => time between polls if msg is nil. default 1.

  • :break_if_nil=>true/false => if true, will break if msg is nil (ie: queue is empty)



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/iron_mq/queues.rb', line 144

def poll(options={}, &blk)
  sleep_duration = options[:sleep_duration] || 1
  while true
    #p options
    msg = get(options)
    if msg.nil?
      if options[:break_if_nil]
        break
      else
        sleep sleep_duration
      end
    else
      yield msg
      msg.delete
    end
  end
end

#post(body, options = {}) ⇒ Object



129
130
131
# File 'lib/iron_mq/queues.rb', line 129

def post(body, options={})
  @client.messages.post(body, options.merge(:queue_name => name))
end

#rawObject



77
78
79
# File 'lib/iron_mq/queues.rb', line 77

def raw
  @data
end

#reloadObject



93
94
95
# File 'lib/iron_mq/queues.rb', line 93

def reload
  load_queue
end

#sizeObject



113
114
115
116
117
118
119
# File 'lib/iron_mq/queues.rb', line 113

def size
  return raw["size"] if raw["size"]
  return @size if @size
  q = load_queue()
  @size = q.size
  @size
end

#total_messagesObject



121
122
123
124
125
126
127
# File 'lib/iron_mq/queues.rb', line 121

def total_messages
  return raw["total_messages"] if raw["total_messages"]
  return @total_messages if @total_messages
  q = load_queue()
  @total_messages = q.total_messages
  @total_messages
end