Class: RightAws::SqsGen2::Queue
- Inherits:
-
Object
- Object
- RightAws::SqsGen2::Queue
- Defined in:
- lib/sqs/right_sqs_gen2.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
-
#add_permissions(label, grantees, actions) ⇒ Object
Add permission to the queue.
-
#clear ⇒ Object
Clears queue, deleting only the visible messages.
-
#delete(force = false) ⇒ Object
Deletes queue.
-
#get_attribute(attribute = 'All') ⇒ Object
Retrieves queue attributes.
-
#get_attributes(*attributes) ⇒ Object
Retrieves queue attributes.
-
#get_permissions ⇒ Object
Get current permissions set.
-
#initialize(sqs, url_or_name) ⇒ Queue
constructor
Creates new Queue instance.
-
#pop(attributes = nil) ⇒ Object
Pops (and deletes) first accessible message from queue.
-
#receive(visibility = nil, attributes = nil) ⇒ Object
Retrieves first accessible message from queue.
-
#receive_messages(number_of_messages = 1, visibility = nil, attributes = nil) ⇒ Object
Retrieves several messages from queue.
-
#remove_permissions(label) ⇒ Object
Revoke any permissions in the queue policy that matches the
label
parameter. -
#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
112 113 114 115 116 |
# File 'lib/sqs/right_sqs_gen2.rb', line 112 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.
96 97 98 |
# File 'lib/sqs/right_sqs_gen2.rb', line 96 def name @name end |
#sqs ⇒ Object (readonly)
Returns the value of attribute sqs.
96 97 98 |
# File 'lib/sqs/right_sqs_gen2.rb', line 96 def sqs @sqs end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
96 97 98 |
# File 'lib/sqs/right_sqs_gen2.rb', line 96 def url @url end |
Class Method Details
.create(sqs, url_or_name, create = true, visibility = nil) ⇒ Object
103 104 105 |
# File 'lib/sqs/right_sqs_gen2.rb', line 103 def self.create(sqs, url_or_name, create=true, visibility=nil) sqs.queue(url_or_name, create, visibility) end |
Instance Method Details
#add_permissions(label, grantees, actions) ⇒ Object
Add permission to the queue.
queue.('testLabel',['125074342641', '125074342642'],
['SendMessage','SendMessage','ReceiveMessage']) #=> true
277 278 279 |
# File 'lib/sqs/right_sqs_gen2.rb', line 277 def (label, grantees, actions) @sqs.interface.(@url, label, grantees, actions) end |
#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
137 138 139 |
# File 'lib/sqs/right_sqs_gen2.rb', line 137 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
148 149 150 |
# File 'lib/sqs/right_sqs_gen2.rb', line 148 def delete(force=false) @sqs.interface.delete_queue(@url) end |
#get_attribute(attribute = 'All') ⇒ Object
Retrieves queue attributes. If the name of attribute is set, returns its value. Otherwise, returns a hash of attributes.
queue.get_attribute(‘VisibilityTimeout’) #=> “VisibilityTimeout”=>“45”
P.S. This guy is deprecated. Use get_attributes
instead.
250 251 252 253 |
# File 'lib/sqs/right_sqs_gen2.rb', line 250 def get_attribute(attribute='All') attributes = get_attributes(attribute) attribute=='All' ? attributes : attributes[attribute] end |
#get_attributes(*attributes) ⇒ Object
Retrieves queue attributes.
queue.get_attributes #=>
{"ApproximateNumberOfMessages" => "0",
"LastModifiedTimestamp" => "1240946032",
"CreatedTimestamp" => "1240816887",
"VisibilityTimeout" => "30",
"Policy" => "{"Version":"2008-10-17","Id":...}"}
queue.get_attributes("LastModifiedTimestamp", "VisibilityTimeout") #=>
{"LastModifiedTimestamp" => "1240946032",
"VisibilityTimeout" => "30"}
268 269 270 |
# File 'lib/sqs/right_sqs_gen2.rb', line 268 def get_attributes(*attributes) @sqs.interface.get_queue_attributes(@url, attributes) end |
#get_permissions ⇒ Object
Get current permissions set. The set is JSON packed.
sqs. #=>
'{"Version":"2008-10-17","Id":"/826693181925/kd-test-gen-2_5/SQSDefaultPolicy",
"Statement":[{"Sid":"kd-perm-04","Effect":"Allow","Principal":{"AWS":"100000000001",
"AWS":"100000000001","AWS":"100000000002"},"Action":["SQS:SendMessage","SQS:DeleteMessage",
"SQS:ReceiveMessage"],"Resource":"/826693181925/kd-test-gen-2_5"},{"Sid":"kd-perm-03",
"Effect":"Allow","Principal":{"AWS":"648772224137"},"Action":"SQS:SendMessage",
"Resource":"/826693181925/kd-test-gen-2_5"}]}'
299 300 301 |
# File 'lib/sqs/right_sqs_gen2.rb', line 299 def get_attributes('Policy')['Policy'] end |
#pop(attributes = nil) ⇒ Object
Pops (and deletes) first accessible message from queue. Returns Message instance or nil
if the queue is empty.
queue.pop #=> #<RightAws::SqsGen2::Message:0xb7bf0884 ... >
# pop a message with custom attributes
m = queue.pop(['SenderId', 'SentTimestamp']) #=> #<RightAws::SqsGen2::Message:0xb7bf1884 ... >
m.attributes #=> {"SentTimestamp"=>"1240991906937", "SenderId"=>"800000000005"}
198 199 200 201 202 203 204 205 206 |
# File 'lib/sqs/right_sqs_gen2.rb', line 198 def pop(attributes=nil) list = @sqs.interface.(@url, 1, attributes) return nil if list.empty? entry = list[0] msg = Message.new(self, entry['MessageId'], entry['ReceiptHandle'], entry['Body'], visibility, entry['Attributes']) msg.received_at = Time.now msg.receive_checksum = entry['MD5OfBody'] msg end |
#receive(visibility = nil, attributes = nil) ⇒ Object
Retrieves first accessible message from queue. Returns Message instance or nil
it the queue is empty.
queue.receive #=> #<RightAws::SqsGen2::Message:0xb7bf0884 ... >
184 185 186 187 |
# File 'lib/sqs/right_sqs_gen2.rb', line 184 def receive(visibility=nil, attributes=nil) list = (1, visibility, attributes) list.empty? ? nil : list[0] end |
#receive_messages(number_of_messages = 1, visibility = nil, attributes = nil) ⇒ Object
Retrieves several messages from queue. Returns an array of Message instances.
queue.(2,10) #=> array of messages
169 170 171 172 173 174 175 176 177 |
# File 'lib/sqs/right_sqs_gen2.rb', line 169 def (=1, visibility=nil, attributes=nil) list = @sqs.interface.(@url, , visibility, attributes) list.map! do |entry| msg = Message.new(self, entry['MessageId'], entry['ReceiptHandle'], entry['Body'], visibility, entry['Attributes']) msg.received_at = Time.now msg.receive_checksum = entry['MD5OfBody'] msg end end |
#remove_permissions(label) ⇒ Object
Revoke any permissions in the queue policy that matches the label
parameter.
sqs.('testLabel') # => true
285 286 287 |
# File 'lib/sqs/right_sqs_gen2.rb', line 285 def (label) @sqs.interface.(@url, label) end |
#send_message(message) ⇒ Object Also known as: push
Sends new message to queue. Returns new Message instance that has been sent to queue.
154 155 156 157 158 159 160 161 |
# File 'lib/sqs/right_sqs_gen2.rb', line 154 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’
239 240 241 242 |
# File 'lib/sqs/right_sqs_gen2.rb', line 239 def set_attribute(attribute, value) @sqs.interface.set_queue_attributes(@url, attribute, value) value end |
#size ⇒ Object
Retrieves queue size.
queue.size #=> 1
122 123 124 |
# File 'lib/sqs/right_sqs_gen2.rb', line 122 def size @sqs.interface.get_queue_length(@url) end |
#visibility ⇒ Object
Retrieves VisibilityTimeout
value for the queue. Returns new timeout value.
queue.visibility #=> 30
213 214 215 |
# File 'lib/sqs/right_sqs_gen2.rb', line 213 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
224 225 226 227 |
# File 'lib/sqs/right_sqs_gen2.rb', line 224 def visibility=(visibility_timeout) @sqs.interface.set_queue_attributes(@url, 'VisibilityTimeout', visibility_timeout) visibility_timeout end |