Class: Aws::Sqs::Queue
- Inherits:
-
Object
- Object
- Aws::Sqs::Queue
- Defined in:
- lib/sqs/sqs.rb
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 ⇒ Object
Clears queue, deleting only the visible messages.
-
#delete(force = false) ⇒ Object
Deletes queue.
-
#get_attribute(attribute = 'All') ⇒ Object
Retrieves queue attributes.
-
#initialize(sqs, url_or_name) ⇒ Queue
constructor
Creates new Queue instance.
-
#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
122 123 124 125 126 127 |
# File 'lib/sqs/sqs.rb', line 122 def initialize(sqs, url_or_name) @sqs = sqs @url = @sqs.interface.queue_url_by_name(url_or_name) raise ResourceNotFoundError, "Queue '#{url_or_name}' not found." unless @url @name = @sqs.interface.queue_name_by_url(@url) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
106 107 108 |
# File 'lib/sqs/sqs.rb', line 106 def name @name end |
#sqs ⇒ Object (readonly)
Returns the value of attribute sqs.
106 107 108 |
# File 'lib/sqs/sqs.rb', line 106 def sqs @sqs end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
106 107 108 |
# File 'lib/sqs/sqs.rb', line 106 def url @url end |
Class Method Details
.create(sqs, url_or_name, create = true, visibility = nil) ⇒ Object
113 114 115 |
# File 'lib/sqs/sqs.rb', line 113 def self.create(sqs, url_or_name, create=true, visibility=nil) sqs.queue(url_or_name, create, visibility) end |
Instance Method Details
#clear ⇒ Object
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
148 149 150 |
# File 'lib/sqs/sqs.rb', line 148 def clear() @sqs.interface.clear_queue(@url) end |
#delete(force = false) ⇒ Object
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
159 160 161 |
# File 'lib/sqs/sqs.rb', line 159 def delete(force=false) @sqs.interface.delete_queue(@url) 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’) #=> “VisibilityTimeout”=>“45”
260 261 262 263 |
# File 'lib/sqs/sqs.rb', line 260 def get_attribute(attribute='All') attributes = @sqs.interface.get_queue_attributes(@url, attribute) attribute=='All' ? attributes : attributes[attribute] end |
#pop ⇒ Object
Pops (and deletes) first accessible message from queue. Returns Message instance or nil
if the queue is empty.
queue.pop #=> #<Aws::Sqs::Message:0xb7bf0884 ... >
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/sqs/sqs.rb', line 207 def pop list = @sqs.interface.(@url, 1) return nil if list.empty? entry = list[0] msg = Message.new(self, entry['MessageId'], entry['ReceiptHandle'], entry['Body'], visibility) msg.received_at = Time.now msg.receive_checksum = entry['MD5OfBody'] msg end |
#receive(visibility = nil) ⇒ Object
Retrieves first accessible message from queue. Returns Message instance or nil
it the queue is empty.
queue.receive #=> #<Aws::Sqs::Message:0xb7bf0884 ... >
197 198 199 200 |
# File 'lib/sqs/sqs.rb', line 197 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
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/sqs/sqs.rb', line 181 def (=1, visibility=nil) list = @sqs.interface.(@url, , visibility) list.map! do |entry| msg = Message.new(self, entry['MessageId'], entry['ReceiptHandle'], entry['Body'], visibility) msg.received_at = Time.now msg.receive_checksum = entry['MD5OfBody'] 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.
165 166 167 168 169 170 171 172 |
# File 'lib/sqs/sqs.rb', line 165 def () = .to_s res = @sqs.interface.(@url, ) msg = Message.new(self, res['MessageId'], nil, ) msg.send_checksum = res['MD5OfMessageBody'] 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. 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’
249 250 251 252 |
# File 'lib/sqs/sqs.rb', line 249 def set_attribute(attribute, value) @sqs.interface.set_queue_attributes(@url, attribute, value) value end |
#size ⇒ Object
Retrieves queue size.
queue.size #=> 1
133 134 135 |
# File 'lib/sqs/sqs.rb', line 133 def size @sqs.interface.get_queue_length(@url) end |
#visibility ⇒ Object
Retrieves VisibilityTimeout
value for the queue. Returns new timeout value.
queue.visibility #=> 30
223 224 225 |
# File 'lib/sqs/sqs.rb', line 223 def visibility @sqs.interface.get_queue_attributes(@url, 'VisibilityTimeout')['VisibilityTimeout'] end |
#visibility=(visibility_timeout) ⇒ Object
Sets new VisibilityTimeout
for the queue. Returns new timeout value.
queue.visibility #=> 30
queue.visibility = 33
queue.visibility #=> 33
234 235 236 237 |
# File 'lib/sqs/sqs.rb', line 234 def visibility=(visibility_timeout) @sqs.interface.set_queue_attributes(@url, 'VisibilityTimeout', visibility_timeout) visibility_timeout end |