qoobaa-aws-sqs 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,111 @@
1
+ # If ActiveSupport is loaded, then great - use it. But we don't
2
+ # want a dependency on it, so if it's not present, define the few
3
+ # extensions that we want to use...
4
+ unless defined? ActiveSupport::CoreExtensions
5
+ # These are ActiveSupport-;like extensions to do a few handy things in the gems
6
+ # Derived from ActiveSupport, so the AS copyright notice applies:
7
+ #
8
+ #
9
+ #
10
+ # Copyright (c) 2005 David Heinemeier Hansson
11
+ #
12
+ # Permission is hereby granted, free of charge, to any person obtaining
13
+ # a copy of this software and associated documentation files (the
14
+ # "Software"), to deal in the Software without restriction, including
15
+ # without limitation the rights to use, copy, modify, merge, publish,
16
+ # distribute, sublicense, and/or sell copies of the Software, and to
17
+ # permit persons to whom the Software is furnished to do so, subject to
18
+ # the following conditions:
19
+ #
20
+ # The above copyright notice and this permission notice shall be
21
+ # included in all copies or substantial portions of the Software.
22
+ #
23
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
+ #++
31
+ #
32
+ #
33
+ class String #:nodoc:
34
+
35
+ # Constantize tries to find a declared constant with the name specified
36
+ # in the string. It raises a NameError when the name is not in CamelCase
37
+ # or is not initialized.
38
+ #
39
+ # Examples
40
+ # "Module".constantize #=> Module
41
+ # "Class".constantize #=> Class
42
+ def constantize()
43
+ unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
44
+ raise NameError, "#{self.inspect} is not a valid constant name!"
45
+ end
46
+
47
+ Object.module_eval("::#{$1}", __FILE__, __LINE__)
48
+ end
49
+
50
+ end
51
+
52
+
53
+ class Object #:nodoc:
54
+ # "", " ", nil, [], and {} are blank
55
+ def blank?
56
+ if respond_to?(:empty?) && respond_to?(:strip)
57
+ empty? or strip.empty?
58
+ elsif respond_to?(:empty?)
59
+ empty?
60
+ else
61
+ !self
62
+ end
63
+ end
64
+ end
65
+
66
+ class NilClass #:nodoc:
67
+ def blank?
68
+ true
69
+ end
70
+ end
71
+
72
+ class FalseClass #:nodoc:
73
+ def blank?
74
+ true
75
+ end
76
+ end
77
+
78
+ class TrueClass #:nodoc:
79
+ def blank?
80
+ false
81
+ end
82
+ end
83
+
84
+ class Array #:nodoc:
85
+ alias_method :blank?, :empty?
86
+ end
87
+
88
+ class Hash #:nodoc:
89
+ alias_method :blank?, :empty?
90
+
91
+ # Return a new hash with all keys converted to symbols.
92
+ def symbolize_keys
93
+ inject({}) do |options, (key, value)|
94
+ options[key.to_sym] = value
95
+ options
96
+ end
97
+ end
98
+ end
99
+
100
+ class String #:nodoc:
101
+ def blank?
102
+ empty? || strip.empty?
103
+ end
104
+ end
105
+
106
+ class Numeric #:nodoc:
107
+ def blank?
108
+ false
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,283 @@
1
+ #
2
+ # Copyright (c) 2008 RightScale Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #
23
+
24
+ module Aws
25
+
26
+ #
27
+ # Aws::Sqs -- RightScale's Amazon SQS interface, API version
28
+ # 2008-01-01 and later.
29
+ # The Aws::Sqs class provides a complete interface to the second generation of Amazon's Simple
30
+ # Queue Service.
31
+ # For explanations of the semantics
32
+ # of each call, please refer to Amazon's documentation at
33
+ # http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=31
34
+ #
35
+ #
36
+ # Aws::Sqs is built atop Aws::SqsInterface, a lower-level
37
+ # procedural API that may be appropriate for certain programs.
38
+ #
39
+ # Error handling: all operations raise an Aws::AwsError in case
40
+ # of problems. Note that transient errors are automatically retried.
41
+ #
42
+ # sqs = Aws::Sqs.new(aws_access_key_id, aws_secret_access_key)
43
+ # queue1 = sqs.queue('my_awesome_queue')
44
+ # ...
45
+ # queue2 = Aws::Sqs::Queue.create(sqs, 'my_cool_queue', true)
46
+ # puts queue2.size
47
+ # ...
48
+ # message1 = queue2.receive
49
+ # message1.visibility = 0
50
+ # puts message1
51
+ # ...
52
+ # queue2.clear(true)
53
+ # queue2.send_message('Ola-la!')
54
+ # message2 = queue2.pop
55
+ # ...
56
+ #
57
+ # NB: Second-generation SQS has eliminated the entire access grant mechanism present in Gen 1.
58
+ #
59
+ # Params is a hash:
60
+ #
61
+ # {:server => 'queue.amazonaws.com' # Amazon service host: 'queue.amazonaws.com' (default)
62
+ # :port => 443 # Amazon service port: 80 or 443 (default)
63
+ # :multi_thread => true|false # Multi-threaded (connection per each thread): true or false (default)
64
+ # :signature_version => '0' # The signature version : '0' or '1'(default)
65
+ # :logger => Logger Object} # Logger instance: logs to STDOUT if omitted }
66
+ class Sqs
67
+ attr_reader :interface
68
+
69
+ def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
70
+ @interface = SqsInterface.new(aws_access_key_id, aws_secret_access_key, params)
71
+ end
72
+
73
+ # Retrieves a list of queues.
74
+ # Returns an +array+ of +Queue+ instances.
75
+ #
76
+ # Aws::Sqs.queues #=> array of queues
77
+ #
78
+ def queues(prefix=nil)
79
+ @interface.list_queues(prefix).map do |url|
80
+ Queue.new(self, url)
81
+ end
82
+ end
83
+
84
+ # Returns Queue instance by queue name.
85
+ # If the queue does not exist at Amazon SQS and +create+ is true, the method creates it.
86
+ #
87
+ # Aws::Sqs.queue('my_awesome_queue') #=> #<Aws::Sqs::Queue:0xb7b626e4 ... >
88
+ #
89
+ def queue(queue_name, create=true, visibility=nil)
90
+ url = @interface.queue_url_by_name(queue_name)
91
+ url = (create ? @interface.create_queue(queue_name, visibility) : nil) unless url
92
+ url ? Queue.new(self, url) : nil
93
+ end
94
+
95
+
96
+ class Queue
97
+ attr_reader :name, :url, :sqs
98
+
99
+ # Returns Queue instance by queue name.
100
+ # If the queue does not exist at Amazon SQS and +create+ is true, the method creates it.
101
+ #
102
+ # Aws::Sqs::Queue.create(sqs, 'my_awesome_queue') #=> #<Aws::Sqs::Queue:0xb7b626e4 ... >
103
+ #
104
+ def self.create(sqs, url_or_name, create=true, visibility=nil)
105
+ sqs.queue(url_or_name, create, visibility)
106
+ end
107
+
108
+ # Creates new Queue instance.
109
+ # Does not create a queue at Amazon.
110
+ #
111
+ # queue = Aws::Sqs::Queue.new(sqs, 'my_awesome_queue')
112
+ #
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
118
+
119
+ # Retrieves queue size.
120
+ #
121
+ # queue.size #=> 1
122
+ #
123
+ def size
124
+ @sqs.interface.get_queue_length(@url)
125
+ end
126
+
127
+ # Clears queue, deleting only the visible messages. Any message within its visibility
128
+ # timeout will not be deleted, and will re-appear in the queue in the
129
+ # future when the timeout expires.
130
+ #
131
+ # To delete all messages in a queue and eliminate the chance of any
132
+ # messages re-appearing in the future, it's best to delete the queue and
133
+ # re-create it as a new queue. Note that doing this will take at least 60
134
+ # s since SQS does not allow re-creation of a queue within this interval.
135
+ #
136
+ # queue.clear() #=> true
137
+ #
138
+ def clear()
139
+ @sqs.interface.clear_queue(@url)
140
+ end
141
+
142
+ # Deletes queue. Any messages in the queue will be permanently lost.
143
+ # Returns +true+.
144
+ #
145
+ # NB: Use with caution; severe data loss is possible!
146
+ #
147
+ # queue.delete(true) #=> true
148
+ #
149
+ def delete(force=false)
150
+ @sqs.interface.delete_queue(@url)
151
+ end
152
+
153
+ # Sends new message to queue.
154
+ # Returns new Message instance that has been sent to queue.
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
163
+ alias_method :push, :send_message
164
+
165
+ # Retrieves several messages from queue.
166
+ # Returns an array of Message instances.
167
+ #
168
+ # queue.receive_messages(2,10) #=> array of messages
169
+ #
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
180
+
181
+ # Retrieves first accessible message from queue.
182
+ # Returns Message instance or +nil+ it the queue is empty.
183
+ #
184
+ # queue.receive #=> #<Aws::Sqs::Message:0xb7bf0884 ... >
185
+ #
186
+ def receive(visibility=nil)
187
+ list = receive_messages(1, visibility)
188
+ list.empty? ? nil : list[0]
189
+ end
190
+
191
+ # Pops (and deletes) first accessible message from queue.
192
+ # Returns Message instance or +nil+ if the queue is empty.
193
+ #
194
+ # queue.pop #=> #<Aws::Sqs::Message:0xb7bf0884 ... >
195
+ #
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
206
+
207
+ # Retrieves +VisibilityTimeout+ value for the queue.
208
+ # Returns new timeout value.
209
+ #
210
+ # queue.visibility #=> 30
211
+ #
212
+ def visibility
213
+ @sqs.interface.get_queue_attributes(@url, 'VisibilityTimeout')['VisibilityTimeout']
214
+ end
215
+
216
+ # Sets new +VisibilityTimeout+ for the queue.
217
+ # Returns new timeout value.
218
+ #
219
+ # queue.visibility #=> 30
220
+ # queue.visibility = 33
221
+ # queue.visibility #=> 33
222
+ #
223
+ def visibility=(visibility_timeout)
224
+ @sqs.interface.set_queue_attributes(@url, 'VisibilityTimeout', visibility_timeout)
225
+ visibility_timeout
226
+ end
227
+
228
+ # Sets new queue attribute value.
229
+ # Not all attributes may be changed: +ApproximateNumberOfMessages+ (for example) is a read only attribute.
230
+ # Returns a value to be assigned to attribute.
231
+ # Currently, 'VisibilityTimeout' is the only settable queue attribute.
232
+ # Attempting to set non-existent attributes generates an indignant
233
+ # exception.
234
+ #
235
+ # queue.set_attribute('VisibilityTimeout', '100') #=> '100'
236
+ # queue.get_attribute('VisibilityTimeout') #=> '100'
237
+ #
238
+ def set_attribute(attribute, value)
239
+ @sqs.interface.set_queue_attributes(@url, attribute, value)
240
+ value
241
+ end
242
+
243
+ # Retrieves queue attributes.
244
+ # At this moment Amazon supports +VisibilityTimeout+ and +ApproximateNumberOfMessages+ only.
245
+ # If the name of attribute is set, returns its value. Otherwise, returns a hash of attributes.
246
+ #
247
+ # queue.get_attribute('VisibilityTimeout') #=> {"VisibilityTimeout"=>"45"}
248
+ #
249
+ def get_attribute(attribute='All')
250
+ attributes = @sqs.interface.get_queue_attributes(@url, attribute)
251
+ attribute=='All' ? attributes : attributes[attribute]
252
+ end
253
+ end
254
+
255
+ class Message
256
+ attr_reader :queue, :id, :body, :visibility, :receipt_handle
257
+ attr_accessor :sent_at, :received_at, :send_checksum, :receive_checksum
258
+
259
+ def initialize(queue, id=nil, rh = nil, body=nil, visibility=nil)
260
+ @queue = queue
261
+ @id = id
262
+ @receipt_handle = rh
263
+ @body = body
264
+ @visibility = visibility
265
+ @sent_at = nil
266
+ @received_at = nil
267
+ @send_checksum = nil
268
+ @receive_checksum = nil
269
+ end
270
+
271
+ # Returns +Message+ instance body.
272
+ def to_s
273
+ @body
274
+ end
275
+
276
+ # Removes message from queue.
277
+ # Returns +true+.
278
+ def delete
279
+ @queue.sqs.interface.delete_message(@queue.url, @receipt_handle) if @receipt_handle
280
+ end
281
+ end
282
+ end
283
+ end
@@ -0,0 +1,440 @@
1
+ #
2
+ # Copyright (c) 2007-2008 RightScale Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #
23
+
24
+ module Aws
25
+
26
+ #
27
+ # Right::Aws::SqsInterface - RightScale's low-level Amazon SQS interface
28
+ # For explanations of the semantics
29
+ # of each call, please refer to Amazon's documentation at
30
+ # http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=31
31
+ #
32
+ # This class provides a procedural interface to SQS. Conceptually it is
33
+ # mostly a pass-through interface to SQS and its API is very similar to the
34
+ # bare SQS API. For a somewhat higher-level and object-oriented interface, see
35
+ # Aws::Sqs.
36
+
37
+ class SqsInterface < AwsBase
38
+ include AwsBaseInterface
39
+
40
+ API_VERSION = "2008-01-01"
41
+ DEFAULT_HOST = "queue.amazonaws.com"
42
+ DEFAULT_PORT = 443
43
+ DEFAULT_PROTOCOL = "https"
44
+ REQUEST_TTL = 30
45
+ DEFAULT_VISIBILITY_TIMEOUT = 30
46
+
47
+ @@bench = AwsBenchmarkingBlock.new
48
+ def self.bench_xml
49
+ @@bench.xml
50
+ end
51
+ def self.bench_sqs
52
+ @@bench.service
53
+ end
54
+
55
+ @@api = API_VERSION
56
+ def self.api
57
+ @@api
58
+ end
59
+
60
+ # Creates a new SqsInterface instance. This instance is limited to
61
+ # operations on SQS objects created with Amazon's 2008-01-01 API version. This
62
+ # interface will not work on objects created with prior API versions. See
63
+ # Amazon's article "Migrating to Amazon SQS API version 2008-01-01" at:
64
+ # http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1148
65
+ #
66
+ # sqs = Aws::SqsInterface.new('1E3GDYEOGFJPIT75KDT40','hgTHt68JY07JKUY08ftHYtERkjgtfERn57DFE379', {:multi_thread => true, :logger => Logger.new('/tmp/x.log')})
67
+ #
68
+ # Params is a hash:
69
+ #
70
+ # {:server => 'queue.amazonaws.com' # Amazon service host: 'queue.amazonaws.com' (default)
71
+ # :port => 443 # Amazon service port: 80 or 443 (default)
72
+ # :multi_thread => true|false # Multi-threaded (connection per each thread): true or false (default)
73
+ # :signature_version => '0' # The signature version : '0' or '1'(default)
74
+ # :logger => Logger Object} # Logger instance: logs to STDOUT if omitted }
75
+ #
76
+ def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
77
+ init({ :name => 'SQS',
78
+ :default_host => ENV['SQS_URL'] ? URI.parse(ENV['SQS_URL']).host : DEFAULT_HOST,
79
+ :default_port => ENV['SQS_URL'] ? URI.parse(ENV['SQS_URL']).port : DEFAULT_PORT,
80
+ :default_protocol => ENV['SQS_URL'] ? URI.parse(ENV['SQS_URL']).scheme : DEFAULT_PROTOCOL },
81
+ aws_access_key_id || ENV['AWS_ACCESS_KEY_ID'],
82
+ aws_secret_access_key || ENV['AWS_SECRET_ACCESS_KEY'],
83
+ params)
84
+ end
85
+
86
+
87
+ #-----------------------------------------------------------------
88
+ # Requests
89
+ #-----------------------------------------------------------------
90
+
91
+ # Generates a request hash for the query API
92
+ def generate_request(action, param={}) # :nodoc:
93
+ # For operation requests on a queue, the queue URI will be a parameter,
94
+ # so we first extract it from the call parameters. Next we remove any
95
+ # parameters with no value or with symbolic keys. We add the header
96
+ # fields required in all requests, and then the headers passed in as
97
+ # params. We sort the header fields alphabetically and then generate the
98
+ # signature before URL escaping the resulting query and sending it.
99
+ service = param[:queue_url] ? URI(param[:queue_url]).path : '/'
100
+ param.each{ |key, value| param.delete(key) if (value.nil? || key.is_a?(Symbol)) }
101
+ service_hash = { "Action" => action,
102
+ "Expires" => (Time.now + REQUEST_TTL).utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
103
+ "AWSAccessKeyId" => @aws_access_key_id,
104
+ "Version" => API_VERSION }
105
+ service_hash.update(param)
106
+ service_params = signed_service_params(@aws_secret_access_key, service_hash, :get, @params[:server], service)
107
+ request = Net::HTTP::Get.new("#{AwsUtils.URLencode(service)}?#{service_params}")
108
+ # prepare output hash
109
+ { :request => request,
110
+ :server => @params[:server],
111
+ :port => @params[:port],
112
+ :protocol => @params[:protocol] }
113
+ end
114
+
115
+ def generate_post_request(action, param={}) # :nodoc:
116
+ service = param[:queue_url] ? URI(param[:queue_url]).path : '/'
117
+ message = param[:message] # extract message body if nesessary
118
+ param.each{ |key, value| param.delete(key) if (value.nil? || key.is_a?(Symbol)) }
119
+ service_hash = { "Action" => action,
120
+ "Expires" => (Time.now + REQUEST_TTL).utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
121
+ "AWSAccessKeyId" => @aws_access_key_id,
122
+ "MessageBody" => message,
123
+ "Version" => API_VERSION }
124
+ service_hash.update(param)
125
+ #
126
+ service_params = signed_service_params(@aws_secret_access_key, service_hash, :post, @params[:server], service)
127
+ request = Net::HTTP::Post.new(AwsUtils::URLencode(service))
128
+ request['Content-Type'] = 'application/x-www-form-urlencoded'
129
+ request.body = service_params
130
+ # prepare output hash
131
+ { :request => request,
132
+ :server => @params[:server],
133
+ :port => @params[:port],
134
+ :protocol => @params[:protocol] }
135
+ end
136
+
137
+ # Sends request to Amazon and parses the response
138
+ # Raises AwsError if any banana happened
139
+ def request_info(request, parser) # :nodoc:
140
+ thread = @params[:multi_thread] ? Thread.current : Thread.main
141
+ thread[:sqs_connection] ||= Aws::HttpConnection.new(:exception => AwsError, :logger => @logger)
142
+ request_info_impl(thread[:sqs_connection], @@bench, request, parser)
143
+ end
144
+
145
+ # Creates a new queue, returning its URI.
146
+ #
147
+ # sqs.create_queue('my_awesome_queue') #=> 'http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue'
148
+ #
149
+ def create_queue(queue_name, default_visibility_timeout=nil)
150
+ req_hash = generate_request('CreateQueue', 'QueueName' => queue_name,
151
+ 'DefaultVisibilityTimeout' => default_visibility_timeout || DEFAULT_VISIBILITY_TIMEOUT )
152
+ request_info(req_hash, SqsCreateQueueParser.new(:logger => @logger))
153
+ rescue
154
+ on_exception
155
+ end
156
+
157
+ # Lists all queues owned by this user that have names beginning with +queue_name_prefix+.
158
+ # If +queue_name_prefix+ is omitted then retrieves a list of all queues.
159
+ # Queue creation is an eventual operation and created queues may not show up in immediately subsequent list_queues calls.
160
+ #
161
+ # sqs.create_queue('my_awesome_queue')
162
+ # sqs.create_queue('my_awesome_queue_2')
163
+ # sqs.list_queues('my_awesome') #=> ['http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue','http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue_2']
164
+ #
165
+ def list_queues(queue_name_prefix=nil)
166
+ req_hash = generate_request('ListQueues', 'QueueNamePrefix' => queue_name_prefix)
167
+ request_info(req_hash, SqsListQueuesParser.new(:logger => @logger))
168
+ rescue
169
+ on_exception
170
+ end
171
+
172
+ # Deletes queue. Any messages in the queue are permanently lost.
173
+ # Returns +true+ or an exception.
174
+ # Queue deletion can take up to 60 s to propagate through SQS. Thus, after a deletion, subsequent list_queues calls
175
+ # may still show the deleted queue. It is not unusual within the 60 s window to see the deleted queue absent from
176
+ # one list_queues call but present in the subsequent one. Deletion is eventual.
177
+ #
178
+ # sqs.delete_queue('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue_2') #=> true
179
+ #
180
+ #
181
+ def delete_queue(queue_url)
182
+ req_hash = generate_request('DeleteQueue', :queue_url => queue_url)
183
+ request_info(req_hash, SqsStatusParser.new(:logger => @logger))
184
+ rescue
185
+ on_exception
186
+ end
187
+
188
+ # Retrieves the queue attribute(s). Returns a hash of attribute(s) or an exception.
189
+ #
190
+ # sqs.get_queue_attributes('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue')
191
+ # #=> {"ApproximateNumberOfMessages"=>"0", "VisibilityTimeout"=>"30"}
192
+ #
193
+ def get_queue_attributes(queue_url, attribute='All')
194
+ req_hash = generate_request('GetQueueAttributes', 'AttributeName' => attribute, :queue_url => queue_url)
195
+ request_info(req_hash, SqsGetQueueAttributesParser.new(:logger => @logger))
196
+ rescue
197
+ on_exception
198
+ end
199
+
200
+ # Sets queue attribute. Returns +true+ or an exception.
201
+ #
202
+ # sqs.set_queue_attributes('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', "VisibilityTimeout", 10) #=> true
203
+ #
204
+ # From the SQS Dev Guide:
205
+ # "Currently, you can set only the
206
+ # VisibilityTimeout attribute for a queue...
207
+ # When you change a queue's attributes, the change can take up to 60 seconds to propagate
208
+ # throughout the SQS system."
209
+ #
210
+ # NB: Attribute values may not be immediately available to other queries
211
+ # for some time after an update. See the SQS documentation for
212
+ # semantics, but in general propagation can take up to 60 s.
213
+ def set_queue_attributes(queue_url, attribute, value)
214
+ req_hash = generate_request('SetQueueAttributes', 'Attribute.Name' => attribute, 'Attribute.Value' => value, :queue_url => queue_url)
215
+ request_info(req_hash, SqsStatusParser.new(:logger => @logger))
216
+ rescue
217
+ on_exception
218
+ end
219
+
220
+ # Retrieves a list of messages from queue. Returns an array of hashes in format: <tt>{:id=>'message_id', body=>'message_body'}</tt>
221
+ #
222
+ # sqs.receive_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue',10, 5) #=>
223
+ # [{"ReceiptHandle"=>"Euvo62...kw==", "MD5OfBody"=>"16af2171b5b83cfa35ce254966ba81e3",
224
+ # "Body"=>"Goodbyte World!", "MessageId"=>"MUM4WlAyR...pYOTA="}, ..., {}]
225
+ #
226
+ # Normally this call returns fewer messages than the maximum specified,
227
+ # even if they are available.
228
+ #
229
+ def receive_message(queue_url, max_number_of_messages=1, visibility_timeout=nil)
230
+ return [] if max_number_of_messages == 0
231
+ req_hash = generate_post_request('ReceiveMessage', 'MaxNumberOfMessages' => max_number_of_messages, 'VisibilityTimeout' => visibility_timeout,
232
+ :queue_url => queue_url )
233
+ request_info(req_hash, SqsReceiveMessageParser.new(:logger => @logger))
234
+ rescue
235
+ on_exception
236
+ end
237
+
238
+ # Sends a new message to a queue. Message size is limited to 8 KB.
239
+ # If successful, this call returns a hash containing key/value pairs for
240
+ # "MessageId" and "MD5OfMessageBody":
241
+ #
242
+ # sqs.send_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', 'message_1') #=> "1234567890...0987654321"
243
+ # => {"MessageId"=>"MEs4M0JKNlRCRTBBSENaMjROTk58QVFRNzNEREhDVFlFOVJDQ1JKNjF8UTdBRllCUlJUMjhKMUI1WDJSWDE=", "MD5OfMessageBody"=>"16af2171b5b83cfa35ce254966ba81e3"}
244
+ #
245
+ # On failure, send_message raises an exception.
246
+ #
247
+ #
248
+ def send_message(queue_url, message)
249
+ req_hash = generate_post_request('SendMessage', :message => message, :queue_url => queue_url)
250
+ request_info(req_hash, SqsSendMessagesParser.new(:logger => @logger))
251
+ rescue
252
+ on_exception
253
+ end
254
+
255
+ # Same as send_message
256
+ alias_method :push_message, :send_message
257
+
258
+
259
+ # Deletes message from queue. Returns +true+ or an exception. Amazon
260
+ # returns +true+ on deletion of non-existent messages. You must use the
261
+ # receipt handle for a message to delete it, not the message ID.
262
+ #
263
+ # From the SQS Developer Guide:
264
+ # "It is possible you will receive a message even after you have deleted it. This might happen
265
+ # on rare occasions if one of the servers storing a copy of the message is unavailable when
266
+ # you request to delete the message. The copy remains on the server and might be returned to
267
+ # you again on a subsequent receive request. You should create your system to be
268
+ # idempotent so that receiving a particular message more than once is not a problem. "
269
+ #
270
+ # sqs.delete_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', 'Euvo62/1nlIet...ao03hd9Sa0w==') #=> true
271
+ #
272
+ def delete_message(queue_url, receipt_handle)
273
+ req_hash = generate_request('DeleteMessage', 'ReceiptHandle' => receipt_handle, :queue_url => queue_url)
274
+ request_info(req_hash, SqsStatusParser.new(:logger => @logger))
275
+ rescue
276
+ on_exception
277
+ end
278
+
279
+ # Given the queue's short name, this call returns the queue URL or +nil+ if queue is not found
280
+ # sqs.queue_url_by_name('my_awesome_queue') #=> 'http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue'
281
+ #
282
+ def queue_url_by_name(queue_name)
283
+ return queue_name if queue_name.include?('/')
284
+ queue_urls = list_queues(queue_name)
285
+ queue_urls.each do |queue_url|
286
+ return queue_url if queue_name_by_url(queue_url) == queue_name
287
+ end
288
+ nil
289
+ rescue
290
+ on_exception
291
+ end
292
+
293
+ # Returns short queue name by url.
294
+ #
295
+ # Sqs.queue_name_by_url('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> 'my_awesome_queue'
296
+ #
297
+ def self.queue_name_by_url(queue_url)
298
+ queue_url[/[^\/]*$/]
299
+ rescue
300
+ on_exception
301
+ end
302
+
303
+ # Returns short queue name by url.
304
+ #
305
+ # sqs.queue_name_by_url('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> 'my_awesome_queue'
306
+ #
307
+ def queue_name_by_url(queue_url)
308
+ self.class.queue_name_by_url(queue_url)
309
+ rescue
310
+ on_exception
311
+ end
312
+
313
+ # Returns approximate number of messages in queue.
314
+ #
315
+ # sqs.get_queue_length('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> 3
316
+ #
317
+ def get_queue_length(queue_url)
318
+ get_queue_attributes(queue_url)['ApproximateNumberOfMessages'].to_i
319
+ rescue
320
+ on_exception
321
+ end
322
+
323
+ # Removes all visible messages from queue. Return +true+ or an exception.
324
+ #
325
+ # sqs.clear_queue('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> true
326
+ #
327
+ def clear_queue(queue_url)
328
+ while (pop_messages(queue_url, 10).length > 0) ; end # delete all messages in queue
329
+ true
330
+ rescue
331
+ on_exception
332
+ end
333
+
334
+ # Pops (retrieves and deletes) up to 'number_of_messages' from queue. Returns an array of retrieved messages in format: <tt>[{:id=>'message_id', :body=>'message_body'}]</tt>.
335
+ #
336
+ # sqs.pop_messages('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', 3) #=>
337
+ # [{"ReceiptHandle"=>"Euvo62/...+Zw==", "MD5OfBody"=>"16af2...81e3", "Body"=>"Goodbyte World!",
338
+ # "MessageId"=>"MEZI...JSWDE="}, {...}, ... , {...} ]
339
+ #
340
+ def pop_messages(queue_url, number_of_messages=1)
341
+ messages = receive_message(queue_url, number_of_messages)
342
+ messages.each do |message|
343
+ delete_message(queue_url, message['ReceiptHandle'])
344
+ end
345
+ messages
346
+ rescue
347
+ on_exception
348
+ end
349
+
350
+ # Pops (retrieves and deletes) first accessible message from queue. Returns the message in format <tt>{:id=>'message_id', :body=>'message_body'}</tt> or +nil+.
351
+ #
352
+ # sqs.pop_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=>
353
+ # {:id=>"12345678904GEZX9746N|0N9ED344VK5Z3SV1DTM0|1RVYH4X3TJ0987654321", :body=>"message_1"}
354
+ #
355
+ def pop_message(queue_url)
356
+ messages = pop_messages(queue_url)
357
+ messages.blank? ? nil : messages[0]
358
+ rescue
359
+ on_exception
360
+ end
361
+
362
+ #-----------------------------------------------------------------
363
+ # PARSERS: Status Response Parser
364
+ #-----------------------------------------------------------------
365
+
366
+ class SqsStatusParser < AwsParser # :nodoc:
367
+ def tagend(name)
368
+ if name == 'ResponseMetadata'
369
+ @result = true
370
+ end
371
+ end
372
+ end
373
+
374
+ #-----------------------------------------------------------------
375
+ # PARSERS: Queue
376
+ #-----------------------------------------------------------------
377
+
378
+ class SqsCreateQueueParser < AwsParser # :nodoc:
379
+ def tagend(name)
380
+ @result = @text if name == 'QueueUrl'
381
+ end
382
+ end
383
+
384
+ class SqsListQueuesParser < AwsParser # :nodoc:
385
+ def reset
386
+ @result = []
387
+ end
388
+ def tagend(name)
389
+ @result << @text if name == 'QueueUrl'
390
+ end
391
+ end
392
+
393
+ class SqsGetQueueAttributesParser < AwsParser # :nodoc:
394
+ def reset
395
+ @result = {}
396
+ end
397
+ def tagend(name)
398
+ case name
399
+ when 'Name' ; @current_attribute = @text
400
+ when 'Value' ; @result[@current_attribute] = @text
401
+ end
402
+ end
403
+ end
404
+
405
+ #-----------------------------------------------------------------
406
+ # PARSERS: Messages
407
+ #-----------------------------------------------------------------
408
+
409
+ class SqsReceiveMessageParser < AwsParser # :nodoc:
410
+ def reset
411
+ @result = []
412
+ end
413
+ def tagstart(name, attributes)
414
+ @current_message = {} if name == 'Message'
415
+ end
416
+ def tagend(name)
417
+ case name
418
+ when 'MessageId' ; @current_message['MessageId'] = @text
419
+ when 'ReceiptHandle' ; @current_message['ReceiptHandle'] = @text
420
+ when 'MD5OfBody' ; @current_message['MD5OfBody'] = @text
421
+ when 'Body'; @current_message['Body'] = @text; @result << @current_message
422
+ end
423
+ end
424
+ end
425
+
426
+ class SqsSendMessagesParser < AwsParser # :nodoc:
427
+ def reset
428
+ @result = {}
429
+ end
430
+ def tagend(name)
431
+ case name
432
+ when 'MessageId' ; @result['MessageId'] = @text
433
+ when 'MD5OfMessageBody' ; @result['MD5OfMessageBody'] = @text
434
+ end
435
+ end
436
+ end
437
+
438
+ end
439
+
440
+ end