Class: BetterSqs::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/better_sqs/client.rb

Overview

A class that wraps the aws sdk v2 SQS client to reduce interface complexity

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



7
8
9
10
# File 'lib/better_sqs/client.rb', line 7

def initialize
  # if BetterSqs has not been configured then run the default configuration
  BetterSqs.configure unless BetterSqs.configured?
end

Instance Attribute Details

#sqsAws::SQS::Client

Get the existing or create a new instances of the SQS client

Returns:

  • (Aws::SQS::Client)

    an instance of the SQS client



52
53
54
# File 'lib/better_sqs/client.rb', line 52

def sqs
  @sqs
end

Instance Method Details

#defer_retry(message) ⇒ Object

Updates the message visibility timeout to create some delay before an attempt will be made to reprocess the

message

Parameters:

  • message (Messages::Sqs)

    the message for which the next retry should be delayed



43
44
45
46
47
# File 'lib/better_sqs/client.rb', line 43

def defer_retry(message)
  sqs.change_message_visibility queue_url:          url_for_queue(message.queue),
                                receipt_handle:     message.receipt_handle,
                                visibility_timeout: BetterSqs.configuration.sqs_message_deferral_seconds
end

#delete(message) ⇒ Object

Delete a message from the queue

Parameters:

  • message (Messages::Sqs)

    the message that should be deleted



35
36
37
# File 'lib/better_sqs/client.rb', line 35

def delete(message)
  sqs.delete_message queue_url: url_for_queue(message.queue), receipt_handle: message.receipt_handle
end

#push(queue_name, message_body) ⇒ Types::SendMessageResult

Push a message onto a queue

Parameters:

  • queue_name (String, Symbol)

    the name of the queue that the message should pushed onto

  • message_body (String)

    the message as it will be pushed onto the queue, no serialization occurs as part of this method. You need to encode or serialize your object to a string before sending it to this method

Returns:

  • (Types::SendMessageResult)

    the sent message object returned from s3



18
19
20
# File 'lib/better_sqs/client.rb', line 18

def push(queue_name, message_body)
  sqs.send_message(queue_url: url_for_queue(queue_name), message_body: message_body)
end

#reserve(queue_name) ⇒ Messages::Sqs, NilClass

Reserve a message from the specified queue

Parameters:

  • queue_name (String, Symbol)

    the name of the SQS queue to reserve a message from

Returns:

  • (Messages::Sqs, NilClass)

    the message retrieved from the queue



26
27
28
29
30
# File 'lib/better_sqs/client.rb', line 26

def reserve(queue_name)
  resp = sqs.receive_message(queue_url: url_for_queue(queue_name), max_number_of_messages: 1)
  return nil unless resp.messages.any?
  Message.new queue_client: self, queue: queue_name, sqs_message: resp.messages.first
end

#url_for_queue(queue_name) ⇒ AWS::SQS::Queue

Get the specified queue instance if it already exists, otherwise create it and wait for it to be readied

Parameters:

  • queue_name (String, Symbol)

    the name of the queue to be created

Returns:

  • (AWS::SQS::Queue)

    the requested queue instance



60
61
62
# File 'lib/better_sqs/client.rb', line 60

def url_for_queue(queue_name)
  sqs.create_queue(queue_name: queue_name).queue_url
end