hackerdude-aws 2.3.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +187 -0
- data/lib/acf/right_acf_interface.rb +383 -0
- data/lib/alexa/alexa.rb +239 -0
- data/lib/aws.rb +30 -0
- data/lib/awsbase/aws_response_array.rb +30 -0
- data/lib/awsbase/benchmark_fix.rb +39 -0
- data/lib/awsbase/right_awsbase.rb +1239 -0
- data/lib/awsbase/support.rb +149 -0
- data/lib/ec2/right_ec2.rb +1874 -0
- data/lib/ec2/right_mon_interface.rb +233 -0
- data/lib/elb/elb_interface.rb +359 -0
- data/lib/rds/rds.rb +216 -0
- data/lib/right_aws.rb +57 -0
- data/lib/s3/right_s3.rb +1110 -0
- data/lib/s3/right_s3_interface.rb +1235 -0
- data/lib/sdb/active_sdb.rb +990 -0
- data/lib/sdb/right_sdb_interface.rb +816 -0
- data/lib/sqs/right_sqs.rb +287 -0
- data/lib/sqs/right_sqs_interface.rb +445 -0
- data/test/acf/test_acf.rb +148 -0
- data/test/acf/test_helper.rb +2 -0
- data/test/alexa/test_alexa.rb +53 -0
- data/test/alexa/test_helper.rb +2 -0
- data/test/ec2/test_ec2.rb +205 -0
- data/test/ec2/test_helper.rb +2 -0
- data/test/ec2/test_mon.rb +17 -0
- data/test/elb/test_elb.rb +51 -0
- data/test/http_connection.rb +87 -0
- data/test/rds/test_rds.rb +181 -0
- data/test/s3/test_helper.rb +3 -0
- data/test/s3/test_s3.rb +425 -0
- data/test/s3/test_s3_stubbed.rb +97 -0
- data/test/sdb/test_active_sdb.rb +338 -0
- data/test/sdb/test_helper.rb +3 -0
- data/test/sdb/test_sdb.rb +207 -0
- data/test/sqs/test_helper.rb +2 -0
- data/test/sqs/test_sqs.rb +206 -0
- data/test/test_credentials.rb +54 -0
- data/test/ts_right_aws.rb +13 -0
- metadata +182 -0
@@ -0,0 +1,287 @@
|
|
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 = @interface.create_queue(queue_name, visibility) # this returns the url even if it exists
|
93
|
+
url ? Queue.new(self, url) : nil
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
class Queue
|
98
|
+
attr_reader :name, :url, :sqs
|
99
|
+
|
100
|
+
# Returns Queue instance by queue name.
|
101
|
+
# If the queue does not exist at Amazon SQS and +create+ is true, the method creates it.
|
102
|
+
#
|
103
|
+
# Aws::Sqs::Queue.create(sqs, 'my_awesome_queue') #=> #<Aws::Sqs::Queue:0xb7b626e4 ... >
|
104
|
+
#
|
105
|
+
def self.create(sqs, url_or_name, create=true, visibility=nil)
|
106
|
+
sqs.queue(url_or_name, create, visibility)
|
107
|
+
end
|
108
|
+
|
109
|
+
# Creates new Queue instance.
|
110
|
+
# Does not create a queue at Amazon.
|
111
|
+
#
|
112
|
+
# queue = Aws::Sqs::Queue.new(sqs, 'my_awesome_queue')
|
113
|
+
#
|
114
|
+
def initialize(sqs, url_or_name)
|
115
|
+
@sqs = sqs
|
116
|
+
@url = @sqs.interface.queue_url_by_name(url_or_name)
|
117
|
+
@name = @sqs.interface.queue_name_by_url(@url)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Retrieves queue size.
|
121
|
+
#
|
122
|
+
# queue.size #=> 1
|
123
|
+
#
|
124
|
+
def size
|
125
|
+
@sqs.interface.get_queue_length(@url)
|
126
|
+
end
|
127
|
+
|
128
|
+
# Clears queue, deleting only the visible messages. Any message within its visibility
|
129
|
+
# timeout will not be deleted, and will re-appear in the queue in the
|
130
|
+
# future when the timeout expires.
|
131
|
+
#
|
132
|
+
# To delete all messages in a queue and eliminate the chance of any
|
133
|
+
# messages re-appearing in the future, it's best to delete the queue and
|
134
|
+
# re-create it as a new queue. Note that doing this will take at least 60
|
135
|
+
# s since SQS does not allow re-creation of a queue within this interval.
|
136
|
+
#
|
137
|
+
# queue.clear() #=> true
|
138
|
+
#
|
139
|
+
def clear()
|
140
|
+
@sqs.interface.clear_queue(@url)
|
141
|
+
end
|
142
|
+
|
143
|
+
# Deletes queue. Any messages in the queue will be permanently lost.
|
144
|
+
# Returns +true+.
|
145
|
+
#
|
146
|
+
# NB: Use with caution; severe data loss is possible!
|
147
|
+
#
|
148
|
+
# queue.delete(true) #=> true
|
149
|
+
#
|
150
|
+
def delete(force=false)
|
151
|
+
@sqs.interface.delete_queue(@url)
|
152
|
+
end
|
153
|
+
|
154
|
+
# Sends new message to queue.
|
155
|
+
# Returns new Message instance that has been sent to queue.
|
156
|
+
def send_message(message)
|
157
|
+
message = message.to_s
|
158
|
+
res = @sqs.interface.send_message(@url, message)
|
159
|
+
msg = Message.new(self, res['MessageId'], nil, message)
|
160
|
+
msg.send_checksum = res['MD5OfMessageBody']
|
161
|
+
msg.sent_at = Time.now
|
162
|
+
msg
|
163
|
+
end
|
164
|
+
alias_method :push, :send_message
|
165
|
+
|
166
|
+
# Retrieves several messages from queue.
|
167
|
+
# Returns an array of Message instances.
|
168
|
+
#
|
169
|
+
# queue.receive_messages(2,10) #=> array of messages
|
170
|
+
#
|
171
|
+
def receive_messages(number_of_messages=1, visibility=nil)
|
172
|
+
list = @sqs.interface.receive_message(@url, number_of_messages, visibility)
|
173
|
+
list.map! do |entry|
|
174
|
+
msg = Message.new(self, entry['MessageId'], entry['ReceiptHandle'],
|
175
|
+
entry['Body'], visibility)
|
176
|
+
msg.received_at = Time.now
|
177
|
+
msg.receive_checksum = entry['MD5OfBody']
|
178
|
+
msg
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
# Retrieves first accessible message from queue.
|
183
|
+
# Returns Message instance or +nil+ it the queue is empty.
|
184
|
+
#
|
185
|
+
# queue.receive #=> #<Aws::Sqs::Message:0xb7bf0884 ... >
|
186
|
+
#
|
187
|
+
def receive(visibility=nil)
|
188
|
+
list = receive_messages(1, visibility)
|
189
|
+
list.empty? ? nil : list[0]
|
190
|
+
end
|
191
|
+
|
192
|
+
# Pops (and deletes) first accessible message from queue.
|
193
|
+
# Returns Message instance or +nil+ if the queue is empty.
|
194
|
+
#
|
195
|
+
# queue.pop #=> #<Aws::Sqs::Message:0xb7bf0884 ... >
|
196
|
+
#
|
197
|
+
def pop
|
198
|
+
list = @sqs.interface.pop_messages(@url, 1)
|
199
|
+
return nil if list.empty?
|
200
|
+
entry = list[0]
|
201
|
+
msg = Message.new(self, entry['MessageId'], entry['ReceiptHandle'],
|
202
|
+
entry['Body'], visibility)
|
203
|
+
msg.received_at = Time.now
|
204
|
+
msg.receive_checksum = entry['MD5OfBody']
|
205
|
+
msg
|
206
|
+
end
|
207
|
+
|
208
|
+
# Retrieves +VisibilityTimeout+ value for the queue.
|
209
|
+
# Returns new timeout value.
|
210
|
+
#
|
211
|
+
# queue.visibility #=> 30
|
212
|
+
#
|
213
|
+
def visibility
|
214
|
+
@sqs.interface.get_queue_attributes(@url, 'VisibilityTimeout')['VisibilityTimeout']
|
215
|
+
end
|
216
|
+
|
217
|
+
# Sets new +VisibilityTimeout+ for the queue.
|
218
|
+
# Returns new timeout value.
|
219
|
+
#
|
220
|
+
# queue.visibility #=> 30
|
221
|
+
# queue.visibility = 33
|
222
|
+
# queue.visibility #=> 33
|
223
|
+
#
|
224
|
+
def visibility=(visibility_timeout)
|
225
|
+
@sqs.interface.set_queue_attributes(@url, 'VisibilityTimeout', visibility_timeout)
|
226
|
+
visibility_timeout
|
227
|
+
end
|
228
|
+
|
229
|
+
# Sets new queue attribute value.
|
230
|
+
# Not all attributes may be changed: +ApproximateNumberOfMessages+ (for example) is a read only attribute.
|
231
|
+
# Returns a value to be assigned to attribute.
|
232
|
+
# Currently, 'VisibilityTimeout' is the only settable queue attribute.
|
233
|
+
# Attempting to set non-existent attributes generates an indignant
|
234
|
+
# exception.
|
235
|
+
#
|
236
|
+
# queue.set_attribute('VisibilityTimeout', '100') #=> '100'
|
237
|
+
# queue.get_attribute('VisibilityTimeout') #=> '100'
|
238
|
+
#
|
239
|
+
def set_attribute(attribute, value)
|
240
|
+
@sqs.interface.set_queue_attributes(@url, attribute, value)
|
241
|
+
value
|
242
|
+
end
|
243
|
+
|
244
|
+
# Retrieves queue attributes.
|
245
|
+
# At this moment Amazon supports +VisibilityTimeout+ and +ApproximateNumberOfMessages+ only.
|
246
|
+
# If the name of attribute is set, returns its value. Otherwise, returns a hash of attributes.
|
247
|
+
#
|
248
|
+
# queue.get_attribute('VisibilityTimeout') #=> {"VisibilityTimeout"=>"45"}
|
249
|
+
#
|
250
|
+
def get_attribute(attribute='All')
|
251
|
+
attributes = @sqs.interface.get_queue_attributes(@url, attribute)
|
252
|
+
attribute=='All' ? attributes : attributes[attribute]
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
class Message
|
257
|
+
attr_reader :queue, :id, :body, :visibility, :receipt_handle
|
258
|
+
attr_accessor :sent_at, :received_at, :send_checksum, :receive_checksum
|
259
|
+
|
260
|
+
def initialize(queue, id=nil, rh = nil, body=nil, visibility=nil)
|
261
|
+
@queue = queue
|
262
|
+
@id = id
|
263
|
+
@receipt_handle = rh
|
264
|
+
@body = body
|
265
|
+
@visibility = visibility
|
266
|
+
@sent_at = nil
|
267
|
+
@received_at = nil
|
268
|
+
@send_checksum = nil
|
269
|
+
@receive_checksum = nil
|
270
|
+
end
|
271
|
+
|
272
|
+
# Returns +Message+ instance body.
|
273
|
+
def to_s
|
274
|
+
@body
|
275
|
+
end
|
276
|
+
|
277
|
+
# Removes message from queue.
|
278
|
+
# Returns +true+.
|
279
|
+
def delete
|
280
|
+
@queue.sqs.interface.delete_message(@queue.url, @receipt_handle) if @receipt_handle
|
281
|
+
end
|
282
|
+
|
283
|
+
end
|
284
|
+
|
285
|
+
|
286
|
+
end
|
287
|
+
end
|
@@ -0,0 +1,445 @@
|
|
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 API version 2008-01-01 and later.
|
29
|
+
# For explanations of the semantics
|
30
|
+
# of each call, please refer to Amazon's documentation at
|
31
|
+
# http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=31
|
32
|
+
#
|
33
|
+
# This class provides a procedural interface to SQS. Conceptually it is
|
34
|
+
# mostly a pass-through interface to SQS and its API is very similar to the
|
35
|
+
# bare SQS API. For a somewhat higher-level and object-oriented interface, see
|
36
|
+
# Aws::Sqs.
|
37
|
+
|
38
|
+
class SqsInterface < AwsBase
|
39
|
+
include AwsBaseInterface
|
40
|
+
|
41
|
+
API_VERSION = "2009-02-01"
|
42
|
+
DEFAULT_HOST = "queue.amazonaws.com"
|
43
|
+
DEFAULT_PORT = 443
|
44
|
+
DEFAULT_PROTOCOL = 'https'
|
45
|
+
REQUEST_TTL = 30
|
46
|
+
DEFAULT_VISIBILITY_TIMEOUT = 30
|
47
|
+
|
48
|
+
|
49
|
+
@@bench = AwsBenchmarkingBlock.new
|
50
|
+
def self.bench_xml
|
51
|
+
@@bench.xml
|
52
|
+
end
|
53
|
+
def self.bench_sqs
|
54
|
+
@@bench.service
|
55
|
+
end
|
56
|
+
|
57
|
+
@@api = API_VERSION
|
58
|
+
def self.api
|
59
|
+
@@api
|
60
|
+
end
|
61
|
+
|
62
|
+
# Creates a new SqsInterface instance. This instance is limited to
|
63
|
+
# operations on SQS objects created with Amazon's 2008-01-01 API version. This
|
64
|
+
# interface will not work on objects created with prior API versions. See
|
65
|
+
# Amazon's article "Migrating to Amazon SQS API version 2008-01-01" at:
|
66
|
+
# http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1148
|
67
|
+
#
|
68
|
+
# sqs = Aws::SqsInterface.new('1E3GDYEOGFJPIT75KDT40','hgTHt68JY07JKUY08ftHYtERkjgtfERn57DFE379', {:multi_thread => true, :logger => Logger.new('/tmp/x.log')})
|
69
|
+
#
|
70
|
+
# Params is a hash:
|
71
|
+
#
|
72
|
+
# {:server => 'queue.amazonaws.com' # Amazon service host: 'queue.amazonaws.com' (default)
|
73
|
+
# :port => 443 # Amazon service port: 80 or 443 (default)
|
74
|
+
# :multi_thread => true|false # Multi-threaded (connection per each thread): true or false (default)
|
75
|
+
# :signature_version => '0' # The signature version : '0' or '1'(default)
|
76
|
+
# :logger => Logger Object} # Logger instance: logs to STDOUT if omitted }
|
77
|
+
#
|
78
|
+
def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
|
79
|
+
init({ :name => 'SQS',
|
80
|
+
:default_host => ENV['SQS_URL'] ? URI.parse(ENV['SQS_URL']).host : DEFAULT_HOST,
|
81
|
+
:default_port => ENV['SQS_URL'] ? URI.parse(ENV['SQS_URL']).port : DEFAULT_PORT,
|
82
|
+
:default_protocol => ENV['SQS_URL'] ? URI.parse(ENV['SQS_URL']).scheme : DEFAULT_PROTOCOL,
|
83
|
+
:api_version => API_VERSION },
|
84
|
+
aws_access_key_id || ENV['AWS_ACCESS_KEY_ID'],
|
85
|
+
aws_secret_access_key || ENV['AWS_SECRET_ACCESS_KEY'],
|
86
|
+
params)
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
#-----------------------------------------------------------------
|
91
|
+
# Requests
|
92
|
+
#-----------------------------------------------------------------
|
93
|
+
|
94
|
+
# Generates a request hash for the query API
|
95
|
+
def generate_request(action, param={}) # :nodoc:
|
96
|
+
# For operation requests on a queue, the queue URI will be a parameter,
|
97
|
+
# so we first extract it from the call parameters. Next we remove any
|
98
|
+
# parameters with no value or with symbolic keys. We add the header
|
99
|
+
# fields required in all requests, and then the headers passed in as
|
100
|
+
# params. We sort the header fields alphabetically and then generate the
|
101
|
+
# signature before URL escaping the resulting query and sending it.
|
102
|
+
service = param[:queue_url] ? URI(param[:queue_url]).path : '/'
|
103
|
+
param.each{ |key, value| param.delete(key) if (value.nil? || key.is_a?(Symbol)) }
|
104
|
+
service_hash = { "Action" => action,
|
105
|
+
"Expires" => (Time.now + REQUEST_TTL).utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
106
|
+
"AWSAccessKeyId" => @aws_access_key_id,
|
107
|
+
"Version" => API_VERSION }
|
108
|
+
service_hash.update(param)
|
109
|
+
service_params = signed_service_params(@aws_secret_access_key, service_hash, :get, @params[:server], service)
|
110
|
+
request = Net::HTTP::Get.new("#{AwsUtils.URLencode(service)}?#{service_params}")
|
111
|
+
# prepare output hash
|
112
|
+
{ :request => request,
|
113
|
+
:server => @params[:server],
|
114
|
+
:port => @params[:port],
|
115
|
+
:protocol => @params[:protocol] }
|
116
|
+
end
|
117
|
+
|
118
|
+
def generate_post_request(action, param={}) # :nodoc:
|
119
|
+
service = param[:queue_url] ? URI(param[:queue_url]).path : '/'
|
120
|
+
message = param[:message] # extract message body if nesessary
|
121
|
+
param.each{ |key, value| param.delete(key) if (value.nil? || key.is_a?(Symbol)) }
|
122
|
+
service_hash = { "Action" => action,
|
123
|
+
"Expires" => (Time.now + REQUEST_TTL).utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
|
124
|
+
"AWSAccessKeyId" => @aws_access_key_id,
|
125
|
+
"MessageBody" => message,
|
126
|
+
"Version" => API_VERSION }
|
127
|
+
service_hash.update(param)
|
128
|
+
#
|
129
|
+
service_params = signed_service_params(@aws_secret_access_key, service_hash, :post, @params[:server], service)
|
130
|
+
request = Net::HTTP::Post.new(AwsUtils::URLencode(service))
|
131
|
+
request['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
|
132
|
+
request.body = service_params
|
133
|
+
# prepare output hash
|
134
|
+
{ :request => request,
|
135
|
+
:server => @params[:server],
|
136
|
+
:port => @params[:port],
|
137
|
+
:protocol => @params[:protocol] }
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
# Sends request to Amazon and parses the response
|
142
|
+
# Raises AwsError if any banana happened
|
143
|
+
def request_info(request, parser) # :nodoc:
|
144
|
+
thread = @params[:multi_thread] ? Thread.current : Thread.main
|
145
|
+
thread[:sqs_connection] ||= Rightscale::HttpConnection.new(:exception => AwsError, :logger => @logger)
|
146
|
+
request_info_impl(thread[:sqs_connection], @@bench, request, parser)
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
# Creates a new queue, returning its URI.
|
151
|
+
#
|
152
|
+
# sqs.create_queue('my_awesome_queue') #=> 'http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue'
|
153
|
+
#
|
154
|
+
def create_queue(queue_name, default_visibility_timeout=nil)
|
155
|
+
req_hash = generate_request('CreateQueue', 'QueueName' => queue_name,
|
156
|
+
'DefaultVisibilityTimeout' => default_visibility_timeout || DEFAULT_VISIBILITY_TIMEOUT )
|
157
|
+
request_info(req_hash, SqsCreateQueueParser.new(:logger => @logger))
|
158
|
+
rescue
|
159
|
+
on_exception
|
160
|
+
end
|
161
|
+
|
162
|
+
# Lists all queues owned by this user that have names beginning with +queue_name_prefix+.
|
163
|
+
# If +queue_name_prefix+ is omitted then retrieves a list of all queues.
|
164
|
+
# Queue creation is an eventual operation and created queues may not show up in immediately subsequent list_queues calls.
|
165
|
+
#
|
166
|
+
# sqs.create_queue('my_awesome_queue')
|
167
|
+
# sqs.create_queue('my_awesome_queue_2')
|
168
|
+
# sqs.list_queues('my_awesome') #=> ['http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue','http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue_2']
|
169
|
+
#
|
170
|
+
def list_queues(queue_name_prefix=nil)
|
171
|
+
req_hash = generate_request('ListQueues', 'QueueNamePrefix' => queue_name_prefix)
|
172
|
+
request_info(req_hash, SqsListQueuesParser.new(:logger => @logger))
|
173
|
+
rescue
|
174
|
+
on_exception
|
175
|
+
end
|
176
|
+
|
177
|
+
# Deletes queue. Any messages in the queue are permanently lost.
|
178
|
+
# Returns +true+ or an exception.
|
179
|
+
# Queue deletion can take up to 60 s to propagate through SQS. Thus, after a deletion, subsequent list_queues calls
|
180
|
+
# may still show the deleted queue. It is not unusual within the 60 s window to see the deleted queue absent from
|
181
|
+
# one list_queues call but present in the subsequent one. Deletion is eventual.
|
182
|
+
#
|
183
|
+
# sqs.delete_queue('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue_2') #=> true
|
184
|
+
#
|
185
|
+
#
|
186
|
+
def delete_queue(queue_url)
|
187
|
+
req_hash = generate_request('DeleteQueue', :queue_url => queue_url)
|
188
|
+
request_info(req_hash, SqsStatusParser.new(:logger => @logger))
|
189
|
+
rescue
|
190
|
+
on_exception
|
191
|
+
end
|
192
|
+
|
193
|
+
# Retrieves the queue attribute(s). Returns a hash of attribute(s) or an exception.
|
194
|
+
#
|
195
|
+
# sqs.get_queue_attributes('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue')
|
196
|
+
# #=> {"ApproximateNumberOfMessages"=>"0", "VisibilityTimeout"=>"30"}
|
197
|
+
#
|
198
|
+
def get_queue_attributes(queue_url, attribute='All')
|
199
|
+
req_hash = generate_request('GetQueueAttributes', 'AttributeName' => attribute, :queue_url => queue_url)
|
200
|
+
request_info(req_hash, SqsGetQueueAttributesParser.new(:logger => @logger))
|
201
|
+
rescue
|
202
|
+
on_exception
|
203
|
+
end
|
204
|
+
|
205
|
+
# Sets queue attribute. Returns +true+ or an exception.
|
206
|
+
#
|
207
|
+
# sqs.set_queue_attributes('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', "VisibilityTimeout", 10) #=> true
|
208
|
+
#
|
209
|
+
# From the SQS Dev Guide:
|
210
|
+
# "Currently, you can set only the
|
211
|
+
# VisibilityTimeout attribute for a queue...
|
212
|
+
# When you change a queue's attributes, the change can take up to 60 seconds to propagate
|
213
|
+
# throughout the SQS system."
|
214
|
+
#
|
215
|
+
# NB: Attribute values may not be immediately available to other queries
|
216
|
+
# for some time after an update. See the SQS documentation for
|
217
|
+
# semantics, but in general propagation can take up to 60 s.
|
218
|
+
def set_queue_attributes(queue_url, attribute, value)
|
219
|
+
req_hash = generate_request('SetQueueAttributes', 'Attribute.Name' => attribute, 'Attribute.Value' => value, :queue_url => queue_url)
|
220
|
+
request_info(req_hash, SqsStatusParser.new(:logger => @logger))
|
221
|
+
rescue
|
222
|
+
on_exception
|
223
|
+
end
|
224
|
+
|
225
|
+
# Retrieves a list of messages from queue. Returns an array of hashes in format: <tt>{:id=>'message_id', body=>'message_body'}</tt>
|
226
|
+
#
|
227
|
+
# sqs.receive_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue',10, 5) #=>
|
228
|
+
# [{"ReceiptHandle"=>"Euvo62...kw==", "MD5OfBody"=>"16af2171b5b83cfa35ce254966ba81e3",
|
229
|
+
# "Body"=>"Goodbyte World!", "MessageId"=>"MUM4WlAyR...pYOTA="}, ..., {}]
|
230
|
+
#
|
231
|
+
# Normally this call returns fewer messages than the maximum specified,
|
232
|
+
# even if they are available.
|
233
|
+
#
|
234
|
+
def receive_message(queue_url, max_number_of_messages=1, visibility_timeout=nil)
|
235
|
+
return [] if max_number_of_messages == 0
|
236
|
+
req_hash = generate_post_request('ReceiveMessage', 'MaxNumberOfMessages' => max_number_of_messages, 'VisibilityTimeout' => visibility_timeout,
|
237
|
+
:queue_url => queue_url )
|
238
|
+
request_info(req_hash, SqsReceiveMessageParser.new(:logger => @logger))
|
239
|
+
rescue
|
240
|
+
on_exception
|
241
|
+
end
|
242
|
+
|
243
|
+
# Sends a new message to a queue. Message size is limited to 8 KB.
|
244
|
+
# If successful, this call returns a hash containing key/value pairs for
|
245
|
+
# "MessageId" and "MD5OfMessageBody":
|
246
|
+
#
|
247
|
+
# sqs.send_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', 'message_1') #=> "1234567890...0987654321"
|
248
|
+
# => {"MessageId"=>"MEs4M0JKNlRCRTBBSENaMjROTk58QVFRNzNEREhDVFlFOVJDQ1JKNjF8UTdBRllCUlJUMjhKMUI1WDJSWDE=", "MD5OfMessageBody"=>"16af2171b5b83cfa35ce254966ba81e3"}
|
249
|
+
#
|
250
|
+
# On failure, send_message raises an exception.
|
251
|
+
#
|
252
|
+
#
|
253
|
+
def send_message(queue_url, message)
|
254
|
+
req_hash = generate_post_request('SendMessage', :message => message, :queue_url => queue_url)
|
255
|
+
request_info(req_hash, SqsSendMessagesParser.new(:logger => @logger))
|
256
|
+
rescue
|
257
|
+
on_exception
|
258
|
+
end
|
259
|
+
|
260
|
+
# Same as send_message
|
261
|
+
alias_method :push_message, :send_message
|
262
|
+
|
263
|
+
|
264
|
+
# Deletes message from queue. Returns +true+ or an exception. Amazon
|
265
|
+
# returns +true+ on deletion of non-existent messages. You must use the
|
266
|
+
# receipt handle for a message to delete it, not the message ID.
|
267
|
+
#
|
268
|
+
# From the SQS Developer Guide:
|
269
|
+
# "It is possible you will receive a message even after you have deleted it. This might happen
|
270
|
+
# on rare occasions if one of the servers storing a copy of the message is unavailable when
|
271
|
+
# you request to delete the message. The copy remains on the server and might be returned to
|
272
|
+
# you again on a subsequent receive request. You should create your system to be
|
273
|
+
# idempotent so that receiving a particular message more than once is not a problem. "
|
274
|
+
#
|
275
|
+
# sqs.delete_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', 'Euvo62/1nlIet...ao03hd9Sa0w==') #=> true
|
276
|
+
#
|
277
|
+
def delete_message(queue_url, receipt_handle)
|
278
|
+
req_hash = generate_request('DeleteMessage', 'ReceiptHandle' => receipt_handle, :queue_url => queue_url)
|
279
|
+
request_info(req_hash, SqsStatusParser.new(:logger => @logger))
|
280
|
+
rescue
|
281
|
+
on_exception
|
282
|
+
end
|
283
|
+
|
284
|
+
# Given the queue's short name, this call returns the queue URL or +nil+ if queue is not found
|
285
|
+
# sqs.queue_url_by_name('my_awesome_queue') #=> 'http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue'
|
286
|
+
#
|
287
|
+
def queue_url_by_name(queue_name)
|
288
|
+
return queue_name if queue_name.include?('/')
|
289
|
+
queue_urls = list_queues(queue_name)
|
290
|
+
queue_urls.each do |queue_url|
|
291
|
+
return queue_url if queue_name_by_url(queue_url) == queue_name
|
292
|
+
end
|
293
|
+
nil
|
294
|
+
rescue
|
295
|
+
on_exception
|
296
|
+
end
|
297
|
+
|
298
|
+
# Returns short queue name by url.
|
299
|
+
#
|
300
|
+
# RightSqs.queue_name_by_url('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> 'my_awesome_queue'
|
301
|
+
#
|
302
|
+
def self.queue_name_by_url(queue_url)
|
303
|
+
queue_url[/[^\/]*$/]
|
304
|
+
rescue
|
305
|
+
on_exception
|
306
|
+
end
|
307
|
+
|
308
|
+
# Returns short queue name by url.
|
309
|
+
#
|
310
|
+
# sqs.queue_name_by_url('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> 'my_awesome_queue'
|
311
|
+
#
|
312
|
+
def queue_name_by_url(queue_url)
|
313
|
+
self.class.queue_name_by_url(queue_url)
|
314
|
+
rescue
|
315
|
+
on_exception
|
316
|
+
end
|
317
|
+
|
318
|
+
# Returns approximate number of messages in queue.
|
319
|
+
#
|
320
|
+
# sqs.get_queue_length('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> 3
|
321
|
+
#
|
322
|
+
def get_queue_length(queue_url)
|
323
|
+
get_queue_attributes(queue_url)['ApproximateNumberOfMessages'].to_i
|
324
|
+
rescue
|
325
|
+
on_exception
|
326
|
+
end
|
327
|
+
|
328
|
+
# Removes all visible messages from queue. Return +true+ or an exception.
|
329
|
+
#
|
330
|
+
# sqs.clear_queue('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> true
|
331
|
+
#
|
332
|
+
def clear_queue(queue_url)
|
333
|
+
while (pop_messages(queue_url, 10).length > 0) ; end # delete all messages in queue
|
334
|
+
true
|
335
|
+
rescue
|
336
|
+
on_exception
|
337
|
+
end
|
338
|
+
|
339
|
+
# 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>.
|
340
|
+
#
|
341
|
+
# sqs.pop_messages('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', 3) #=>
|
342
|
+
# [{"ReceiptHandle"=>"Euvo62/...+Zw==", "MD5OfBody"=>"16af2...81e3", "Body"=>"Goodbyte World!",
|
343
|
+
# "MessageId"=>"MEZI...JSWDE="}, {...}, ... , {...} ]
|
344
|
+
#
|
345
|
+
def pop_messages(queue_url, number_of_messages=1)
|
346
|
+
messages = receive_message(queue_url, number_of_messages)
|
347
|
+
messages.each do |message|
|
348
|
+
delete_message(queue_url, message['ReceiptHandle'])
|
349
|
+
end
|
350
|
+
messages
|
351
|
+
rescue
|
352
|
+
on_exception
|
353
|
+
end
|
354
|
+
|
355
|
+
# Pops (retrieves and deletes) first accessible message from queue. Returns the message in format <tt>{:id=>'message_id', :body=>'message_body'}</tt> or +nil+.
|
356
|
+
#
|
357
|
+
# sqs.pop_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=>
|
358
|
+
# {:id=>"12345678904GEZX9746N|0N9ED344VK5Z3SV1DTM0|1RVYH4X3TJ0987654321", :body=>"message_1"}
|
359
|
+
#
|
360
|
+
def pop_message(queue_url)
|
361
|
+
messages = pop_messages(queue_url)
|
362
|
+
messages.blank? ? nil : messages[0]
|
363
|
+
rescue
|
364
|
+
on_exception
|
365
|
+
end
|
366
|
+
|
367
|
+
#-----------------------------------------------------------------
|
368
|
+
# PARSERS: Status Response Parser
|
369
|
+
#-----------------------------------------------------------------
|
370
|
+
|
371
|
+
class SqsStatusParser < AwsParser # :nodoc:
|
372
|
+
def tagend(name)
|
373
|
+
if name == 'ResponseMetadata'
|
374
|
+
@result = true
|
375
|
+
end
|
376
|
+
end
|
377
|
+
end
|
378
|
+
|
379
|
+
#-----------------------------------------------------------------
|
380
|
+
# PARSERS: Queue
|
381
|
+
#-----------------------------------------------------------------
|
382
|
+
|
383
|
+
class SqsCreateQueueParser < AwsParser # :nodoc:
|
384
|
+
def tagend(name)
|
385
|
+
@result = @text if name == 'QueueUrl'
|
386
|
+
end
|
387
|
+
end
|
388
|
+
|
389
|
+
class SqsListQueuesParser < AwsParser # :nodoc:
|
390
|
+
def reset
|
391
|
+
@result = []
|
392
|
+
end
|
393
|
+
def tagend(name)
|
394
|
+
@result << @text if name == 'QueueUrl'
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
398
|
+
class SqsGetQueueAttributesParser < AwsParser # :nodoc:
|
399
|
+
def reset
|
400
|
+
@result = {}
|
401
|
+
end
|
402
|
+
def tagend(name)
|
403
|
+
case name
|
404
|
+
when 'Name' ; @current_attribute = @text
|
405
|
+
when 'Value' ; @result[@current_attribute] = @text
|
406
|
+
end
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
#-----------------------------------------------------------------
|
411
|
+
# PARSERS: Messages
|
412
|
+
#-----------------------------------------------------------------
|
413
|
+
|
414
|
+
class SqsReceiveMessageParser < AwsParser # :nodoc:
|
415
|
+
def reset
|
416
|
+
@result = []
|
417
|
+
end
|
418
|
+
def tagstart(name, attributes)
|
419
|
+
@current_message = {} if name == 'Message'
|
420
|
+
end
|
421
|
+
def tagend(name)
|
422
|
+
case name
|
423
|
+
when 'MessageId' ; @current_message['MessageId'] = @text
|
424
|
+
when 'ReceiptHandle' ; @current_message['ReceiptHandle'] = @text
|
425
|
+
when 'MD5OfBody' ; @current_message['MD5OfBody'] = @text
|
426
|
+
when 'Body'; @current_message['Body'] = @text; @result << @current_message
|
427
|
+
end
|
428
|
+
end
|
429
|
+
end
|
430
|
+
|
431
|
+
class SqsSendMessagesParser < AwsParser # :nodoc:
|
432
|
+
def reset
|
433
|
+
@result = {}
|
434
|
+
end
|
435
|
+
def tagend(name)
|
436
|
+
case name
|
437
|
+
when 'MessageId' ; @result['MessageId'] = @text
|
438
|
+
when 'MD5OfMessageBody' ; @result['MD5OfMessageBody'] = @text
|
439
|
+
end
|
440
|
+
end
|
441
|
+
end
|
442
|
+
|
443
|
+
end
|
444
|
+
|
445
|
+
end
|