Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions kombu/transport/SQS/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,15 @@
https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-metadata.html

SQS supports sending message attributes along with the message body.
To use this feature, you can pass a 'message_attributes' as keyword argument
to `basic_publish` method.
To use this feature, pass ``message_attributes`` to ``Producer.publish``
(or to Celery's ``apply_async``); it is sent as the SQS ``MessageAttributes``
and kept out of the message body:

.. code-block:: python

producer.publish(body, routing_key='myqueue', message_attributes={
'tenant': {'DataType': 'String', 'StringValue': '42'},
})

Other Features supported by this transport
==========================================
Expand Down Expand Up @@ -665,10 +672,13 @@ def _put(self, queue, message, **kwargs):
q_url = self._new_queue(queue)
kwargs = {'QueueUrl': q_url}
if 'properties' in message:
if 'message_attributes' in message['properties']:
# we don't want to want to have the attribute in the body
kwargs['MessageAttributes'] = \
message['properties'].pop('message_attributes')
properties = message['properties']
if 'message_attributes' in properties:
kwargs['MessageAttributes'] = properties['message_attributes']
message = {**message, 'properties': {
key: value for key, value in properties.items()
if key != 'message_attributes'
}}
# Support SQS fair queue system.
if 'MessageGroupId' in message['properties']:
kwargs['MessageGroupId'] = \
Expand Down
50 changes: 50 additions & 0 deletions t/unit/transport/SQS/test_SQS.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import annotations

import base64
import json
import logging
import os
import random
Expand Down Expand Up @@ -2230,6 +2231,55 @@ def test_fifo_queue_with_custom_message_group_id_and_deduplication_id(self):
assert call_kwargs['MessageGroupId'] == 'tenant-456'
assert call_kwargs['MessageDeduplicationId'] == 'unique-msg-789'

def test_put_keeps_message_attributes_when_the_message_is_sent_again(self):
connection = Connection(transport=SQS.Transport, transport_options={
'predefined_queues': example_predefined_queues,
})
channel = connection.channel()
channel.sqs = Mock()
send_message = channel.sqs.return_value.send_message
attributes = {'tenant': {'DataType': 'String', 'StringValue': '42'}}
message = channel.prepare_message(
'body', properties={'message_attributes': attributes},
)

channel._put('queue-1', message)
channel._put('queue-1', message)

assert [
sent.kwargs['MessageAttributes']
for sent in send_message.call_args_list
] == [attributes, attributes]
assert message['properties']['message_attributes'] == attributes
body = json.loads(
base64.b64decode(send_message.call_args.kwargs['MessageBody'])
)
assert 'message_attributes' not in body['properties']

def test_put_keeps_message_attributes_on_a_retried_publish(self):
connection = Connection(transport=SQS.Transport, transport_options={
'predefined_queues': example_predefined_queues,
})
sqs_client = Mock()
sqs_client.send_message.side_effect = [
EndpointConnectionError(endpoint_url='https://sqs.us-east-1'),
{'MessageId': '1'},
]
attributes = {'tenant': {'DataType': 'String', 'StringValue': '42'}}

with patch.object(SQS.Channel, 'sqs', return_value=sqs_client):
messaging.Producer(connection).publish(
'body', routing_key='queue-1', retry=True,
retry_policy={'max_retries': 1, 'interval_start': 0,
'interval_step': 0, 'interval_max': 0},
message_attributes=attributes,
)

assert [
sent.kwargs['MessageAttributes']
for sent in sqs_client.send_message.call_args_list
] == [attributes, attributes]

def test_message_group_id_with_special_characters(self):
"""Test MessageGroupId with special characters"""
connection = Connection(transport=SQS.Transport, transport_options={
Expand Down