File: sqs_interface.rb

package info (click to toggle)
ruby-aws 2.10.2-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 748 kB
  • sloc: ruby: 7,748; makefile: 16
file content (483 lines) | stat: -rw-r--r-- 19,662 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#
# Copyright (c) 2007-2008 RightScale Inc
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

module Aws

  #
  # Right::Aws::SqsInterface - RightScale's low-level Amazon SQS interface
  # for API version 2008-01-01 and later.
  # For explanations of the semantics
  # of each call, please refer to Amazon's documentation at
  # http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=31
  #
  # This class provides a procedural interface to SQS.  Conceptually it is
  # mostly a pass-through interface to SQS and its API is very similar to the
  # bare SQS API.  For a somewhat higher-level and object-oriented interface, see
  # Aws::Sqs.

  class SqsInterface < AwsBase
    include AwsBaseInterface

    API_VERSION                = "2009-02-01"
    DEFAULT_HOST               = "queue.amazonaws.com"
    DEFAULT_PORT               = 443
    DEFAULT_PROTOCOL           = 'https'
    REQUEST_TTL                = 30
    DEFAULT_VISIBILITY_TIMEOUT = 30


    def self.connection_name
      :sqs_connection
    end

    @@bench = AwsBenchmarkingBlock.new

    def self.bench
      @@bench
    end

    def self.bench_xml
      @@bench.xml
    end

    def self.bench_sqs
      @@bench.service
    end

    @@api = API_VERSION

    def self.api
      @@api
    end

    # Creates a new SqsInterface instance. This instance is limited to
    # operations on SQS objects created with Amazon's 2008-01-01 API version.  This
    # interface will not work on objects created with prior API versions.  See
    # Amazon's article "Migrating to Amazon SQS API version 2008-01-01" at:
    # http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1148
    #
    #  sqs = Aws::SqsInterface.new('1E3GDYEOGFJPIT75KDT40','hgTHt68JY07JKUY08ftHYtERkjgtfERn57DFE379', {:multi_thread => true, :logger => Logger.new('/tmp/x.log')})
    #
    # Params is a hash:
    #
    #    {:server       => 'queue.amazonaws.com' # Amazon service host: 'queue.amazonaws.com' (default)
    #     :port         => 443                   # Amazon service port: 80 or 443 (default)
    #     :multi_thread => true|false            # Multi-threaded (connection per each thread): true or false (default)
    #     :signature_version => '0'              # The signature version : '0' or '1'(default)
    #     :logger       => Logger Object}        # Logger instance: logs to STDOUT if omitted }
    #
    def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
      init({:name             => 'SQS',
            :default_host     => ENV['SQS_URL'] ? URI.parse(ENV['SQS_URL']).host : DEFAULT_HOST,
            :default_port     => ENV['SQS_URL'] ? URI.parse(ENV['SQS_URL']).port : DEFAULT_PORT,
            :default_protocol => ENV['SQS_URL'] ? URI.parse(ENV['SQS_URL']).scheme : DEFAULT_PROTOCOL,
            :api_version      => API_VERSION},
           aws_access_key_id || ENV['AWS_ACCESS_KEY_ID'],
           aws_secret_access_key || ENV['AWS_SECRET_ACCESS_KEY'],
           params)
    end


    #-----------------------------------------------------------------
    #      Requests
    #-----------------------------------------------------------------

    # Generates a request hash for the query API
    def generate_request(action, param={}) # :nodoc:
      # For operation requests on a queue, the queue URI will be a parameter,
      # so we first extract it from the call parameters.  Next we remove any
      # parameters with no value or with symbolic keys.  We add the header
      # fields required in all requests, and then the headers passed in as
      # params.  We sort the header fields alphabetically and then generate the
      # signature before URL escaping the resulting query and sending it.
      service = param[:queue_url] ? URI(param[:queue_url]).path : '/'
      param.each { |key, value| param.delete(key) if (value.nil? || key.is_a?(Symbol)) }
      service_hash = {"Action"         => action,
                      "Expires"        => (Time.now + REQUEST_TTL).utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
                      "AWSAccessKeyId" => @aws_access_key_id,
                      "Version"        => API_VERSION}
      service_hash.update(param)
      service_params = signed_service_params(@aws_secret_access_key, service_hash, :get, @params[:server], service)
      request        = Net::HTTP::Get.new("#{Utils.URLencode(service)}?#{service_params}")
      # prepare output hash
      {:request  => request,
       :server   => @params[:server],
       :port     => @params[:port],
       :protocol => @params[:protocol]}
    end

    def generate_post_request(action, param={}) # :nodoc:
      service = param[:queue_url] ? URI(param[:queue_url]).path : '/'
      message = param[:message] # extract message body if nesessary
      param.each { |key, value| param.delete(key) if (value.nil? || key.is_a?(Symbol)) }
      service_hash = {"Action"         => action,
                      "Expires"        => (Time.now + REQUEST_TTL).utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
                      "AWSAccessKeyId" => @aws_access_key_id,
                      "MessageBody"    => message,
                      "Version"        => API_VERSION}
      service_hash.update(param)
      #
      service_params          = signed_service_params(@aws_secret_access_key, service_hash, :post, @params[:server], service)
      request                 = Net::HTTP::Post.new(Utils::URLencode(service))
      request['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
      request.body            = service_params
      # prepare output hash
      {:request  => request,
       :server   => @params[:server],
       :port     => @params[:port],
       :protocol => @params[:protocol]}
    end


    # Sends request to Amazon and parses the response
    # Raises AwsError if any banana happened
# todo: remove this and switch to using request_info2
    def request_info(request, parser, options={}) # :nodoc:
      conn = get_conn(self.class.connection_name, @params, @logger)
      request_info_impl(conn, @@bench, request, parser, options)
    end

    # Creates a new queue, returning its URI.
    #
    #  sqs.create_queue('my_awesome_queue') #=> 'http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue'
    #
    def create_queue(queue_name, default_visibility_timeout=nil)
      req_hash = generate_request('CreateQueue', 'QueueName' => queue_name,
                                  'DefaultVisibilityTimeout' => default_visibility_timeout || DEFAULT_VISIBILITY_TIMEOUT)
      request_info(req_hash, SqsCreateQueueParser.new(:logger => @logger))
    rescue
      on_exception
    end

    # Lists all queues owned by this user that have names beginning with +queue_name_prefix+.
    # If +queue_name_prefix+ is omitted then retrieves a list of all queues.
    # Queue creation is an eventual operation and created queues may not show up in immediately subsequent list_queues calls.
    #
    #  sqs.create_queue('my_awesome_queue')
    #  sqs.create_queue('my_awesome_queue_2')
    #  sqs.list_queues('my_awesome') #=> ['http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue','http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue_2']
    #
    def list_queues(queue_name_prefix=nil)
      req_hash = generate_request('ListQueues', 'QueueNamePrefix' => queue_name_prefix)
      request_info(req_hash, SqsListQueuesParser.new(:logger => @logger))
    rescue
      on_exception
    end

    # Deletes queue. Any messages in the queue are permanently lost.
    # Returns +true+ or an exception.
    # Queue deletion can take up to 60 s to propagate through SQS.  Thus, after a deletion, subsequent list_queues calls
    # may still show the deleted queue.  It is not unusual within the 60 s window to see the deleted queue absent from
    # one list_queues call but present in the subsequent one.  Deletion is eventual.
    #
    #  sqs.delete_queue('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue_2') #=> true
    #
    #
    def delete_queue(queue_url)
      req_hash = generate_request('DeleteQueue', :queue_url => queue_url)
      request_info(req_hash, SqsStatusParser.new(:logger => @logger))
    rescue
      on_exception
    end

    # Retrieves the queue attribute(s). Returns a hash of attribute(s) or an exception.
    #
    #  sqs.get_queue_attributes('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue')
    #  #=> {"ApproximateNumberOfMessages"=>"0", "VisibilityTimeout"=>"30"}
    #
    def get_queue_attributes(queue_url, attribute='All')
      req_hash = generate_request('GetQueueAttributes', 'AttributeName' => attribute, :queue_url => queue_url)
      request_info(req_hash, SqsGetQueueAttributesParser.new(:logger => @logger))
    rescue
      on_exception
    end

    # Sets queue attribute. Returns +true+ or an exception.
    #
    #  sqs.set_queue_attributes('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', "VisibilityTimeout", 10) #=> true
    #
    # From the SQS Dev Guide:
    # "Currently, you can set only the
    # VisibilityTimeout attribute for a queue...
    # When you change a queue's attributes, the change can take up to 60 seconds to propagate
    # throughout the SQS system."
    #
    # NB: Attribute values may not be immediately available to other queries
    # for some time after an update. See the SQS documentation for
    # semantics, but in general propagation can take up to 60 s.
    def set_queue_attributes(queue_url, attribute, value)
      req_hash = generate_request('SetQueueAttributes', 'Attribute.Name' => attribute, 'Attribute.Value' => value, :queue_url => queue_url)
      request_info(req_hash, SqsStatusParser.new(:logger => @logger))
    rescue
      on_exception
    end

    # Retrieves a list of messages from queue. Returns an array of hashes in format: <tt>{:id=>'message_id', body=>'message_body'}</tt>
    #
    #   sqs.receive_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue',10, 5) #=>
    #    [{"ReceiptHandle"=>"Euvo62...kw==", "MD5OfBody"=>"16af2171b5b83cfa35ce254966ba81e3",
    #      "Body"=>"Goodbyte World!", "MessageId"=>"MUM4WlAyR...pYOTA="}, ..., {}]
    #
    # Normally this call returns fewer messages than the maximum specified,
    # even if they are available.
    #
    def receive_message(queue_url, max_number_of_messages=1, visibility_timeout=nil)
      return [] if max_number_of_messages == 0
      req_hash = generate_post_request('ReceiveMessage', 'MaxNumberOfMessages' => max_number_of_messages, 'VisibilityTimeout' => visibility_timeout,
                                       :queue_url                              => queue_url)
      request_info(req_hash, SqsReceiveMessageParser.new(:logger => @logger))
    rescue
      on_exception
    end

    # Sends a new message to a queue.  Message size is limited to 8 KB.
    # If successful, this call returns a hash containing key/value pairs for
    # "MessageId" and "MD5OfMessageBody":
    #
    #  sqs.send_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', 'message_1') #=> "1234567890...0987654321"
    #  => {"MessageId"=>"MEs4M0JKNlRCRTBBSENaMjROTk58QVFRNzNEREhDVFlFOVJDQ1JKNjF8UTdBRllCUlJUMjhKMUI1WDJSWDE=", "MD5OfMessageBody"=>"16af2171b5b83cfa35ce254966ba81e3"}
    #
    # On failure, send_message raises an exception.
    #
    #
    def send_message(queue_url, message)
      req_hash = generate_post_request('SendMessage', :message => message, :queue_url => queue_url)
      request_info(req_hash, SqsSendMessagesParser.new(:logger => @logger))
    rescue
      on_exception
    end

    # Same as send_message
    alias_method :push_message, :send_message


    # Deletes message from queue. Returns +true+ or an exception.  Amazon
    # returns +true+ on deletion of non-existent messages.  You must use the
    # receipt handle for a message to delete it, not the message ID.
    #
    # From the SQS Developer Guide:
    # "It is possible you will receive a message even after you have deleted it. This might happen
    # on rare occasions if one of the servers storing a copy of the message is unavailable when
    # you request to delete the message. The copy remains on the server and might be returned to
    # you again on a subsequent receive request. You should create your system to be
    # idempotent so that receiving a particular message more than once is not a problem. "
    #
    #  sqs.delete_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', 'Euvo62/1nlIet...ao03hd9Sa0w==') #=> true
    #
    def delete_message(queue_url, receipt_handle)
      req_hash = generate_request('DeleteMessage', 'ReceiptHandle' => receipt_handle, :queue_url => queue_url)
      request_info(req_hash, SqsStatusParser.new(:logger => @logger))
    rescue
      on_exception
    end

    # Sets new visibility timeout value for a message identified by "receipt_handle"
    # Check out the Amazon SQS API documentation for further details.
    def change_message_visibility(queue_url, receipt_handle, visibility_timeout)
      req_hash = generate_request(
          "ChangeMessageVisibility",
          "ReceiptHandle"     => receipt_handle,
          "VisibilityTimeout" => visibility_timeout,
          :queue_url          => queue_url
      )
      request_info(req_hash, SqsStatusParser.new(:logger => @logger))
    rescue
      on_exception
    end

    # Given the queue's short name, this call returns the queue URL or +nil+ if queue is not found
    #  sqs.queue_url_by_name('my_awesome_queue') #=> 'http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue'
    #
    def queue_url_by_name(queue_name)
      return queue_name if queue_name.include?('/')
      queue_urls = list_queues(queue_name)
      queue_urls.each do |queue_url|
        return queue_url if queue_name_by_url(queue_url) == queue_name
      end
      nil
    rescue
      on_exception
    end

    # Returns short queue name by url.
    #
    #  RightSqs.queue_name_by_url('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> 'my_awesome_queue'
    #
    def self.queue_name_by_url(queue_url)
      queue_url[/[^\/]*$/]
#    rescue
#      on_exception
    end

    # Returns short queue name by url.
    #
    #  sqs.queue_name_by_url('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> 'my_awesome_queue'
    #
    def queue_name_by_url(queue_url)
      self.class.queue_name_by_url(queue_url)
    rescue
      on_exception
    end

    # Returns approximate number of messages in queue.
    #
    #  sqs.get_queue_length('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> 3
    #
    def get_queue_length(queue_url)
      get_queue_attributes(queue_url)['ApproximateNumberOfMessages'].to_i
    rescue
      on_exception
    end

    # Removes all visible messages from queue. Return +true+ or an exception.
    #
    #  sqs.clear_queue('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=> true
    #
    def clear_queue(queue_url)
      while (pop_messages(queue_url, 10).length > 0);
      end # delete all messages in queue
      true
    rescue
      on_exception
    end

    # 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>.
    #
    #   sqs.pop_messages('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue', 3) #=>
    #   [{"ReceiptHandle"=>"Euvo62/...+Zw==", "MD5OfBody"=>"16af2...81e3", "Body"=>"Goodbyte World!",
    #   "MessageId"=>"MEZI...JSWDE="}, {...}, ... , {...} ]
    #
    def pop_messages(queue_url, number_of_messages=1)
      messages = receive_message(queue_url, number_of_messages)
      messages.each do |message|
        delete_message(queue_url, message['ReceiptHandle'])
      end
      messages
    rescue
      on_exception
    end

    # Pops (retrieves and  deletes) first accessible message from queue. Returns the message in format <tt>{:id=>'message_id', :body=>'message_body'}</tt> or +nil+.
    #
    #  sqs.pop_message('http://queue.amazonaws.com/ZZ7XXXYYYBINS/my_awesome_queue') #=>
    #    {:id=>"12345678904GEZX9746N|0N9ED344VK5Z3SV1DTM0|1RVYH4X3TJ0987654321", :body=>"message_1"}
    #
    def pop_message(queue_url)
      messages = pop_messages(queue_url)
      messages.nil? ? nil : messages[0]
    rescue
      on_exception
    end

    #-----------------------------------------------------------------
    #      PARSERS: Status Response Parser
    #-----------------------------------------------------------------

    class SqsStatusParser < AwsParser # :nodoc:
      def tagend(name)
        if name == 'ResponseMetadata'
          @result = true
        end
      end
    end

    #-----------------------------------------------------------------
    #      PARSERS: Queue
    #-----------------------------------------------------------------

    class SqsCreateQueueParser < AwsParser # :nodoc:
      def tagend(name)
        @result = @text if name == 'QueueUrl'
      end
    end

    class SqsListQueuesParser < AwsParser # :nodoc:
      def reset
        @result = []
      end

      def tagend(name)
        @result << @text if name == 'QueueUrl'
      end
    end

    class SqsGetQueueAttributesParser < AwsParser # :nodoc:
      def reset
        @result = {}
      end

      def tagend(name)
        case name
          when 'Name';
            @current_attribute = @text
          when 'Value';
            @result[@current_attribute] = @text
        end
      end
    end

    #-----------------------------------------------------------------
    #      PARSERS: Messages
    #-----------------------------------------------------------------

    class SqsReceiveMessageParser < AwsParser # :nodoc:
      def reset
        @result = []
      end

      def tagstart(name, attributes)
        @current_message = {} if name == 'Message'
      end

      def tagend(name)
        case name
          when 'MessageId';
            @current_message['MessageId'] = @text
          when 'ReceiptHandle';
            @current_message['ReceiptHandle'] = @text
          when 'MD5OfBody';
            @current_message['MD5OfBody'] = @text
          when 'Body';
            @current_message['Body'] = @text; @result << @current_message
        end
      end
    end

    class SqsSendMessagesParser < AwsParser # :nodoc:
      def reset
        @result = {}
      end

      def tagend(name)
        case name
          when 'MessageId';
            @result['MessageId'] = @text
          when 'MD5OfMessageBody';
            @result['MD5OfMessageBody'] = @text
        end
      end
    end

  end

end