Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Prevent sending sync webhook if the previous one failed recently (#18063) (#18079)
Cezary Miącz954887b39f13
diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.pyindex a9ef8a641b..b4b92396dd 100644--- a/saleor/plugins/webhook/plugin.py+++ b/saleor/plugins/webhook/plugin.py@@ -62,7 +62,7 @@ from ...payment.utils import ()from ...settings import WEBHOOK_SYNC_TIMEOUTfrom ...thumbnail.models import Thumbnail-from ...webhook.const import WEBHOOK_CACHE_DEFAULT_TIMEOUT+from ...webhook.const import WEBHOOK_CACHE_DEFAULT_TTLfrom ...webhook.event_types import WebhookEventAsyncType, WebhookEventSyncTypefrom ...webhook.payloads import (generate_checkout_payload,@@ -2819,7 +2819,7 @@ class WebhookPlugin(BasePlugin):self.allow_replica,subscribable_object=list_payment_method_data,request_timeout=WEBHOOK_SYNC_TIMEOUT,- cache_timeout=WEBHOOK_CACHE_DEFAULT_TIMEOUT,+ cache_timeout=WEBHOOK_CACHE_DEFAULT_TTL,requestor=self.requestor,)if response_data:diff --git a/saleor/plugins/webhook/tests/test_list_shipping_methods_for_checkout.py b/saleor/plugins/webhook/tests/test_list_shipping_methods_for_checkout.pyindex 271f1308b8..a062e39fad 100644--- a/saleor/plugins/webhook/tests/test_list_shipping_methods_for_checkout.py+++ b/saleor/plugins/webhook/tests/test_list_shipping_methods_for_checkout.py@@ -4,6 +4,7 @@ from unittest import mockfrom django.utils import timezone+from ....webhook import constfrom ....webhook.event_types import WebhookEventSyncTypefrom ....webhook.payloads import generate_checkout_payloadfrom ....webhook.transport.shipping import (@@ -62,7 +63,7 @@ def test_get_shipping_methods_for_checkout_set_cache(@mock.patch("saleor.webhook.transport.synchronous.transport.cache.set")@mock.patch("saleor.webhook.transport.synchronous.transport.send_webhook_request_sync")-def test_get_shipping_methods_no_webhook_response_does_not_set_cache(+def test_get_shipping_methods_no_webhook_response_sets_short_term_cache(mocked_webhook,mocked_cache_set,webhook_plugin,@@ -71,6 +72,17 @@ def test_get_shipping_methods_no_webhook_response_does_not_set_cache():# givenmocked_webhook.return_value = None++ payload = generate_checkout_payload(checkout_with_item)+ key_data = get_cache_data_for_shipping_list_methods_for_checkout(payload)+ target_url = shipping_app.webhooks.first().target_url+ cache_key = generate_cache_key_for_webhook(+ key_data,+ target_url,+ WebhookEventSyncType.SHIPPING_LIST_METHODS_FOR_CHECKOUT,+ shipping_app.id,+ )+plugin = webhook_plugin()# when@@ -78,7 +90,11 @@ def test_get_shipping_methods_no_webhook_response_does_not_set_cache(# thenassert mocked_webhook.called- assert not mocked_cache_set.called+ mocked_cache_set.assert_called_once_with(+ cache_key,+ const.SYNC_WEBHOOK_FAILURE_SENTINEL,+ timeout=const.SYNC_WEBHOOK_FAILURE_CACHE_TTL,+ )@mock.patch("saleor.webhook.transport.synchronous.transport.cache.get")diff --git a/saleor/plugins/webhook/tests/test_list_stored_payment_methods_webhook.py b/saleor/plugins/webhook/tests/test_list_stored_payment_methods_webhook.pyindex 38c7039b8a..5d7afdf98c 100644--- a/saleor/plugins/webhook/tests/test_list_stored_payment_methods_webhook.py+++ b/saleor/plugins/webhook/tests/test_list_stored_payment_methods_webhook.py@@ -6,7 +6,7 @@ import graphenefrom ....core.models import EventDeliveryfrom ....payment.interface import ListStoredPaymentMethodsRequestDatafrom ....settings import WEBHOOK_SYNC_TIMEOUT-from ....webhook.const import WEBHOOK_CACHE_DEFAULT_TIMEOUT+from ....webhook import constfrom ....webhook.event_types import WebhookEventSyncTypefrom ....webhook.tests.subscription_webhooks.subscription_queries import (LIST_STORED_PAYMENT_METHODS as LIST_STORED_PAYMENT_METHODS_SUBSCRIPTION,@@ -79,7 +79,7 @@ def test_list_stored_payment_methods_with_static_payload(mocked_cache_set.assert_called_once_with(expected_cache_key,webhook_list_stored_payment_methods_response,- timeout=WEBHOOK_CACHE_DEFAULT_TIMEOUT,+ timeout=const.WEBHOOK_CACHE_DEFAULT_TTL,)assert response@@ -238,7 +238,7 @@ def test_list_stored_payment_methods_with_subscription_payload(mocked_cache_set.assert_called_once_with(expected_cache_key,webhook_list_stored_payment_methods_response,- timeout=WEBHOOK_CACHE_DEFAULT_TIMEOUT,+ timeout=const.WEBHOOK_CACHE_DEFAULT_TTL,)assert response@@ -350,6 +350,10 @@ def test_list_stored_payment_methods_app_returns_incorrect_response(assert not EventDelivery.objects.exists()mocked_cache_get.assert_called_once_with(expected_cache_key)- assert not mocked_cache_set.called+ mocked_cache_set.assert_called_once_with(+ expected_cache_key,+ const.SYNC_WEBHOOK_FAILURE_SENTINEL,+ timeout=const.SYNC_WEBHOOK_FAILURE_CACHE_TTL,+ )assert response == []diff --git a/saleor/plugins/webhook/tests/test_payment_method_initialize_tokenization.py b/saleor/plugins/webhook/tests/test_payment_method_initialize_tokenization.pyindex a52d3dccbb..1d56574565 100644--- a/saleor/plugins/webhook/tests/test_payment_method_initialize_tokenization.py+++ b/saleor/plugins/webhook/tests/test_payment_method_initialize_tokenization.py@@ -13,7 +13,7 @@ from ....payment.interface import (PaymentMethodTokenizationResult,)from ....settings import WEBHOOK_SYNC_TIMEOUT-from ....webhook.const import WEBHOOK_CACHE_DEFAULT_TIMEOUT+from ....webhook.const import WEBHOOK_CACHE_DEFAULT_TTLfrom ....webhook.event_types import WebhookEventSyncTypefrom ....webhook.models import Webhookfrom ....webhook.transport.utils import (@@ -496,7 +496,7 @@ def test_expected_result_invalidates_cache_for_app(mocked_cache_set.assert_called_once_with(expected_cache_key,list_stored_payment_methods_response,- timeout=WEBHOOK_CACHE_DEFAULT_TIMEOUT,+ timeout=WEBHOOK_CACHE_DEFAULT_TTL,)# whendiff --git a/saleor/plugins/webhook/tests/test_payment_method_process_tokenization.py b/saleor/plugins/webhook/tests/test_payment_method_process_tokenization.pyindex 2ee4ebe127..e81eb549f7 100644--- a/saleor/plugins/webhook/tests/test_payment_method_process_tokenization.py+++ b/saleor/plugins/webhook/tests/test_payment_method_process_tokenization.py@@ -12,7 +12,7 @@ from ....payment.interface import (PaymentMethodTokenizationResult,)from ....settings import WEBHOOK_SYNC_TIMEOUT-from ....webhook.const import WEBHOOK_CACHE_DEFAULT_TIMEOUT+from ....webhook.const import WEBHOOK_CACHE_DEFAULT_TTLfrom ....webhook.event_types import WebhookEventSyncTypefrom ....webhook.models import Webhookfrom ....webhook.transport.utils import (@@ -512,7 +512,7 @@ def test_expected_result_invalidates_cache_for_app(mocked_cache_set.assert_called_once_with(expected_cache_key,list_stored_payment_methods_response,- timeout=WEBHOOK_CACHE_DEFAULT_TIMEOUT,+ timeout=WEBHOOK_CACHE_DEFAULT_TTL,)# whendiff --git a/saleor/plugins/webhook/tests/test_stored_payment_method_request_delete.py b/saleor/plugins/webhook/tests/test_stored_payment_method_request_delete.pyindex 6f3ec2b188..54ae127814 100644--- a/saleor/plugins/webhook/tests/test_stored_payment_method_request_delete.py+++ b/saleor/plugins/webhook/tests/test_stored_payment_method_request_delete.py@@ -12,7 +12,7 @@ from ....payment.interface import (StoredPaymentMethodRequestDeleteResult,)from ....settings import WEBHOOK_SYNC_TIMEOUT-from ....webhook.const import WEBHOOK_CACHE_DEFAULT_TIMEOUT+from ....webhook.const import WEBHOOK_CACHE_DEFAULT_TTLfrom ....webhook.event_types import WebhookEventSyncTypefrom ....webhook.models import Webhookfrom ....webhook.transport.utils import (@@ -436,7 +436,7 @@ def test_stored_payment_method_request_delete_invalidates_cache_for_app(mocked_cache_set.assert_called_once_with(expected_cache_key,list_stored_payment_methods_response,- timeout=WEBHOOK_CACHE_DEFAULT_TIMEOUT,+ timeout=WEBHOOK_CACHE_DEFAULT_TTL,)# whendiff --git a/saleor/webhook/const.py b/saleor/webhook/const.pyindex 10b03fb11a..3abb50aa40 100644--- a/saleor/webhook/const.py+++ b/saleor/webhook/const.py@@ -1,5 +1,9 @@CACHE_EXCLUDED_SHIPPING_TIME = 60 * 3-WEBHOOK_CACHE_DEFAULT_TIMEOUT: int = 5 * 60 # 5 minutes+WEBHOOK_CACHE_DEFAULT_TTL: int = 5 * 60 # 5 minutes+SYNC_WEBHOOK_FAILURE_CACHE_TTL: int = 1 # 1 second+SYNC_WEBHOOK_FAILURE_SENTINEL = (+ "WEBHOOK_FAILURE" # Arbitrary value to indicate webhook failure in cache+)APP_ID_PREFIX = "app"MAX_FILTERABLE_CHANNEL_SLUGS_LIMIT = 500diff --git a/saleor/webhook/transport/synchronous/transport.py b/saleor/webhook/transport/synchronous/transport.pyindex e7c552244c..d2ee6d8095 100644--- a/saleor/webhook/transport/synchronous/transport.py+++ b/saleor/webhook/transport/synchronous/transport.py@@ -37,8 +37,7 @@ from ....payment.utils import (from ....webhook.circuit_breaker.breaker_board import (initialize_breaker_board,)-from ... import observability-from ...const import WEBHOOK_CACHE_DEFAULT_TIMEOUT+from ... import const, observabilityfrom ...event_types import WebhookEventSyncTypefrom ...payloads import generate_transaction_action_request_payloadfrom ...utils import get_webhooks_for_event@@ -210,6 +209,14 @@ def trigger_webhook_sync_if_not_cached(cache_data, webhook.target_url, event_type, webhook.app_id)response_data = cache.get(cache_key)+ if response_data == const.SYNC_WEBHOOK_FAILURE_SENTINEL:+ # Prevent sending webhook if the previous one failed recently.+ logger.warning(+ "[Webhook] Skipping request to %s for event %s due to previous failure.",+ sanitize_url_for_logging(webhook.target_url),+ event_type,+ )+ return Noneif response_data is None:response_data = trigger_webhook_sync(event_type,@@ -226,7 +233,13 @@ def trigger_webhook_sync_if_not_cached(cache.set(cache_key,response_data,- timeout=cache_timeout or WEBHOOK_CACHE_DEFAULT_TIMEOUT,+ timeout=cache_timeout or const.WEBHOOK_CACHE_DEFAULT_TTL,+ )+ else:+ cache.set(+ cache_key,+ const.SYNC_WEBHOOK_FAILURE_SENTINEL,+ timeout=const.SYNC_WEBHOOK_FAILURE_CACHE_TTL,)return response_data