Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Make Saleor webhooks handle SQS FIFO queues

Piotr Blaszczyk479700b856c9
diff --git a/saleor/plugins/webhook/tasks.py b/saleor/plugins/webhook/tasks.py
index 25ea59bad6..52fb3bd3b4 100644
--- a/saleor/plugins/webhook/tasks.py
+++ b/saleor/plugins/webhook/tasks.py
@@ -79,6 +79,7 @@ def send_webhook_using_aws_sqs(target_url, message, domain, signature, event_typ
queue_url = urlunparse(
("https", parts.hostname, parts.path, parts.params, parts.query, parts.fragment)
)
+ is_fifo = parts.path.endswith(".fifo")
msg_attributes = {
"SaleorDomain": {"DataType": "String", "StringValue": domain},
@@ -86,11 +87,15 @@ def send_webhook_using_aws_sqs(target_url, message, domain, signature, event_typ
}
if signature:
msg_attributes["Signature"] = {"DataType": "String", "StringValue": signature}
- client.send_message(
- QueueUrl=queue_url,
- MessageAttributes=msg_attributes,
- MessageBody=message.decode("utf-8"),
- )
+
+ message_kwargs = {
+ "QueueUrl": queue_url,
+ "MessageAttributes": msg_attributes,
+ "MessageBody": message.decode("utf-8"),
+ }
+ if is_fifo:
+ message_kwargs["MessageGroupId"] = domain
+ client.send_message(**message_kwargs)
def send_webhook_using_google_cloud_pubsub(
diff --git a/saleor/plugins/webhook/tests/test_webhook_protocols.py b/saleor/plugins/webhook/tests/test_webhook_protocols.py
index 7db8f14433..dcd207e0fa 100644
--- a/saleor/plugins/webhook/tests/test_webhook_protocols.py
+++ b/saleor/plugins/webhook/tests/test_webhook_protocols.py
@@ -12,7 +12,13 @@ from ...webhook import signature_for_payload
from ...webhook.tasks import trigger_webhooks_for_event
+ "queue_name, additional_call_args",
+ (("queue_name", {}), ("queue_name.fifo", {"MessageGroupId": "mirumee.com"})),
+)
def test_trigger_webhooks_with_aws_sqs(
+ queue_name,
+ additional_call_args,
webhook,
order_with_lines,
permission_manage_orders,
@@ -35,7 +41,7 @@ def test_trigger_webhooks_with_aws_sqs(
webhook.target_url = (
f"awssqs://{access_key}:{secret_key}@sqs.{region}.amazonaws.com/account_id/"
- f"queue_name"
+ f"{queue_name}"
)
webhook.save()
@@ -49,14 +55,16 @@ def test_trigger_webhooks_with_aws_sqs(
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
)
- mocked_client.send_message.assert_called_once_with(
- QueueUrl="https://sqs.us-east-1.amazonaws.com/account_id/queue_name",
- MessageAttributes={
+ expected_call_args = {
+ "QueueUrl": f"https://sqs.us-east-1.amazonaws.com/account_id/{queue_name}",
+ "MessageAttributes": {
"SaleorDomain": {"DataType": "String", "StringValue": "mirumee.com"},
"EventType": {"DataType": "String", "StringValue": "order_created"},
},
- MessageBody=expected_data,
- )
+ "MessageBody": expected_data,
+ }
+ expected_call_args.update(additional_call_args)
+ mocked_client.send_message.assert_called_once_with(**expected_call_args)
def test_trigger_webhooks_with_aws_sqs_and_secret_key(