Class: Shoryuken::Message

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/shoryuken/message.rb

Overview

Represents an SQS message received by a Shoryuken worker. This class wraps the raw AWS SQS message data and provides convenient methods for interacting with the message, including deletion and visibility timeout management.

Message instances are automatically created by Shoryuken and passed to your worker’s ‘perform` method as the first argument.

Examples:

Basic worker with message handling

class MyWorker
  include Shoryuken::Worker
  shoryuken_options queue: 'my_queue'

  def perform(sqs_msg, body)
    puts "Processing message #{sqs_msg.message_id}"
    puts "Message body: #{body}"
    puts "Queue: #{sqs_msg.queue_name}"

    # Process the message...

    # Delete the message when done (if auto_delete is false)
    sqs_msg.delete unless auto_delete?
  end
end

Working with message attributes

def perform(sqs_msg, body)
  # Access standard SQS attributes
  sender_id = sqs_msg.attributes['SenderId']
  sent_timestamp = sqs_msg.attributes['SentTimestamp']

  # Access custom message attributes
  priority = sqs_msg.message_attributes['Priority']&.[]('StringValue')
  user_id = sqs_msg.message_attributes['UserId']&.[]('StringValue')
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, queue, data) ⇒ Message

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new Message instance wrapping SQS message data.

Parameters:

  • client (Aws::SQS::Client)

    The SQS client for message operations

  • queue (Shoryuken::Queue)

    The queue this message came from

  • data (Aws::SQS::Types::Message)

    The raw SQS message data



95
96
97
98
99
100
# File 'lib/shoryuken/message.rb', line 95

def initialize(client, queue, data)
  self.client     = client
  self.data       = data
  self.queue_url  = queue.url
  self.queue_name = queue.name
end

Instance Attribute Details

#clientAws::SQS::Client

Returns The SQS client used for message operations.

Returns:

  • (Aws::SQS::Client)

    The SQS client used for message operations



78
79
80
# File 'lib/shoryuken/message.rb', line 78

def client
  @client
end

#dataAws::SQS::Types::Message

Returns The raw SQS message data.

Returns:

  • (Aws::SQS::Types::Message)

    The raw SQS message data



87
88
89
# File 'lib/shoryuken/message.rb', line 87

def data
  @data
end

#queue_nameString

Returns The name of the queue this message came from.

Returns:

  • (String)

    The name of the queue this message came from



84
85
86
# File 'lib/shoryuken/message.rb', line 84

def queue_name
  @queue_name
end

#queue_urlString

Returns The URL of the SQS queue this message came from.

Returns:

  • (String)

    The URL of the SQS queue this message came from



81
82
83
# File 'lib/shoryuken/message.rb', line 81

def queue_url
  @queue_url
end

Instance Method Details

#attributesHash

Returns the SQS message attributes (system attributes).

Returns:

  • (Hash)

    System attributes like SenderId, SentTimestamp, etc.



68
69
70
71
72
73
74
75
# File 'lib/shoryuken/message.rb', line 68

def_delegators(:data,
:message_id,
:receipt_handle,
:md5_of_body,
:body,
:attributes,
:md5_of_message_attributes,
:message_attributes)

#bodyString

Returns the raw message body as received from SQS.

Returns:

  • (String)

    The raw message body



68
69
70
71
72
73
74
75
# File 'lib/shoryuken/message.rb', line 68

def_delegators(:data,
:message_id,
:receipt_handle,
:md5_of_body,
:body,
:attributes,
:md5_of_message_attributes,
:message_attributes)

#change_visibility(options) ⇒ Aws::SQS::Types::ChangeMessageVisibilityResult

Changes the visibility timeout of this message with additional options. This allows you to hide the message from other consumers for a longer or shorter period.

Examples:

Extending visibility with additional options

sqs_msg.change_visibility(visibility_timeout: 300)

Parameters:

  • options (Hash)

    Options to pass to change_message_visibility

Options Hash (options):

  • :visibility_timeout (Integer)

    New visibility timeout in seconds

Returns:

  • (Aws::SQS::Types::ChangeMessageVisibilityResult)

    The change result

Raises:

  • (Aws::SQS::Errors::ServiceError)

    If the change fails

See Also:



127
128
129
130
131
# File 'lib/shoryuken/message.rb', line 127

def change_visibility(options)
  client.change_message_visibility(
    options.merge(queue_url: queue_url, receipt_handle: data.receipt_handle)
  )
end

#deleteAws::SQS::Types::DeleteMessageResult

Deletes this message from the SQS queue. Once deleted, the message will not be redelivered and cannot be retrieved again. This is typically called after successful message processing when auto_delete is disabled.

Returns:

  • (Aws::SQS::Types::DeleteMessageResult)

    The deletion result

Raises:

  • (Aws::SQS::Errors::ServiceError)

    If the deletion fails



108
109
110
111
112
113
# File 'lib/shoryuken/message.rb', line 108

def delete
  client.delete_message(
    queue_url: queue_url,
    receipt_handle: data.receipt_handle
  )
end

#md5_of_bodyString

Returns the MD5 hash of the message body.

Returns:

  • (String)

    MD5 hash of the message body



68
69
70
71
72
73
74
75
# File 'lib/shoryuken/message.rb', line 68

def_delegators(:data,
:message_id,
:receipt_handle,
:md5_of_body,
:body,
:attributes,
:md5_of_message_attributes,
:message_attributes)

#md5_of_message_attributesString

Returns the MD5 hash of the message attributes.

Returns:

  • (String)

    MD5 hash of message attributes



68
69
70
71
72
73
74
75
# File 'lib/shoryuken/message.rb', line 68

def_delegators(:data,
:message_id,
:receipt_handle,
:md5_of_body,
:body,
:attributes,
:md5_of_message_attributes,
:message_attributes)

#message_attributesHash

Returns custom message attributes set by the sender.

Returns:

  • (Hash)

    Custom message attributes with typed values



68
69
70
71
72
73
74
75
# File 'lib/shoryuken/message.rb', line 68

def_delegators(:data,
:message_id,
:receipt_handle,
:md5_of_body,
:body,
:attributes,
:md5_of_message_attributes,
:message_attributes)

#message_idString

Returns the unique SQS message ID.

Returns:

  • (String)

    The message ID assigned by SQS



68
69
70
71
72
73
74
75
# File 'lib/shoryuken/message.rb', line 68

def_delegators(:data,
:message_id,
:receipt_handle,
:md5_of_body,
:body,
:attributes,
:md5_of_message_attributes,
:message_attributes)

#receipt_handleString

Returns the receipt handle needed for deleting or modifying the message.

Returns:

  • (String)

    The receipt handle for this message



68
69
70
71
72
73
74
75
# File 'lib/shoryuken/message.rb', line 68

def_delegators(:data,
:message_id,
:receipt_handle,
:md5_of_body,
:body,
:attributes,
:md5_of_message_attributes,
:message_attributes)

#visibility_timeout=(timeout) ⇒ Aws::SQS::Types::ChangeMessageVisibilityResult

Sets the visibility timeout for this message. This is a convenience method for changing only the visibility timeout.

Examples:

Extending processing time

def perform(sqs_msg, body)
  if complex_processing_needed?(body)
    sqs_msg.visibility_timeout = 1800  # 30 minutes
  end

  process_message(body)
end

Making message immediately visible again

sqs_msg.visibility_timeout = 0  # Make visible immediately

Parameters:

  • timeout (Integer)

    New visibility timeout in seconds (0-43200)

Returns:

  • (Aws::SQS::Types::ChangeMessageVisibilityResult)

    The change result

Raises:

  • (Aws::SQS::Errors::ServiceError)

    If the change fails



151
152
153
154
155
156
157
# File 'lib/shoryuken/message.rb', line 151

def visibility_timeout=(timeout)
  client.change_message_visibility(
    queue_url: queue_url,
    receipt_handle: data.receipt_handle,
    visibility_timeout: timeout
  )
end