Class RightAws::Sqs::Queue
In: lib/sqs/right_sqs.rb
Parent: Object
RuntimeError AwsError AwsNoChange RightAWSParser RightErrorResponseParser RightHttp2xxParser AcfInterface SqsInterface SqsGen2Interface S3Interface Ec2 SdbInterface RightAwsBase ActiveSdbConnect ActiveSdb SqsGen2 S3 S3Generator Sqs RightDummyParser AWSErrorHandler AwsBenchmarkingBlock AwsUtils RightSaxParserCallback lib/sqs/right_sqs_interface.rb lib/sqs/right_sqs_gen2.rb lib/s3/right_s3.rb lib/acf/right_acf_interface.rb lib/sqs/right_sqs_gen2_interface.rb lib/sqs/right_sqs.rb lib/sdb/right_sdb_interface.rb lib/sdb/active_sdb.rb lib/ec2/right_ec2.rb lib/s3/right_s3_interface.rb lib/awsbase/right_awsbase.rb RightAwsBaseInterface VERSION RightAws dot/m_13_0.png

Methods

Attributes

name  [R] 
sqs  [R] 
url  [R] 

Public Class methods

Returns Queue instance by queue name. If the queue does not exist at Amazon SQS and create is true, the method creates it.

 RightAws::Sqs::Queue.create(sqs, 'my_awesome_queue') #=> #<RightAws::Sqs::Queue:0xb7b626e4 ... >

[Source]

     # File lib/sqs/right_sqs.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::Sqs::Queue.new(sqs, 'my_awesome_queue')

[Source]

     # File lib/sqs/right_sqs.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

Public Instance methods

Clears queue. Deletes only the visible messages unless force is true.

 queue.clear(true) #=> true

P.S. when force==true the queue deletes then creates again. This is the quickest method to clear a big queue or a queue with ‘locked’ messages. All queue attributes are restored. But there is no way to restore grantees’ permissions to this queue. If you have no grantees except ‘root’ then you have no problems. Otherwise, it‘s better to use queue.clear(false).

PS This function is no longer supported. Amazon has changed the SQS semantics to require at least 60 seconds between queue deletion and creation. Hence this method will fail with an exception.

[Source]

     # File lib/sqs/right_sqs.rb, line 141
141:       def clear(force=false)
142: ##        if force
143: ##          @sqs.interface.force_clear_queue(@url)
144: ##        else
145:           @sqs.interface.clear_queue(@url)
146: ##        end
147:       end

Deletes queue. Queue must be empty or force must be set to true. Returns true.

 queue.delete(true) #=> true

[Source]

     # File lib/sqs/right_sqs.rb, line 155
155:       def delete(force=false)
156:         @sqs.interface.delete_queue(@url, force)
157:       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’) #=> ‘100‘

[Source]

     # File lib/sqs/right_sqs.rb, line 254
254:       def get_attribute(attribute='All')
255:         attributes = @sqs.interface.get_queue_attributes(@url, attribute)
256:         attribute=='All' ? attributes : attributes[attribute]
257:       end

Retrieves a list of grantees. Returns an array of Grantee instances if the grantee_email_address is unset. Otherwise returns a Grantee instance that points to grantee_email_address or nil.

 grantees = queue.grantees #=> [#<RightAws::Sqs::Grantee:0xb7bf0888 ... >, ...]
  ...
 grantee  = queue.grantees('cool_guy@email.address') #=> nil | #<RightAws::Sqs::Grantee:0xb7bf0888 ... >

[Source]

     # File lib/sqs/right_sqs.rb, line 267
267:       def grantees(grantee_email_address=nil, permission = nil)
268:         hash = @sqs.interface.list_grants(@url, grantee_email_address, permission)
269:         grantees = []
270:         hash.each do |key, value|
271:           grantees << Grantee.new(self, grantee_email_address, key, value[:name], value[:perms])
272:         end
273:         if grantee_email_address
274:           grantees.blank? ? nil : grantees.shift
275:         else
276:           grantees
277:         end
278:       end

Peeks message body.

 queue.peek #=> #<RightAws::Sqs::Message:0xb7bf0884 ... >

[Source]

     # File lib/sqs/right_sqs.rb, line 197
197:       def peek(message_id)
198:         entry = @sqs.interface.peek_message(@url, message_id)
199:         msg   = Message.new(self, entry[:id], entry[:body])
200:         msg.received_at = Time.now 
201:         msg
202:       end

Pops (and deletes) first accessible message from queue. Returns Message instance or nil it the queue is empty.

 queue.pop #=> #<RightAws::Sqs::Message:0xb7bf0884 ... >

[Source]

     # File lib/sqs/right_sqs.rb, line 209
209:       def pop
210:         msg = receive
211:         msg.delete if msg
212:         msg
213:       end
push(message)

Alias for send_message

Retrieves first accessible message from queue. Returns Message instance or nil it the queue is empty.

 queue.receive #=> #<RightAws::Sqs::Message:0xb7bf0884 ... >

[Source]

     # File lib/sqs/right_sqs.rb, line 188
188:       def receive(visibility=nil)
189:         list = receive_messages(1, visibility)
190:         list.empty? ? nil : list[0]
191:       end

Retrieves several messages from queue. Returns an array of Message instances.

 queue.receive_messages(2,10) #=> array of messages

[Source]

     # File lib/sqs/right_sqs.rb, line 174
174:       def receive_messages(number_of_messages=1, visibility=nil)
175:         list = @sqs.interface.receive_messages(@url, number_of_messages, visibility)
176:         list.map! do |entry|
177:           msg             = Message.new(self, entry[:id], entry[:body], visibility)
178:           msg.received_at = Time.now 
179:           msg
180:         end
181:       end

Sends new message to queue. Returns new Message instance that has been sent to queue.

[Source]

     # File lib/sqs/right_sqs.rb, line 161
161:       def send_message(message)
162:         message = message.to_s
163:         msg     = Message.new(self, @sqs.interface.send_message(@url, message), message)
164:         msg.sent_at = Time.now
165:         msg
166:       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.

queue.set_attribute(‘VisibilityTimeout’, ‘100’) #=> ‘100’ queue.get_attribute(‘VisibilityTimeout’) #=> ‘100‘

[Source]

     # File lib/sqs/right_sqs.rb, line 243
243:       def set_attribute(attribute, value)
244:         @sqs.interface.set_queue_attributes(@url, attribute, value)
245:         value
246:       end

Retrieves queue size.

 queue.size #=> 1

[Source]

     # File lib/sqs/right_sqs.rb, line 123
123:       def size
124:         @sqs.interface.get_queue_length(@url)
125:       end

Retrieves VisibilityTimeout value for the queue. Returns new timeout value.

 queue.visibility #=> 30

[Source]

     # File lib/sqs/right_sqs.rb, line 220
220:       def visibility
221:         @sqs.interface.get_visibility_timeout(@url)
222:       end

Sets new VisibilityTimeout for the queue. Returns new timeout value.

 queue.visibility #=> 30
 queue.visibility = 33
 queue.visibility #=> 33

[Source]

     # File lib/sqs/right_sqs.rb, line 231
231:       def visibility=(visibility_timeout)
232:         @sqs.interface.set_visibility_timeout(@url, visibility_timeout)
233:         visibility_timeout
234:       end

[Validate]