Class: RightAws::Sqs::Queue
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sqs ⇒ Object
readonly
Returns the value of attribute sqs.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
-
.create(sqs, url_or_name, create = true, visibility = nil) ⇒ Object
Returns Queue instance by queue name.
Instance Method Summary collapse
-
#clear(force = false) ⇒ Object
Clears queue.
-
#delete(force = false) ⇒ Object
Deletes queue.
-
#get_attribute(attribute = 'All') ⇒ Object
Retrieves queue attributes.
-
#grantees(grantee_email_address = nil, permission = nil) ⇒ Object
Retrieves a list of grantees.
-
#initialize(sqs, url_or_name) ⇒ Queue
constructor
Creates new Queue instance.
-
#peek(message_id) ⇒ Object
Peeks message body.
-
#pop ⇒ Object
Pops (and deletes) first accessible message from queue.
-
#receive(visibility = nil) ⇒ Object
Retrieves first accessible message from queue.
-
#receive_messages(number_of_messages = 1, visibility = nil) ⇒ Object
Retrieves several messages from queue.
-
#send_message(message) ⇒ Object
(also: #push)
Sends new message to queue.
-
#set_attribute(attribute, value) ⇒ Object
Sets new queue attribute value.
-
#size ⇒ Object
Retrieves queue size.
-
#visibility ⇒ Object
Retrieves
VisibilityTimeout
value for the queue. -
#visibility=(visibility_timeout) ⇒ Object
Sets new
VisibilityTimeout
for the queue.
Constructor Details
#initialize(sqs, url_or_name) ⇒ Queue
105 106 107 108 109 |
# File 'lib/sqs/right_sqs.rb', line 105 def initialize(sqs, url_or_name) @sqs = sqs @url = @sqs.interface.queue_url_by_name(url_or_name) @name = @sqs.interface.queue_name_by_url(@url) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
89 90 91 |
# File 'lib/sqs/right_sqs.rb', line 89 def name @name end |
#sqs ⇒ Object (readonly)
Returns the value of attribute sqs.
89 90 91 |
# File 'lib/sqs/right_sqs.rb', line 89 def sqs @sqs end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
89 90 91 |
# File 'lib/sqs/right_sqs.rb', line 89 def url @url end |
Class Method Details
.create(sqs, url_or_name, create = true, visibility = nil) ⇒ Object
96 97 98 |
# File 'lib/sqs/right_sqs.rb', line 96 def self.create(sqs, url_or_name, create=true, visibility=nil) sqs.queue(url_or_name, create, visibility) end |
Instance Method Details
#clear(force = false) ⇒ Object
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.
133 134 135 136 137 138 139 |
# File 'lib/sqs/right_sqs.rb', line 133 def clear(force=false) ## if force ## @sqs.interface.force_clear_queue(@url) ## else @sqs.interface.clear_queue(@url) ## end end |
#delete(force = false) ⇒ Object
Deletes queue. Queue must be empty or force
must be set to true
. Returns true
.
queue.delete(true) #=> true
147 148 149 |
# File 'lib/sqs/right_sqs.rb', line 147 def delete(force=false) @sqs.interface.delete_queue(@url, force) end |
#get_attribute(attribute = 'All') ⇒ Object
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’
246 247 248 249 |
# File 'lib/sqs/right_sqs.rb', line 246 def get_attribute(attribute='All') attributes = @sqs.interface.get_queue_attributes(@url, attribute) attribute=='All' ? attributes : attributes[attribute] end |
#grantees(grantee_email_address = nil, permission = nil) ⇒ Object
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('[email protected]') #=> nil | #<RightAws::Sqs::Grantee:0xb7bf0888 ... >
259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/sqs/right_sqs.rb', line 259 def grantees(grantee_email_address=nil, = nil) hash = @sqs.interface.list_grants(@url, grantee_email_address, ) grantees = [] hash.each do |key, value| grantees << Grantee.new(self, grantee_email_address, key, value[:name], value[:perms]) end if grantee_email_address grantees.blank? ? nil : grantees.shift else grantees end end |
#peek(message_id) ⇒ Object
Peeks message body.
queue.peek #=> #<RightAws::Sqs::Message:0xb7bf0884 ... >
189 190 191 192 193 194 |
# File 'lib/sqs/right_sqs.rb', line 189 def peek() entry = @sqs.interface.(@url, ) msg = Message.new(self, entry[:id], entry[:body]) msg.received_at = Time.now msg end |
#pop ⇒ Object
Pops (and deletes) first accessible message from queue. Returns Message instance or nil
it the queue is empty.
queue.pop #=> #<RightAws::Sqs::Message:0xb7bf0884 ... >
201 202 203 204 205 |
# File 'lib/sqs/right_sqs.rb', line 201 def pop msg = receive msg.delete if msg msg end |
#receive(visibility = nil) ⇒ Object
Retrieves first accessible message from queue. Returns Message instance or nil
it the queue is empty.
queue.receive #=> #<RightAws::Sqs::Message:0xb7bf0884 ... >
180 181 182 183 |
# File 'lib/sqs/right_sqs.rb', line 180 def receive(visibility=nil) list = (1, visibility) list.empty? ? nil : list[0] end |
#receive_messages(number_of_messages = 1, visibility = nil) ⇒ Object
Retrieves several messages from queue. Returns an array of Message instances.
queue.(2,10) #=> array of messages
166 167 168 169 170 171 172 173 |
# File 'lib/sqs/right_sqs.rb', line 166 def (=1, visibility=nil) list = @sqs.interface.(@url, , visibility) list.map! do |entry| msg = Message.new(self, entry[:id], entry[:body], visibility) msg.received_at = Time.now msg end end |
#send_message(message) ⇒ Object Also known as: push
Sends new message to queue. Returns new Message instance that has been sent to queue.
153 154 155 156 157 158 |
# File 'lib/sqs/right_sqs.rb', line 153 def () = .to_s msg = Message.new(self, @sqs.interface.(@url, ), ) msg.sent_at = Time.now msg end |
#set_attribute(attribute, value) ⇒ Object
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’
235 236 237 238 |
# File 'lib/sqs/right_sqs.rb', line 235 def set_attribute(attribute, value) @sqs.interface.set_queue_attributes(@url, attribute, value) value end |
#size ⇒ Object
Retrieves queue size.
queue.size #=> 1
115 116 117 |
# File 'lib/sqs/right_sqs.rb', line 115 def size @sqs.interface.get_queue_length(@url) end |
#visibility ⇒ Object
Retrieves VisibilityTimeout
value for the queue. Returns new timeout value.
queue.visibility #=> 30
212 213 214 |
# File 'lib/sqs/right_sqs.rb', line 212 def visibility @sqs.interface.get_visibility_timeout(@url) end |
#visibility=(visibility_timeout) ⇒ Object
Sets new VisibilityTimeout
for the queue. Returns new timeout value.
queue.visibility #=> 30
queue.visibility = 33
queue.visibility #=> 33
223 224 225 226 |
# File 'lib/sqs/right_sqs.rb', line 223 def visibility=(visibility_timeout) @sqs.interface.set_visibility_timeout(@url, visibility_timeout) visibility_timeout end |