Skip to content

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

Control webhook queue name based on webhook protocol (#16311)

Maciej Korycinski13808b88e251
diff --git a/saleor/plugins/webhook/tests/test_webhook_protocols.py b/saleor/plugins/webhook/tests/test_webhook_protocols.py
index b381e5c209..a7cad306c3 100644
--- a/saleor/plugins/webhook/tests/test_webhook_protocols.py
+++ b/saleor/plugins/webhook/tests/test_webhook_protocols.py
@@ -1,4 +1,5 @@
from datetime import timedelta
+from unittest import mock
from unittest.mock import MagicMock, patch
import boto3
@@ -8,6 +9,7 @@ from django.core.serializers import serialize
from google.cloud.pubsub_v1 import PublisherClient
from requests_hardened import HTTPSession
+from ....core.models import EventDelivery
from ....webhook.event_types import WebhookEventAsyncType
from ....webhook.transport import signature_for_payload
from ....webhook.transport.asynchronous.transport import trigger_webhooks_async
@@ -418,3 +420,59 @@ def test_trigger_webhooks_with_http_and_custom_headers(
# then
mock_request.assert_called_once()
assert mock_request.call_args[1]["headers"] == expected_headers
+
+
+ ("expected_queue_name", "target_url"),
+ [
+ ("PUBSUB_QUEUE", "gcpubsub://cloud.google.com/projects/saleor/topics/test"),
+ (
+ "SQS_QUEUE",
+ "awssqs://key:[email protected]/xxxx/myqueue.fifo",
+ ),
+ (None, "https://localhost:8888/webhook-endpoint/"),
+ ],
+)
+ "saleor.webhook.transport.asynchronous.transport.send_webhook_request_async.apply_async"
+)
+def test_trigger_webhooks_async_pick_up_queue_based_on_protocol(
+ mock_async_apply,
+ expected_queue_name,
+ target_url,
+ app,
+ order_with_lines,
+ permission_manage_orders,
+ settings,
+ webhook,
+ django_assert_num_queries,
+):
+ # given
+ settings.WEBHOOK_CELERY_QUEUE_NAME = None
+ settings.WEBHOOK_SQS_CELERY_QUEUE_NAME = "SQS_QUEUE"
+ settings.WEBHOOK_PUBSUB_CELERY_QUEUE_NAME = "PUBSUB_QUEUE"
+
+ webhook.app.permissions.add(permission_manage_orders)
+ webhook.target_url = target_url
+ webhook.save()
+
+ expected_data = serialize("json", [order_with_lines])
+
+ # when
+ with django_assert_num_queries(2):
+ trigger_webhooks_async(
+ expected_data,
+ WebhookEventAsyncType.ORDER_CREATED,
+ [webhook],
+ allow_replica=False,
+ )
+
+ # then
+ delivery = EventDelivery.objects.get()
+ mock_async_apply.assert_called_once_with(
+ kwargs={"event_delivery_id": delivery.id},
+ queue=expected_queue_name,
+ bind=True,
+ retry_backoff=10,
+ retry_kwargs={"max_retries": 5},
+ )
diff --git a/saleor/settings.py b/saleor/settings.py
index 201940222f..a0ee2ab9da 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -898,6 +898,13 @@ UPDATE_SEARCH_VECTOR_INDEX_QUEUE_NAME = os.environ.get(
)
# Queue name for "async webhook" events
WEBHOOK_CELERY_QUEUE_NAME = os.environ.get("WEBHOOK_CELERY_QUEUE_NAME", None)
+WEBHOOK_SQS_CELERY_QUEUE_NAME = os.environ.get(
+ "WEBHOOK_SQS_CELERY_QUEUE_NAME", WEBHOOK_CELERY_QUEUE_NAME
+)
+WEBHOOK_PUBSUB_CELERY_QUEUE_NAME = os.environ.get(
+ "WEBHOOK_PUBSUB_CELERY_QUEUE_NAME", WEBHOOK_CELERY_QUEUE_NAME
+)
+
CHECKOUT_WEBHOOK_EVENTS_CELERY_QUEUE_NAME = os.environ.get(
"CHECKOUT_WEBHOOK_EVENTS_CELERY_QUEUE_NAME", WEBHOOK_CELERY_QUEUE_NAME
)
diff --git a/saleor/webhook/transport/asynchronous/transport.py b/saleor/webhook/transport/asynchronous/transport.py
index d9ca71982f..c74df3888d 100644
--- a/saleor/webhook/transport/asynchronous/transport.py
+++ b/saleor/webhook/transport/asynchronous/transport.py
@@ -164,6 +164,16 @@ def create_event_delivery_list_for_webhooks(
return event_deliveries
+def get_queue_name_for_webhook(webhook, default_queue):
+ return {
+ WebhookSchemes.AWS_SQS: settings.WEBHOOK_SQS_CELERY_QUEUE_NAME,
+ WebhookSchemes.GOOGLE_CLOUD_PUBSUB: settings.WEBHOOK_PUBSUB_CELERY_QUEUE_NAME,
+ }.get(
+ urlparse(webhook.target_url).scheme.lower(),
+ default_queue,
+ )
+
+
def trigger_webhooks_async(
data, # deprecated, legacy_data_generator should be used instead
event_type,
@@ -218,11 +228,13 @@ def trigger_webhooks_async(
request_time=request_time,
)
)
-
for delivery in deliveries:
send_webhook_request_async.apply_async(
kwargs={"event_delivery_id": delivery.id},
- queue=queue or settings.WEBHOOK_CELERY_QUEUE_NAME,
+ queue=get_queue_name_for_webhook(
+ delivery.webhook,
+ default_queue=queue or settings.WEBHOOK_CELERY_QUEUE_NAME,
+ ),
bind=True,
retry_backoff=10,
retry_kwargs={"max_retries": 5},