Sha256: da19af7ee9990e47fd28fdaa6e9f9cc61cc5b8a31340fb69f9b06977ebd1987e

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

require 'aws-sdk-sqs'
require 'securerandom'

module Arthropod
  module Client
    class ServerError < StandardError; end

    def self.push(queue_name:, body:, client: nil)
      client ||= Aws::SQS::Client.new

      sender_queue = client.create_queue(queue_name: queue_name)
      return_queue = client.create_queue(queue_name: SecureRandom.uuid.gsub("-", "_"))

      # Send our order with a return queue so we can get responses
      client.send_message(queue_url: sender_queue.queue_url, message_body: JSON.dump({ return_queue_url: return_queue.queue_url, body: body }))

      loop do
        response = client.receive_message(queue_url: return_queue.queue_url, max_number_of_messages: 1, wait_time_seconds: 1)
        response.messages.each do |message|
          response = Arthropod::Response.new(client: client, message: message)
          begin
            if response.state == "close"
              return response
            elsif response.state == "error"
              raise Arthropod::Client::ServerError
            else
              yield response if block_given?
            end
          ensure
            client.delete_message(queue_url: return_queue.queue_url, receipt_handle: message.receipt_handle)
          end
        end
      end
    ensure
      client.delete_queue(queue_url: return_queue.queue_url) if return_queue
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arthropod-0.0.3 lib/arthropod/client.rb
arthropod-0.0.2 lib/arthropod/client.rb