| Class | RightAws::SqsGen2::Queue |
| In: |
lib/sqs/right_sqs_gen2.rb
|
| Parent: | Object |
| name | [R] | |
| sqs | [R] | |
| url | [R] |
Returns Queue instance by queue name. If the queue does not exist at Amazon SQS and create is true, the method creates it.
RightAws::SqsGen2::Queue.create(sqs, 'my_awesome_queue') #=> #<RightAws::SqsGen2::Queue:0xb7b626e4 ... >
# File lib/sqs/right_sqs_gen2.rb, line 104
104: def self.create(sqs, url_or_name, create=true, visibility=nil)
105: sqs.queue(url_or_name, create, visibility)
106: end
Creates new Queue instance. Does not create a queue at Amazon.
queue = RightAws::SqsGen2::Queue.new(sqs, 'my_awesome_queue')
# File lib/sqs/right_sqs_gen2.rb, line 113
113: def initialize(sqs, url_or_name)
114: @sqs = sqs
115: @url = @sqs.interface.queue_url_by_name(url_or_name)
116: @name = @sqs.interface.queue_name_by_url(@url)
117: end
Clears queue, deleting only the visible messages. Any message within its visibility timeout will not be deleted, and will re-appear in the queue in the future when the timeout expires.
To delete all messages in a queue and eliminate the chance of any messages re-appearing in the future, it‘s best to delete the queue and re-create it as a new queue. Note that doing this will take at least 60 s since SQS does not allow re-creation of a queue within this interval.
queue.clear() #=> true
# File lib/sqs/right_sqs_gen2.rb, line 138
138: def clear()
139: @sqs.interface.clear_queue(@url)
140: end
Deletes queue. Any messages in the queue will be permanently lost. Returns true.
NB: Use with caution; severe data loss is possible!
queue.delete(true) #=> true
# File lib/sqs/right_sqs_gen2.rb, line 149
149: def delete(force=false)
150: @sqs.interface.delete_queue(@url)
151: end
Retrieves queue attributes. At this moment Amazon supports VisibilityTimeout and ApproximateNumberOfMessages only. If the name of attribute is set, returns its value. Otherwise, returns a hash of attributes.
queue.get_attribute(‘VisibilityTimeout’) #=> {"VisibilityTimeout"=>"45"}
# File lib/sqs/right_sqs_gen2.rb, line 249
249: def get_attribute(attribute='All')
250: attributes = @sqs.interface.get_queue_attributes(@url, attribute)
251: attribute=='All' ? attributes : attributes[attribute]
252: end
Pops (and deletes) first accessible message from queue. Returns Message instance or nil if the queue is empty.
queue.pop #=> #<RightAws::SqsGen2::Message:0xb7bf0884 ... >
# File lib/sqs/right_sqs_gen2.rb, line 196
196: def pop
197: list = @sqs.interface.pop_messages(@url, 1)
198: return nil if list.empty?
199: entry = list[0]
200: msg = Message.new(self, entry['MessageId'], entry['ReceiptHandle'],
201: entry['Body'], visibility)
202: msg.received_at = Time.now
203: msg.receive_checksum = entry['MD5OfBody']
204: msg
205: end
Retrieves first accessible message from queue. Returns Message instance or nil it the queue is empty.
queue.receive #=> #<RightAws::SqsGen2::Message:0xb7bf0884 ... >
# File lib/sqs/right_sqs_gen2.rb, line 186
186: def receive(visibility=nil)
187: list = receive_messages(1, visibility)
188: list.empty? ? nil : list[0]
189: end
Retrieves several messages from queue. Returns an array of Message instances.
queue.receive_messages(2,10) #=> array of messages
# File lib/sqs/right_sqs_gen2.rb, line 170
170: def receive_messages(number_of_messages=1, visibility=nil)
171: list = @sqs.interface.receive_message(@url, number_of_messages, visibility)
172: list.map! do |entry|
173: msg = Message.new(self, entry['MessageId'], entry['ReceiptHandle'],
174: entry['Body'], visibility)
175: msg.received_at = Time.now
176: msg.receive_checksum = entry['MD5OfBody']
177: msg
178: end
179: end
Sends new message to queue. Returns new Message instance that has been sent to queue.
# File lib/sqs/right_sqs_gen2.rb, line 155
155: def send_message(message)
156: message = message.to_s
157: res = @sqs.interface.send_message(@url, message)
158: msg = Message.new(self, res['MessageId'], nil, message)
159: msg.send_checksum = res['MD5OfMessageBody']
160: msg.sent_at = Time.now
161: msg
162: end
Sets new queue attribute value. Not all attributes may be changed: ApproximateNumberOfMessages (for example) is a read only attribute. Returns a value to be assigned to attribute. Currently, ‘VisibilityTimeout’ is the only settable queue attribute. Attempting to set non-existent attributes generates an indignant exception.
queue.set_attribute(‘VisibilityTimeout’, ‘100’) #=> ‘100’ queue.get_attribute(‘VisibilityTimeout’) #=> ‘100‘
# File lib/sqs/right_sqs_gen2.rb, line 238
238: def set_attribute(attribute, value)
239: @sqs.interface.set_queue_attributes(@url, attribute, value)
240: value
241: end
Sets new VisibilityTimeout for the queue. Returns new timeout value.
queue.visibility #=> 30 queue.visibility = 33 queue.visibility #=> 33
# File lib/sqs/right_sqs_gen2.rb, line 223
223: def visibility=(visibility_timeout)
224: @sqs.interface.set_queue_attributes(@url, 'VisibilityTimeout', visibility_timeout)
225: visibility_timeout
226: end